Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Find Integer values in String
Message
 
To
16/12/2010 11:27:36
Vishal Nyamagoudar
Swift Infocom India Pvt Ltd
India
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
ASP.NET
OS:
Windows XP SP2
Network:
Windows XP
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01492931
Message ID:
01493117
Views:
55
>ASP.net using C#
>
>In asp.net text box user will may input string like following formats
>
>Sachin has scored 20/2 Runs
> or
>Sachin has scored 12-2 Runs
> or
>Sachin has scored 5*2 Runs
> or
>Sachin has scored 8+2 Runs
>
>out should get in this format on button Click::
>
>Sachin has scored 10 Runs

In addition to my previous reply (that can give you a generic approach), in your particular case you can do plain coding like:
' VB.NET
Public Sub iSimpleMacro()
        Dim lcString As String = "Sachin has scored 20/2 Runs"
        '        lcString = "Sachin has scored 12-2 Runs"
        '       lcString = "Sachin has scored 5*2 Runs"
        '      lcString = "Sachin has scored 8+2 Runs"
        Dim lcCode As String = iChrTran(lcString, "0123456789+-*/", "")
        lcCode = iChrTran(lcString, lcCode, "")

        Dim lcAO As String = iChrTran(lcCode, "0123456789", "")
        Dim lcNum1 As String = Left(lcCode, lcCode.IndexOf(lcAO))
        Dim lcNum2 As String = lcCode.Substring(lcCode.IndexOf(lcAO) + 1)

        Dim lcResult As String = "{Cannot calculate}"

        Select Case lcAO
            Case "+"
                lcResult = (Val(lcNum1) + Val(lcNum2)).ToString
            Case "-"
                lcResult = (Val(lcNum1) - Val(lcNum2)).ToString
            Case "*"
                lcResult = (Val(lcNum1) * Val(lcNum2)).ToString
            Case "/"
                lcResult = (Val(lcNum1) / Val(lcNum2)).ToString
        End Select
        MsgBox(lcString.Replace(lcCode, lcResult))

    End Sub
    Function iChrTran(ByVal cSearchedExpression As String, ByVal cSearchExpression As String, ByVal cReplacementExpression As String)
        Dim ichr As Integer = 0, retval As String = cSearchedExpression
        While ichr <= cSearchExpression.Length - 1
            If IsNothing(cReplacementExpression) OrElse cReplacementExpression.Length = 0 Then
                retval = retval.Replace(cSearchExpression.Substring(ichr, 1), "")
            Else
                retval = retval.Replace(cSearchExpression.Substring(ichr, 1), _
                                        cReplacementExpression.Substring(IIf(ichr <= cReplacementExpression.Length - 1, _
                                                    ichr, cReplacementExpression.Length - 1), 1))
            End If
            ichr = ichr + 1
        End While
        Return retval
    End Function
Good Luck
Previous
Reply
Map
View

Click here to load this message in the networking platform