I am trying to remove a last character of a string. This last character is a newline (System.Environment.NewLine).
I have tried some things, but I can not remove it.
Example:
myString.Remove(sFP.Length - 1)
Example 2:
myString= Replace(myString, Environment.NewLine, "", myString.Length - 1)
How I can do it?
If your newline is CR LF, it's actually two consecutive characters. Try your Remove
call with Length - 2
.
If you want to remove all "\n" and "\r" characters at the end of string, try calling TrimEnd
, passing the characters:
str.TrimEnd(vbCr, vbLf)
To remove all the whitespace characters (newlines, tabs, spaces, ...) just call TrimEnd
without passing anything.
Dim str As String = "Test" & vbCrLf
str = str.Substring(0, str.Length - vbCrLf.Length)
the same with Environment.NewLine instead of vbCrlf:
str = "Test" & Environment.NewLine
str = str.Substring(0, str.Length - Environment.NewLine.Length)
Btw, the difference is: Environment.NewLine is platform-specific(f.e. returns other string in other OS)
Your remove
-approach didn't work because you didn't assign the return value of this function to your original string reference:
str = str.Remove(str.Length - Environment.NewLine.Length)
or if you want to replace all NewLines:
str = str.Replace(Environment.NewLine, String.Empty)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With