954,551 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How do I delete the last character of a string

I am a beginner in VB. I am trying to make a simple unit converter. Can please some body tell me how can I delete the last character of the number I enter in my inputbox?

thank you

shoebodh
Newbie Poster
1 post since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

Take input as a string. Chop of the last character, then convert string to number.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

I assume you are putting your input to a textbox. So try this:

TextBox1.Text = InputBox("Enter number")
        TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.Text.Length - 1)
waynespangler
Posting Pro in Training
461 posts since Dec 2002
Reputation Points: 84
Solved Threads: 58
 

I am a beginner in VB. I am trying to make a simple unit converter. Can please some body tell me how can I delete the last character of the number I enter in my inputbox?

thank you


You also could use the IsNumeric(TextboxName.Text) Then
TextboxName.Text.Length -1 as the other poster stated which will subtract the last number in the textbox

SolTec
Junior Poster in Training
67 posts since Jan 2008
Reputation Points: 10
Solved Threads: 4
 
Dim str As string 
str = "Ramy1"
str = str.Remove(str.Length - 1);
' str = Ramy
Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 

i think wayne already answered this thread. :D

Jx_Man
Nearly a Senior Poster
3,329 posts since Nov 2007
Reputation Points: 1,372
Solved Threads: 444
 

Yes yes thanks JX :D

Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 

I need to remove the last ",-1,-1,-1" from this line


'XXXXXX XXXX `XXXXXXXXXXXX` XXXXX (XXXX,'XX','XXXX','YYYY YYY YYYYYY YYYYY YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY YYYYYYYYYYYYYYYYYYY.',Y,'YYYYY YY YYYY YYY YYYY XX.XX',X,X,-1,-1,-1)'

dhanalakahmi s
Newbie Poster
1 post since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

Is that a specific example of a more general case?. For example, if the last three fields are always -1,-1,-1 then you just need to replace ",-1,-1,-1)" with ")". However, if the last three fields can take on other values then one solution would be a loop that would look for the last "," and strip off the last field. You could do (for example):

Private Function StripLast(ByVal flds As String, ByVal numflds As Integer) As String

	Do While numflds > 0
		flds = flds.Substring(0, flds.LastIndexOf(","))
		numflds -= 1
	Loop

	Return flds & ")"

End Function


will return the string with the last given number of fields stripped off. Sample call is:

flds = "YYYYYYYYYYYYYYYYYYY.',Y,'YYYYY YY YYYY YYY YYYY XX.XX',X,X,-1,-1,-1)"
flds = StripLast(flds,3)

'flds now has the value "YYYYYYYYYYYYYYYYYYY.',Y,'YYYYY YY YYYY YYY YYYY XX.XX',X,X)"


Is this what you intended?

Reverend Jim
Posting Shark
Moderator
1,169 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You