what is string builder in c#
In C#, the StringBuilder class represents a mutable string of characters that can be changed. It's defined in the System.Text namespace and assembly System.Runtime.dll.
Declare and initialize: Use the following constructors:
StringBuilder():
Creates a new instance of the StringBuilder class
StringBuilder(Int32):
Creates a new instance of the StringBuilder class using the specified capacity
Pass a string value as an argument: For example, ` StringBuilder s = new StringBuilder(`GeeksforGeeks'');
Specify a value for the maximum number of characters: For example, ` StringBuilder s = new StringBuilder(20);`
Use methods for string manipulation: For example, Insert, Remove, and Replace
What is a string builder?
Stringbuilder in Java: Constructors, Methods, and Examples ...
StringBuilder in Java is a class used to create a mutable, or in other words, a modifiable succession of characters. Like StringBuffer, the StringBuilder class is an alternative to the Java Strings Class, as the Strings class provides an immutable succession of characters
Example:
// C# program to demonstrate the
// StringBuilder.Append(value) and
// StringBuilder.AppendLine(value) method
using System;
using System.Text;
class GFG {
// Main Method
public static void Main()
{
// "20" is capacity
StringBuilder s = new StringBuilder("HELLO ", 20);
s.Append("GFG");
// after printing "GEEKS"
// a new line append
s.AppendLine("GEEKS");
s.Append("GeeksForGeeks");
Console.WriteLine(s);
}
}
No comments:
Post a Comment