How to change TXT become int?

Discussion in 'C#' started by wacamoi, Jun 10, 2009.

  1. #1
    Question:1

    74 is a txt form how to become int form ?

    Question: 2

    7y_4oo6!! is a txt string how to get 746 from the txt string and make 746 int form?


    Thanks/
    Regards,
     
    wacamoi, Jun 10, 2009 IP
  2. Social.Network

    Social.Network Member

    Messages:
    517
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    35
    #2
    Are you using ASP or ASP.NET? I will assume ASP and VBScript for now.

    Response 1: Use the CInt function to convert to an Integer subtype
    Response 2: Use RegEx or loop through the string to clean the string

    Example (simple):

    
    Dim strVariable
    strVariable = "74"
    Response.Write CInt(strVariable)
    
    Dim strVariable2
    Dim strVariable3
    Dim strCharacter
    strVariable2 = "7y_4oo6!!"
    
    For i = 1 To Len(strVariable2)
       strCharacter = Mid(strVariable2,i,1)
       If IsNumeric(strCharacter) Then strVariable3 = strVariable3 & strCharacter
    Next
    
    Response.Write CInt(strVariable3)
    Code (markup):
     
    Social.Network, Jun 11, 2009 IP
    wacamoi likes this.
  3. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    For asp.net you can use regex to remove anything that isn't numeric then CInt to convert to integer. e.g:
    
       Dim myStr As String
       Dim myInt As Integer
    
       myStr = "73sdf12"
       myStr = Regex.Replace(myStr, "[^0-9]+?", "")
       myInt = CInt(myStr)    'Returns 7312 as an Integer 
    
    Code (markup):
     
    camjohnson95, Jun 11, 2009 IP
  4. dopanel.com

    dopanel.com Peon

    Messages:
    93
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    In Asp ,you don't need to convert the type of text which is made up by some digital.
    The second problem can using RegEx Or Using Code below:
    
      Function GetNumber(Str)
        On Error Resume Next
        Dim TmpStr
        Dim Counter
        For Counter = 1 to Len(Str)
            Cint(Mid(Str,Counter,1))
            If Err.Number=0 Then
              TmpStr = TmpStr & Mid(Str,Counter,1)
            End If
        Next
        GetNumber = TmpStr
      ENd Function
    
    Code (markup):
     
    dopanel.com, Jun 11, 2009 IP
    wacamoi likes this.
  5. Social.Network

    Social.Network Member

    Messages:
    517
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    35
    #5
    I would NEVER recommend the above. The CInt function throws an error if the character is not numeric and then you check the error number to control program flow.
     
    Social.Network, Jun 11, 2009 IP
  6. wacamoi

    wacamoi Peon

    Messages:
    810
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I am also think about a string without any number or empty.
     
    wacamoi, Jun 11, 2009 IP
  7. dopanel.com

    dopanel.com Peon

    Messages:
    93
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yes,but this is the simple way to get the number in the string.
    RegEx is not easy for starter!
     
    dopanel.com, Jun 12, 2009 IP