Just a quick question in case anyone has done it allready.
I recieve several strings in this format
"abc123ab"
"123bs"
etc
The letters either end can be any letter up to a maximum of three letters per string each end but could be no letters and only the number is significant as it is used to sort a whole structured array.
Is there an easy way to extract just the number into an integer variable?
Regards
CD
Extract Number From String
-
- Addict
- Posts: 1309
- Joined: Fri Aug 28, 2015 6:10 pm
- Location: Portugal
Extract Number From String
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
- RSBasic
- Moderator
- Posts: 1218
- Joined: Thu Dec 31, 2009 11:05 pm
- Location: Gernsbach (Germany)
- Contact:
Re: Extract Number From String
Code: Select all
EnableExplicit
Procedure.s StringToNumberString(String$)
Protected a
Protected CurrentCharacter$
Protected NumberString$
For a=1 To Len(String$)
CurrentCharacter$ = Mid(String$, a, 1)
If Asc(CurrentCharacter$) > 47 And Asc(CurrentCharacter$) < 58
NumberString$ + CurrentCharacter$
EndIf
Next
ProcedureReturn NumberString$
EndProcedure
Debug StringToNumberString("abc0123ab")
Debug StringToNumberString("0123bs")
Code: Select all
EnableExplicit
Procedure StringToNumber(String$)
Protected a
Protected CurrentCharacter$
Protected NumberString$
For a=1 To Len(String$)
CurrentCharacter$ = Mid(String$, a, 1)
If Asc(CurrentCharacter$) > 47 And Asc(CurrentCharacter$) < 58
NumberString$ + CurrentCharacter$
EndIf
Next
ProcedureReturn Val(NumberString$)
EndProcedure
Debug StringToNumber("abc123ab")
Debug StringToNumber("123bs")
Or you use this RegEx code: (\d+)
Code: Select all
If CreateRegularExpression(0, "(\d+)")
Dim Result$(0)
NbFound = ExtractRegularExpression(0, "abc123ab", Result$())
For k = 0 To NbFound-1
Debug Result$(k)
Next
Else
Debug RegularExpressionError()
EndIf
-
- Addict
- Posts: 1309
- Joined: Fri Aug 28, 2015 6:10 pm
- Location: Portugal
Re: Extract Number From String
Excellent All good
Thanks
CD
Thanks
CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Re: Extract Number From String
Other regex
Code: Select all
EnableExplicit
Enumeration
#Regex
EndEnumeration
Procedure.i Extract_Number(Str_Input.s)
CreateRegularExpression(#Regex, "\d+")
If ExamineRegularExpression(#Regex, Str_Input)
While NextRegularExpressionMatch(#Regex)
ProcedureReturn Val(RegularExpressionMatchString(#Regex))
Wend
EndIf
; FreeRegularExpression(#Regex) ; Not needed here because working in Procedure will auto free ressources when leave
EndProcedure
Debug Extract_Number("abc123ab")
Debug Extract_Number("123bs")
Re: Extract Number From String
@RSBasic
never use a function as the -To- expression. it will be executed every single time:
also iterating through strings like that, is very slow and inefficient. pointers should be a LOT faster:
c ya,
nco2k
never use a function as the -To- expression. it will be executed every single time:
Code: Select all
Procedure Len2(String$)
Debug String$
ProcedureReturn Len(String$)
EndProcedure
For i = 1 To Len2("Test") : Next
Debug "---"
Len2 = Len2("Test")
For i = 1 To Len2 : Next
Code: Select all
Procedure.q StringToNumber(*String.Character)
Protected Result.q, *Number, Length
While *String\c
If *String\c >= '0' And *String\c <= '9'
If *Number = #False
*Number = *String
EndIf
Length + 1
ElseIf *Number
Break
EndIf
*String + SizeOf(Character)
Wend
If *Number
Result = Val(PeekS(*Number, Length))
EndIf
ProcedureReturn Result
EndProcedure
Debug StringToNumber(@"abc123ab")
Debug StringToNumber(@"123bs")
Debug StringToNumber(@"123")
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf