C# String.Substring equivalent for StringBuilder?

James picture James · Aug 13, 2014 · Viewed 15.8k times · Source

StringBuilder does not appear to have a Substring(start, len) method... what am I missing here?

Answer

Steve picture Steve · Aug 13, 2014

The StringBuilder class has a special version of the ToString method that takes two arguments, exactly as Substring(startIndex, length).

 StringBuilder sb = new StringBuilder("This is a Test");
 string test = sb.ToString(10, 4);
 Console.WriteLine(test);   // result = Test