Trim Function

  • Thread starter Thread starter cmdolcet69
  • Start date Start date
C

cmdolcet69

How do I setup a string command to trim the length of the string -1

I need to get ride of the last character in my string
 
How do I setup a string command to trim the length of the string -1

I need to get ride of the last character in my string

Dim strText As String = "ABCDEFGHX"

strText = strText.Remove(strText.Length - 1)

OR

strText = strText.Substring(0, strText.Length - 1)

Chris
 
Why not do it the .NET way?

newString = oldString.SubString(0, oldString.Length - 1)

Surely that should be

If oldString IsNot Nothing AndAlso oldString.Length>1 Then
newString = oldString.SubString(0, oldString.Length - 1)
Else
newString=String.Empty
End If

?

Or, using VB,

newString=String.Left(oldString, Len(oldString)-1)

Andrew
 
Andrew,,,

or

Dim NewString as string = string.empty

If Not string.IsNullOrEmpty(oldString) AndAlso oldString.Length>1 then
newString = oldString.SubString(0, oldString.Length - 1)
end if

And this is exactly why the VB functions ( framework shortcuts ) are so
handy :-)


regards

Michel
 
Already explained in your other post. Please don't start a new thread for
the same query.
 
If Not string.IsNullOrEmpty(oldString) AndAlso oldString.Length>1 then

Isn't the check for the length of the string here, unnecessary? If
the call to IsNullOrEmpty returns False, then by definition, it's
length must be at least 1.

Chris
 
Euhmmm...... :-| yes you are right,,, forgot something


Dim NewString as string = string.empty
Dim Oldstring as string ="X"

If Not string.IsNullOrEmpty(oldString) AndAlso oldString.Length>1 then
newString = oldString.SubString(0, oldString.Length - 1)
else
NewString=OldString
end if

Cause in case of a one char string i asume the TS wants the original value
and not a empty string
in the case he wanted a empty string in this sitaution he can remove the
length and else struct
 
Euhmmm...... :-| yes you are right,,, forgot something


Dim NewString as string = string.empty
Dim Oldstring as string ="X"

If Not string.IsNullOrEmpty(oldString) AndAlso oldString.Length>1 then
newString = oldString.SubString(0, oldString.Length - 1)
else
NewString=OldString
end if

Cause in case of a one char string i asume the TS wants the original value
and not a empty string
in the case he wanted a empty string in this sitaution he can remove the
length and else struct
 
Back
Top