When using StringBuilder, note that you should specify the initial capacity when constructing the StringBuilder object. Otherwise, the default capacity is 16 characters. When the default capacity is exceeded due to append characters, A new string buffer is allocated, which is twice the size of the original buffer.
C # string inversion in Algorithm Implementation:
Code Public static string Reverse (string str)
{
If (string. IsNullOrEmpty (str ))
{
Throw new ArgumentException ("invalid parameter ");
}
StringBuilder sb = new StringBuilder (str. Length );
For (int index = str. Length-1; index> = 0; index --)
{
Sb. Append (str [index]);
}
Return sb. ToString ();
}
Public static string Reverse (string str)
{
If (string. IsNullOrEmpty (str ))
{
Throw new ArgumentException ("invalid parameter ");
}
StringBuilder sb = new StringBuilder (str. Length );
For (int index = str. Length-1; index> = 0; index --)
{
Sb. Append (str [index]);
}
Return sb. ToString ();
}