String Functions

zalemale

Newcomer
Joined
Feb 5, 2003
Messages
2
Hi guys. I'm a bit new to this language, and currently I am having difficulty with writing a function for an algorithm testing if a string is in the correct format or not. The function I'm making is boolean, and it's purpose is to accept a string in percentage format (for example...1.23%, 1.23, or .0123 formats only). I have successfully made the algorithm so that the last two formats can be validated in their respective formats, however I can't figure out how to make an input that includes the "%" character to be validated. I have an idea of how to go about this with the right$(string, int) property (as in VB), but I can't seem to get this to work. Any thoughts would be appreciated.

Thanks,
zm
 
Thanks for the advice. Now if I wanted to include the value with the percentage sign within the variable for calculations, how would I remove it when actually performing the calculation (but then display it again once the calculation is finished). For example: Say I want the value 1.34% to be included within a calculation. In order to verify it's in the correct format, I run the "isPercent" function I'm been making on it, realize it's valid input, and then proceed with the calculation (but without the percentage sign since otherwise the program would be halted). I don't want this to effect the display of the number though (If it's inputted as 1.34% in the first place, I don't really want the percent sign to be erased in the text box). Any advice?

Thanks
 
Pass the whole thing to your function as a string (1.34%), then...
Visual Basic:
       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = calc(TextBox1.Text.ToString)
    End Sub

    Private Function calc(ByVal s As String) As String
        If s.EndsWith("%") Then
            s = s.Substring(0, s.Length - 1)
        End If

        Dim n As Double

        If IsNumeric(s) Then
            n = CType(s, Double)
            n += 25.2
        End If

        Return n.ToString & "%"

    End Function
another way is to return as double and do the formatting in the textbox.
 
Back
Top