[RESOLVED] Instead of one charcter it outputs three spaces
Hello, I'm trying to remove last spaces from some strings however my function behaves strangely.
For example when my string is: "word1 word2 word3 word4 "
... and function is
Code:
Private Function TrimLastSpaces(s As String) As String
Dim i As Integer
For i = s.Length To 0 Step -1
If (Mid(s, i - 2, i - 1) = " ") Then
Console.WriteLine("s = " & s)
s = Mid(s, 1, s.Length - 2)
i = s.Length()
Else
Console.WriteLine(Mid(s, i - 2, i - 1))
Exit For
End If
Next
Return s
End Function
it doesn't go in "if" statement, it goes in "else" statement and outputs three space charcters while I'm expecting it to remove space characters one by one.
Maybe it's a very simple thing I'm missing but I couldn't figure it out.
Re: Instead of one charcter it outputs three spaces
Ok, I missed it again. I think it should be: If (Mid(s, i - 1, 1) = " ") Then
Re: Instead of one charcter it outputs three spaces
try this:
Code:
Private Function TrimLastSpaces(ByVal s As String) As String
Return String.Concat(s.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries))
End Function
Re: [RESOLVED] Instead of one charcter it outputs three spaces
Thank you for your reply. The code you gave didn't change the string.
Edit: I forgot "str = TrimLastSpaces(str)" and tried it again but it removed all spaces instead of the last ones.