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

Dani AI

Generated

If you just need a safe "backspace" on the user’s input, avoid substring math and use a StringBuilder. It handles length changes cleanly and is handy if you add a keypad-style Backspace button in your converter UI:

Imports System.Text
Imports Microsoft.VisualBasic

Dim s As String = Interaction.InputBox("Enter number")
If Not String.IsNullOrEmpty(s) Then
    Dim sb As New StringBuilder(s)
    If sb.Length > 0 Then sb.Length -= 1  'remove last char
    s = sb.ToString()
End If

'Validate/parse after trimming to ensure it is numeric
Dim value As Decimal
If Not Decimal.TryParse(s,
                        Globalization.NumberStyles.Number,
                        Globalization.CultureInfo.CurrentCulture,
                        value) Then
    'handle invalid number (show message, reset, etc.)
End If

For cases like post #8 where you want to drop the last comma-separated field before a closing parenthesis (regardless of the field’s value), a simple regex avoids manual scanning:

Imports System.Text.RegularExpressions

Dim result As String = Regex.Replace(inputLine, ",[^,)]*\)$", ")")

Notes:

  • Use Decimal.TryParse rather than permissive checks to catch culture-specific signs and decimal separators.
  • If you only want to remove a trailing newline or whitespace, call TrimEnd() before the removal step.

References:

Recommended Answers

All 10 Replies

Member Avatar for Member #46692

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

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)

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

Dim str As string 
str = "Ramy1"
str = str.Remove(str.Length - 1);
' str = Ramy

i think wayne already answered this thread. :D

Yes yes thanks JX :D

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)'

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?

$string = "Hello Daniweb1234";
$string= substr_replace($string ,"",-4);
echo $string;

--display--
Hello Daniweb

commented: Incorrect and also not relevant -2
  1. This thread is old. Don't revive it.
  2. Your answer has nothing to do with the question.
  3. Your code isn't VB code.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.