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,
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):
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):
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):
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.