Truncate the last character from a given string

wonder_gal

Active member
Joined
Jun 5, 2006
Messages
34
Programming Experience
1-3
Hi.

Can anyone give me an example of how to truncate the rightmost character from a given string? Only one character need to be truncated.

Thanks.
 
pasensyoso_manigbas said:
Trim function can also be used...
Hello pasensyoso,
Would you mind if i ask you to show me an example how can you remove the mostright letter from the string using the trim method please?
I am asking cuz as far i know Trim is using to removes all of the blank spaces ...
Thanks ;)
 
hi kulrom,

I make use of Trim this way...

VB.NET:
Dim x As String = "werfd56"
Dim y As String = x.Trim(x.ToCharArray(6, 1))
MsgBox(y)

it would remove the "6"

Hope that would make a difference...
 
Last edited by a moderator:
Trim removes all occurrences of a set of characters specified in an array from the beginning and end of this instance. So if your string is '666weird666' and you do that x.Trim(x(x.Length - 1)) the result is 'weird'. Not the requested behaviour or what?
 
Of the 3 ways you can remove one char from the end of the string (without using other classes), Trim is not one of them:

Substring(0, length-1)
Remove(length-1)
New String(oldString.ToCharArray, 0, length-1)

it is likely that the first and last methods are synonymous, since a new string must be created with the end cut off anyway..
 
pasensyoso_manigbas said:
hi kulrom,

I make use of Trim this way...

VB.NET:
Dim x As String = "werfd56"
Dim y As String = x.Trim(x.ToCharArray(6, 1))
MsgBox(y)

it would remove the "6"

Hope that would make a difference...

In your case Trim does nothing (well almost nothing) while the one which does something is ToCharArray method of String that again without Trim will not do the same but anyway this is not a way to remove the last character from certain string.
However your way removes the last character. Thanks for your time :)
 
thanx also kulrom for enlighting... since im new to .net i only apply every possible ways I can think of...
 
Hi thanks kulrom for raising up this issue, and also thanks cjard for explaining us the 3 proper technique, thanks u guys for the sharing.... :)
 
Back
Top