String Manipulation with the StringBuilder and String Classes in C #
StringBuilder and String classes provide statement Ways to perform string manipulation.If your program Requires Many manipulations, then the StringBuilder class Will Likely Be More Efficient Than using the String class. The String class Provides year immutable object, That Which means clustering oz the value of the string instance is set, it Can not Be changed. Even though it Appears, that 'the implementation is changing the value of the string instance, Actually It Is Returning a new instance of the String class in memory.Reason for this, the StringBuilder class Will Likely causes less "overhead" in your application.
The examples show how to use the StringBuilder class to append, insert, and replace text Within a StringBuilder instance. It aussi show how to use the String class to copy, split, and find a substring Within a Given String instance.
StringBuilder.Append
The Append method Takes the specified argument and appends it to the end value of the Existing Held in a StringBuilder object. This example is a console application, Which Asks the user to input his / her name. The implementation Takes the name Entered by the user and appends it to the greeting That Is Held in the StringBuilder object "Greeting". The append method of the StringBuilder class HAS 19 overloads. This example uses the Append method of the string object to append to the Name Greeting.The StringBuilder is initialized Greeting with no value, and the value "Good Morning" is Appended to string representation. Next, the name the user is Enters Appended with the code:
Greeting.Append ("" + Name + "!");
StringBuilder.Insert
The Insert method Takes a specified argument and inserts the value at the specified location Within The Existing value of the StringBuilder object. This example is a console application, Which is similar to the Append example. The implementation Takes the name Entered by the user and inserts it at the specified location. The Insert method overloads HAS 18. This example uses the Insert method of the String object to insert the name you enter the after the words "Good Morning" and Before the "!" With the Following code:
Greeting.Insert (12 "," Name +);
The first argument is integer year value Representing the location Within The string to insert the value. The second argument is the value to insert into the Existing string. If the integer value is Greater Than Supplied the length of the string value Contained Within The StringBuilder object (for example, if the value is 35 Entered here), has "System.ArgumentOutOfRangeException" will might occur.
StringBuilder.Replace
The Replace Existing method searches the string value and replaces all instances of the specified value with Another allowance. This example builds on the Insert example by Asking the user to enter a nickname aussi Into the application. If the User has Entered a name and a nickname, then the Application Will replace the original name with the nickname in the StringBuilder "Greeting" object code with the Following:
Greeting.Replace (Name, Nickname);
String.Copy
The Copy method of the String class Creates a new instance of a string value with the Sami have the string copied Being. In this example the user Enters some text, and Placed Into That value is a string object named "inputText". The implementation copies this value Into the new object named "copiedText" and appends Some additional text in the Following code:
string inputText = Console.ReadLine ();
string = copiedText string.Copy (myText);
copiedText + = "more text";
Copy method is a static method of the String class and therefore is Called using the class name and not the instance name.
String.Split and String.Join
The Split method identified a substring delimited Within The string specified by one or more characters. The substring values are Placed Into a string array. In this example, the string instance "textOriginal" contains the value "Value 1, Value 2, Value 3". The implementation calls the split method and initializes the "textarray" string array with the Following code:
string [] = textArray text.Split ('');
The example then loops-through all of the values in the array and writes' em to the screen.
The Join method of the String class concatenates a specified character string of losses Within a string array and Produces a single string. In this example, the "textarray" string array is re-joined using a ":" with the Following code:
newJoin string = string.join ("", textArray);
Join method is a static method of the String class and therefore is Called using the class name and not the instance name.
String.Substring
The Substring method returns a string representation from a specified start point. This method overloads Where Has Two aussi one method accepts a length argument. In this example, a string is extracted from the original string instance 4th at the Hello! Character and Having a length of 3.
Contained originalString.Substring string = (4, 3). ToString ();
StringBuilder and String classes provide statement Ways to perform string manipulation.If your program Requires Many manipulations, then the StringBuilder class Will Likely Be More Efficient Than using the String class. The String class Provides year immutable object, That Which means clustering oz the value of the string instance is set, it Can not Be changed. Even though it Appears, that 'the implementation is changing the value of the string instance, Actually It Is Returning a new instance of the String class in memory.Reason for this, the StringBuilder class Will Likely causes less "overhead" in your application.
The examples show how to use the StringBuilder class to append, insert, and replace text Within a StringBuilder instance. It aussi show how to use the String class to copy, split, and find a substring Within a Given String instance.
StringBuilder.Append
The Append method Takes the specified argument and appends it to the end value of the Existing Held in a StringBuilder object. This example is a console application, Which Asks the user to input his / her name. The implementation Takes the name Entered by the user and appends it to the greeting That Is Held in the StringBuilder object "Greeting". The append method of the StringBuilder class HAS 19 overloads. This example uses the Append method of the string object to append to the Name Greeting.The StringBuilder is initialized Greeting with no value, and the value "Good Morning" is Appended to string representation. Next, the name the user is Enters Appended with the code:
Greeting.Append ("" + Name + "!");
StringBuilder.Insert
The Insert method Takes a specified argument and inserts the value at the specified location Within The Existing value of the StringBuilder object. This example is a console application, Which is similar to the Append example. The implementation Takes the name Entered by the user and inserts it at the specified location. The Insert method overloads HAS 18. This example uses the Insert method of the String object to insert the name you enter the after the words "Good Morning" and Before the "!" With the Following code:
Greeting.Insert (12 "," Name +);
The first argument is integer year value Representing the location Within The string to insert the value. The second argument is the value to insert into the Existing string. If the integer value is Greater Than Supplied the length of the string value Contained Within The StringBuilder object (for example, if the value is 35 Entered here), has "System.ArgumentOutOfRangeException" will might occur.
StringBuilder.Replace
The Replace Existing method searches the string value and replaces all instances of the specified value with Another allowance. This example builds on the Insert example by Asking the user to enter a nickname aussi Into the application. If the User has Entered a name and a nickname, then the Application Will replace the original name with the nickname in the StringBuilder "Greeting" object code with the Following:
Greeting.Replace (Name, Nickname);
String.Copy
The Copy method of the String class Creates a new instance of a string value with the Sami have the string copied Being. In this example the user Enters some text, and Placed Into That value is a string object named "inputText". The implementation copies this value Into the new object named "copiedText" and appends Some additional text in the Following code:
string inputText = Console.ReadLine ();
string = copiedText string.Copy (myText);
copiedText + = "more text";
Copy method is a static method of the String class and therefore is Called using the class name and not the instance name.
String.Split and String.Join
The Split method identified a substring delimited Within The string specified by one or more characters. The substring values are Placed Into a string array. In this example, the string instance "textOriginal" contains the value "Value 1, Value 2, Value 3". The implementation calls the split method and initializes the "textarray" string array with the Following code:
string [] = textArray text.Split ('');
The example then loops-through all of the values in the array and writes' em to the screen.
The Join method of the String class concatenates a specified character string of losses Within a string array and Produces a single string. In this example, the "textarray" string array is re-joined using a ":" with the Following code:
newJoin string = string.join ("", textArray);
Join method is a static method of the String class and therefore is Called using the class name and not the instance name.
String.Substring
The Substring method returns a string representation from a specified start point. This method overloads Where Has Two aussi one method accepts a length argument. In this example, a string is extracted from the original string instance 4th at the Hello! Character and Having a length of 3.
Contained originalString.Substring string = (4, 3). ToString ();
No comments:
Post a Comment