Monday, September 27, 2010

Removing the Last Character of String

Here my requirement is "I need to remove the last character of the string ". I search for this and i found a solution to remove the last character of a string  by using TrimEnd(',') method  , means which character you want want to remove place that end of the character in between single cottation
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

You can easily remove white spaces from both ends of a string using the String.Trim method, as shown in the following code example.
 
 [ 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 String.TrimEnd method removes characters from the end of a string, creating a new string object. An array of characters is passed to this method to specify the characters to be removed. The order of the elements in the character array does not affect the trim operation. The trim stops when a character not specified in the array is found.
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 String.TrimStart method is similar to the String.TrimEnd method except that it creates a new string by removing characters from the beginning of an existing string object. An array of characters is passed to the TrimStart method to specify the characters to be removed. As with the TrimEnd method, the order of the elements in the character array does not affect the trim operation. The trim stops when a character not specified in the array is found.
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 String.Remove method, beginning at a specified position in an existing string, removes a specified number of characters. This method assumes a zero-based index.
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