How do I Find a non numeric value in a string?

mickbarrs
Member Posts: 17
I'm trying to find the position of the first value that is not a numeric value within a string.
pTxtNumber is passed in and could be 10-15672 for example. So I want to return position 3 because this is the first non numeric character. Then I want to chop of the string up to and including this character so that I am left with 15672.
This code i initially did checks for where the numeric value begins but now i want to do the opposit.
FOR i := 1 TO STRLEN(pTxtNumber) DO BEGIN
IF Pos = 0 THEN
IF FORMAT(pTxtNumber) IN THEN
Pos := i;
END;
Any help would be greatly appreciated.
Cheers
Mike
pTxtNumber is passed in and could be 10-15672 for example. So I want to return position 3 because this is the first non numeric character. Then I want to chop of the string up to and including this character so that I am left with 15672.
This code i initially did checks for where the numeric value begins but now i want to do the opposit.
FOR i := 1 TO STRLEN(pTxtNumber) DO BEGIN
IF Pos = 0 THEN
IF FORMAT(pTxtNumber) IN THEN
Pos := i;
END;
Any help would be greatly appreciated.
Cheers
Mike
0
Comments
-
In place of
IF FORMAT(pTxtNumber) IN THEN
put
IF NOT (FORMAT(pTxtNumber) IN ) THEN
I think it could work ...0 -
'The prefix operator cannot be used on text'. I also tried:
IF FORMAT(pTxtNumber) NOT IN THEN
and this did not work either.
I suppose i could enter into a substring 'abcdefg-!"£$%^&*' etc and then check in my string to see if it contains any of the characters in the substring but i thought there might be a more efficient way of saying find the first non-numeric character.
Any ideas?0 -
txtString := '10-15672'; txtNoNumbers := DELCHR(txtString,'=','1234567890'); // remain only non-number chars and the first char in the resulting string will be the first non-numeric char in the original string WHILE txtNoNumber <> '' DO BEGIN // take the first char of "txtNoNumber" (there can be multiple non-numeric chars) // and give me the position where it is in "txtString" intNoNumber := STRPOS(txtString,COPYSTR(txtNoNumber,1,1)); // "txtString" now starts AFTER the first non-numeric char txtString := COPYSTR(txtString,intNoNumber + 1); // lets check if there are other non-numeric chars txtNoNumbers := DELCHR(txtString,'=','1234567890'); END; MESSAGE('The string "txtString" (=%1) doesn''t contain non-numeric chars',txtString);
Regards,Alain Krikilion
No PM,please use the forum. || May the <SOLVED>-attribute be in your title!0 -
If the numbers are always separated by a dash then you could do:
TargetString := DELCHR(SourceString,'=','-');
if there are more saparation characters, (like . or ,) you could do:TargetString := DELCHR(SourceString,'=','-.,');
if you want to remove letters you do:TargetString := DELCHR(SourceString,'=','abcdefghijklmnopqrstuvwxyz');
etcetera
<edit>so that still doesn't give you the position of the first non numeric character..... let me think about that</edit>0 -
SourceString - Text30
TargetString - Text30
IFoundIt - boolean
MyPos - IntegerMyPos := 0; IFoundIt := FALSE; SourceString := '123-456'; REPEAT MyPos += 1; IF DELCHR(COPYSTR(SourceString,MyPos,1),'=','0123456789') <> '' THEN IFoundIt := TRUE; IF MyPos = STRLEN(SourceString) THEN BEGIN IFoundIt := TRUE; MyPos := 0; // no non-numeric value found, so return 0 END; UNTIL IFoundIt; MESSAGE('position is ' + format(MyPos));
That should find the first non-numeric character in SourceString. So then you wrap it in a function and use return value.0 -
how about
mystr := '10-98765'; FOR i := 1 TO STRLEN(mystr) DO BEGIN IF NOT (mystr[i] IN [48..57]) THEN BEGIN // ascii for 0 thru 9 newstr := COPYSTR(mystr,i+1); i := STRLEN(mystr); // drops out of for statement END; END; MESSAGE('%1',newstr)
0 -
so how do you get the ascii value out of the char? I tried with MESSAGE, but that returns the alphanumeric character.0
-
Chars can be treated as integers
AsciiNo is integer
mystr is textmystr := '10-2376' AsciiNo:= mystr[2]; // save char value as integer (ie the ascii value) message('ASCII No. is %1',AsciiNo);
Message returns 48 which is ASCII No. for 0 (zero)0 -
i := 1; while (mystr[i] in ['0'..'9']) and (i<= StrLen(mystr)) do i := i+1; if i>StrLen(mystr) then Exit(0) //only numbers in string... else Exit(i); //return the position of the non-numeric character
That's all folks... 8)1 -
Yeah right, with code samples from two other people I can write really efficient code too0
-
I just added the = FALSE to the second line. This then tells me the line number where the first non-numeric character is.
FOR i := 1 TO STRLEN(pTxtNumber) DO BEGIN
IF FORMAT(pTxtNumber) IN = FALSE THEN
Pos := i;
END;
IntNumber := 0;
IF Pos <> 0 THEN BEGIN
Alpha := DELSTR(pTxtNumber, 1, Pos);
EVALUATE(IntNumber, Alpha);
END;
EXIT(IntNumber);
Thanks for the help!!!
Mike0 -
If your goal is to generate an error if the code contains a non numeric character, why not just do EVALUATE(MyInt,TheWholeThing)?0
-
The code below by Spost29 does just the trick. Mine works but this is a lot clearer.
I just wanted to take the part of the string after the non-numeric character which in this case is '-'. So i wanted to return 98765.
mystr := '10-98765';
FOR i := 1 TO STRLEN(mystr) DO BEGIN
IF NOT (mystr IN [48..57]) THEN BEGIN // ascii for 0 thru 9
newstr := COPYSTR(mystr,i+1);
i := STRLEN(mystr); // drops out of for statement
END;
END;
MESSAGE('%1',newstr)0 -
how about:
mystr := '10-98765'; FOR i := 1 TO STRLEN(mystr) DO BEGIN IF NOT (mystr[i] IN [48..57]) THEN mystr := DELCHR(mystr,'=',format(mystr[i])); END; MESSAGE('%1',mystr)
0 -
Thanks for that. Will give it a try on Monday!!!0
-
the gift that keeps giving
I think it is so cool that these old posts are still helping people
0
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