Ex: here i am adding two textboxes text and i am removing ,(camma) at the end of the combined text
string CCAddress= CC.Text + "," + CC2.Text; //adding two textboxes text into CCAddress
string CCadd = CCAddress.TrimEnd(','); //removing the , at the end
More About Trim Method :
Trimming and Removing Characters
If you are parsing a sentence into individual words, you might end up with words that have blank spaces ( also called white spaces ) on either end of the word. In this situation, you can use one of the trim methods in the System.String class to remove any number of spaces or other characters from a specified position in the string. The following table describes the available trim methods.Method Name | Use | ||||||||
---|---|---|---|---|---|---|---|---|---|
String.Trim | Removes white spaces from the beginning and end of a string. | ||||||||
String.TrimEnd | Removes characters specified in an array of characters from the end of a string. | ||||||||
String.TrimStart | Removes characters specified in an array of characters from the beginning of a string. | ||||||||
String.Remove | Removes a specified number of characters from a specified index position in a string. |
Trim
[ VB ] Dim myString As String = " Big " Console.WriteLine ( "Hello{0}World!", myString ) Dim TrimString As String = myString.Trim ( ) Console.WriteLine ( "Hello{0}World!", TrimString )
[ C# ]
String myString = " Big ";
Console.WriteLine ( "Hello{0}World!", myString );
string TrimString = myString.Trim ( );
Console.WriteLine ( "Hello{0}World!", TrimString );
This code displays the following lines to the console:Hello Big World! HelloBigWorld!
TrimEnd
The following code example removes the last letters of a string using the TrimEnd method. In this example, the position of the 'r' character and the 'W' character are reversed to illustrate that the order of characters in the array does not matter. Notice that this code removes the last word of myString plus part of the first.
[ VB ] Dim myString As String = "Hello World!" Dim myChar As Char ( ) = {"r"c, "o"c, "W"c, "l"c, "d"c, "!"c, " "c } Dim NewString As String = myString.TrimEnd ( myChar ) Console.WriteLine ( NewString )
[ C# ]
string myString = "Hello World!";
char [ ] myChar = {'r','o','W','l','d','!',' '};
string NewString = myString.TrimEnd ( myChar );
Console.WriteLine ( NewString );
This code displays He to the console. The following code example removes the last word of a string using the TrimEnd method. In this code, a comma follows the word Hello and, because the comma is not specified in the array of characters to trim, the trim ends at the comma.
[ VB ] Dim myString As String = "Hello, World!" Dim myChar As Char ( ) = {"r"c, "o"c, "W"c, "l"c, "d"c, "!"c, " "c } Dim NewString As String = myString.TrimEnd ( myChar ) Console.WriteLine ( NewString )
[ C# ]
string myString = "Hello, World!";
char [ ] myChar = {'r','o','W','l','d','!',' '};
string NewString = myString.TrimEnd ( myChar );
Console.WriteLine ( NewString );
This code displays Hello, to the console. TrimStart
The following code example removes the first word of a string. In this example, the position of the 'l' character and the 'H' character are reversed to illustrate that the order of characters in the array does not matter.
[ VB ]
Dim myString As String = "Hello World!"
Dim myChar As Char ( ) = {"e"c, "H"c, "l"c, "o"c, " "c}
Dim NewString As String = myString.TrimStart ( myChar )
Console.WriteLine ( NewString )
[ C# ]
string myString = "Hello World!";
char [ ] myChar = {'e', 'H','l','o',' ' };
string NewString = myString.TrimStart ( myChar );
Console.WriteLine ( NewString );
This code displays World! to the console. Remove
The following code example removes six characters from a string beginning at position five of a zero-based index of the string.
[ VB ] Dim myString As String = "Hello Beautiful World!" Console.WriteLine ( myString.Remove ( 5, 10 ) )
[ C# ]
string myString = "Hello Beautiful World!";
Console.WriteLine ( myString.Remove ( 5,10 ) );
This code displays Hello World! to the console.
No comments:
Post a Comment