Csharp/CSharp Tutorial/String/StringBuilder

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Add formatted string to StringBuilder

using System;
using System.Text;
class MainClass
{
    public static void Main()
    {
        StringBuilder sb = new StringBuilder();
        int number = 1;
        sb.AppendFormat("{0}: {1} ", number++, "another string");
        Console.WriteLine("{0}", sb);
    }
}
1: another string

Append("A ").Append("B ").Append("C... ")

using System;
using System.Text;
public class MainClass
{
    static void Main() {
        StringBuilder sb = new StringBuilder();
        sb.Append("A ").Append("B ").Append("C... ");
        string built1 = sb.ToString();
        sb.Append("D");
        string built2 = sb.ToString();
        Console.WriteLine( built1 );
        Console.WriteLine( built2 );
    }
}
A B C...
A B C... D

Append string and insert string to a StringBuilder

using System;
using System.Text;
class MainClass
{
  static void Main(string[] args)
  {
      StringBuilder MyString = new StringBuilder("Hello");
    
      MyString.Insert(0, "My");
      MyString.Append("World");
    
      Console.WriteLine(MyString);
  }
}
MyHelloWorld

Display the contents of StringBuilder

using System;
using System.Text;
class MainClass
{
  public static void Main()
  {
    StringBuilder myStringBuilder = new StringBuilder();
    
    myStringBuilder.Append("myString");
    
    
    myStringBuilder.Append("Here"s another string", 0, 4);

    Console.WriteLine(myStringBuilder);
  }
}
myStringHere

Set Length property of StringBuilder

using System;
using System.Text;
class MainClass
{
    public static void Main()
    {
      StringBuilder sb = new StringBuilder("test string");
      int length = 0;
    
      length = sb.Length;
      Console.WriteLine("The result is: "{0}"", sb);
      Console.WriteLine("The length is: {0}", length);
    
      sb.Length = 4;
      length = sb.Length;
      Console.WriteLine("The result is: "{0}"", sb);
      Console.WriteLine("The length is: {0}", length);
    
      sb.Length = 20;
      length = sb.Length;
      Console.WriteLine("The result is: "{0}"", sb);
      Console.WriteLine("The length is: {0}", length);
    }
}
The result is: "test string"
The length is: 11
The result is: "test"
The length is: 4
The result is: "test                "
The length is: 20

StringBuilder: append value with format

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Security.Cryptography;
public class MainClass
{
    public static void Main()
    {
        StringBuilder sb = new StringBuilder("Foo");
        sb.AppendFormat("...{0}", DateTime.Now);
        string final = sb.ToString();
        Console.WriteLine(final);
    }
}
Foo...25/03/2007 3:03:25 PM

StringBuilder: Length, Capacity, MaxCapacity

using System;
using System.Text;
class MainClass
{
  public static void DisplayProperties(string name,StringBuilder myStringBuilder)
  {
    Console.WriteLine(name + ".Length = " + myStringBuilder.Length);
    Console.WriteLine(name + ".Capacity = " + myStringBuilder.Capacity);
    Console.WriteLine(name + ".MaxCapacity = " + myStringBuilder.MaxCapacity);
  }
  public static void Main()
  {
    StringBuilder myStringBuilder = new StringBuilder();
    int capacity = 50;
    StringBuilder myStringBuilder2 = new StringBuilder(capacity);
    int maxCapacity = 100;
    StringBuilder myStringBuilder3 = new StringBuilder(capacity, maxCapacity);
    string myString = "To be or not to be";
    StringBuilder myStringBuilder4 = new StringBuilder(myString);
    int startIndex = 0;
    int stringLength = myString.Length;
    StringBuilder myStringBuilder5 = new StringBuilder(myString, startIndex, stringLength, capacity);
    DisplayProperties("myStringBuilder", myStringBuilder);
    DisplayProperties("myStringBuilder2", myStringBuilder2);
    DisplayProperties("myStringBuilder3", myStringBuilder3);
    DisplayProperties("myStringBuilder4", myStringBuilder4);
    DisplayProperties("myStringBuilder5", myStringBuilder5);
  }
}
myStringBuilder.Length = 0
myStringBuilder.Capacity = 16
myStringBuilder.MaxCapacity = 2147483647
myStringBuilder2.Length = 0
myStringBuilder2.Capacity = 50
myStringBuilder2.MaxCapacity = 2147483647
myStringBuilder3.Length = 0
myStringBuilder3.Capacity = 50
myStringBuilder3.MaxCapacity = 100
myStringBuilder4.Length = 18
myStringBuilder4.Capacity = 32
myStringBuilder4.MaxCapacity = 2147483647
myStringBuilder5.Length = 18
myStringBuilder5.Capacity = 50
myStringBuilder5.MaxCapacity = 2147483647

Use StringBuilder to reverse a string

using System;
using System.Text;
class MainClass
{
    public static void Main()
    {
        string str = "www.nfex.ru";
        
        StringBuilder revStr = new StringBuilder();
        for (int count = str.Length - 1; count > -1; count--)
        {
            revStr.Append(str[count]);
        }
        
        Console.WriteLine(revStr.ToString());
    }
}
moc.s2avaj.www

Use the AppendFormat() method to add a formatted string containing a floating point number to myStringBuilder

using System;
using System.Text;
class MainClass
{
  public static void Main()
  {
    StringBuilder myStringBuilder = new StringBuilder();
    myStringBuilder.Append("friends");
    
    float myFloat = 1234.56789f;
    myStringBuilder.AppendFormat("{0, 10:f3}", myFloat);
    Console.WriteLine("myStringBuilder = " + myStringBuilder);
    Console.WriteLine(myStringBuilder);
  }
}
myStringBuilder = friends  1234.568
friends  1234.568

Use the Append() method to append a character to StringBuilder

using System;
using System.Text;
class MainClass
{
  public static void Main()
  {
    StringBuilder myStringBuilder = new StringBuilder();
    
    myStringBuilder.Append("myString");
    
    myStringBuilder.Append("z", 5);

    Console.WriteLine(myStringBuilder);
  }
}
myStringzzzzz

Use the Append() method to append a substring to StringBuilder

using System;
using System.Text;
class MainClass
{
  public static void Main()
  {
    StringBuilder myStringBuilder = new StringBuilder();
    
    myStringBuilder.Append("myString");
    
    
    myStringBuilder.Append("Here"s another string", 0, 4);

    Console.WriteLine(myStringBuilder);
  }
}
myStringHere

Use the Append() method to append two strings, an int, and a bool to StringBuilder

using System;
using System.Text;
class MainClass
{
  public static void Main()
  {
    StringBuilder myStringBuilder = new StringBuilder();
    
    myStringBuilder.Append("myString");
    myStringBuilder.Append(" ... ");
    int myInt = 1234;
    myStringBuilder.Append(myInt);
    bool myBool = true;
    myStringBuilder.Append(myBool);

    Console.WriteLine(myStringBuilder);
  }
}
myString ... 1234True

Use the Insert() method to insert strings into StringBuilder

using System;
using System.Text;
class MainClass
{
  public static void Main()
  {
    StringBuilder myStringBuilder = new StringBuilder();
    myStringBuilder.Append("friends");
    
    myStringBuilder.Insert(2, "friends, ");
    myStringBuilder.Insert(2, "Romans, ", 3);
    Console.WriteLine("myStringBuilder = " + myStringBuilder);
  }
}

Use the Remove() method to remove part of StringBuilder

using System;
using System.Text;
class MainClass
{
  public static void Main()
  {
    StringBuilder myStringBuilder = new StringBuilder();
    myStringBuilder.Append("friends");
    
    myStringBuilder.Insert(2, "friends, ");
    myStringBuilder.Insert(2, "Romans, ", 3);
    Console.WriteLine("myStringBuilder = " + myStringBuilder);
    
    myStringBuilder.Remove(4, 7);
    Console.WriteLine("myStringBuilder = " + myStringBuilder);
  }
}

Use the Replace() method to replace part of StringBuilder

using System;
using System.Text;
class MainClass
{
  public static void Main()
  {
    StringBuilder myStringBuilder = new StringBuilder();
    myStringBuilder.Append("friends");
    
    myStringBuilder.Insert(2, "friends, ");
    myStringBuilder.Insert(2, "Romans, ", 3);
    Console.WriteLine("myStringBuilder = " + myStringBuilder);
    
    myStringBuilder.Replace(",", "?");
    Console.WriteLine("myStringBuilder = " + myStringBuilder);
  }
}

Use the ToString() method to convert StringBuilder to a string

using System;
using System.Text;
class MainClass
{
  public static void Main()
  {
    StringBuilder myStringBuilder = new StringBuilder();
    
    
    myStringBuilder.Append("friends");
    
    string myString2 = myStringBuilder.ToString();
    Console.WriteLine("myString2 = " + myString2);
  }
}
myString2 = friends