find numbers in a string

uri84
Member Posts: 30
hi,
i have a problem and i wnat to know if someone can help me
i have a string, for example: 'sadadas 132 asdad 8/32'
and i want to get the first numbers.
This (132)
what is the better option to do this?
with STRPOS for detect the first number in the string and then find the first blank space?
how can i do this? thanks!
i have a problem and i wnat to know if someone can help me
i have a string, for example: 'sadadas 132 asdad 8/32'
and i want to get the first numbers.
This (132)
what is the better option to do this?
with STRPOS for detect the first number in the string and then find the first blank space?
how can i do this? thanks!
0
Comments
-
I would create a while loop on the strlen of the variable evaluating variable0
-
It depends on how your strings are constructed.
If there is always a space before and after the numbers, you could get always one part of the string (from start - first space, or from space to space) and try to use EVALUATE to determine if it is a number. if it is, exit the loop or function with the result. The sample below is just one way of doing it. "string" is a text variable, "pos" and "pos2" are integer, and "result" is decimal.string := 'test 123 test test 456'; pos := 1; pos2 := STRPOS(string,' '); WHILE (pos > 0) AND (result = 0) DO BEGIN IF EVALUATE(result,COPYSTR(string,pos,pos2-pos)) THEN; pos := pos2 + 1; pos2 := STRPOS(COPYSTR(string,pos),' ') + pos; IF pos = 0 THEN pos := STRLEN(string); END;
If the numbers could be anywhere in the string, you would have to start looking at each character in a string and if the character is a number, you add it to your result and keep on doing that until you hit a non-number. The sample below is another way of doing this. "string" and "result" are text variables, "pos" is integer, "IsNumeric" is boolean.string := 'test 123 test test 456'; pos := 1; WHILE (pos <= STRLEN(string)) AND (result = '') OR ((result <> '') AND IsNumeric) DO BEGIN IsNumeric := string[pos] IN ['0'..'9',',','.','-','+']; IF IsNumeric THEN result := result + FORMAT(string[pos]); pos += 1; END;
Peter Zentner
NAV Practice Manager at Clients First Business Solutions
Did you check out our free training videos at www.dynamics-nav-training.com?
Look at our blogs.0 -
How about this? :P
Adjust as necessary for decimal format with punctuation.OBJECT Report 50012 Why was 456 afraid of 7? { OBJECT-PROPERTIES { Date=07/01/13; Time=[ 6:28:19 PM]; Modified=Yes; Version List=; } PROPERTIES { ProcessingOnly=Yes; } DATAITEMS { { PROPERTIES { DataItemTable=Table2000000026; MaxIteration=1; DataItemVarName=funwithintegers; OnAfterGetRecord=BEGIN result := getFirstNum(string2parse); END; } SECTIONS { { PROPERTIES { SectionType=Body; SectionWidth=12000; SectionHeight=846; } CONTROLS { } } } } } REQUESTFORM { PROPERTIES { Width=9020; Height=3410; } CONTROLS { } } CODE { VAR result@1102611000 : Integer; ok@1102611001 : Boolean; string2parse@1102611002 : TextConst 'ENU=why was 456 afraid of 7? Because 789.'; PROCEDURE getFirstNum@1102611000(InString@1102611000 : Text[1000]) IntegerOut : Integer; VAR Numbers@1102611001 : TextConst 'ENU=0123456789'; i@1102611002 : Integer; numstring@1102611004 : Text[1000]; BEGIN IF DELCHR(InString,'=',' ') = '' THEN EXIT; FOR i := 1 TO STRLEN(InString) DO IF STRPOS(Numbers,COPYSTR(InString,i,1)) <> 0 THEN numstring := numstring + COPYSTR(InString,i,1) ELSE IF numstring <> '' THEN i := STRLEN(InString); IF EVALUATE(IntegerOut,numstring) THEN; END; BEGIN END. } }
Mark Tyler
Pacific City, OR0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions