Delete the last character of a string (VB.NET 2008)

I am trying to remove the last character of a string. This last character is a new line (System.Environment.NewLine).

I tried something, but I can not remove it.

Example:

myString.Remove(sFP.Length - 1) 

Example 2:

 myString= Replace(myString, Environment.NewLine, "", myString.Length - 1) 

How can i do this?

+6
string
source share
3 answers

If your new line is CR LF, these are actually two consecutive characters. Try making a Remove call with Length - 2 .

If you want to delete all characters "\ n" and "\ r" at the end of a line, try calling TrimEnd , passing in the characters:

 str.TrimEnd(vbCr, vbLf) 

To remove all whitespace (newlines, tabs, spaces, ...), just call TrimEnd without passing anything.

+16
source share
 Dim str As String = "Test" & vbCrLf str = str.Substring(0, str.Length - vbCrLf.Length) 

same with Environment.NewLine instead of vbCrlf:

 str = "Test" & Environment.NewLine str = str.Substring(0, str.Length - Environment.NewLine.Length) 

Btw, the difference is this: Environment.NewLine is platform-specific (fe returns another line in another OS)

Your remove -approach does not work because you did not assign the return value of this function to the original string link:

 str = str.Remove(str.Length - Environment.NewLine.Length) 

or if you want to replace all NewLines:

 str = str.Replace(Environment.NewLine, String.Empty) 
+3
source share

Using:

 Dim str As String str = "cars,cars,cars" str = str.Remove(str.LastIndexOf(",")) 
-one
source share

All Articles