Trim Function

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
 
C

Chris Dunaway

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
 
A

Andrew Morton

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
 
M

Michel Posseth [MCP]

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
 
J

James Hahn

Already explained in your other post. Please don't start a new thread for
the same query.
 
C

Chris Dunaway

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
 
M

Michel Posseth [MCP]

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
 
M

Michel Posseth [MCP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top