find numbers in a string

uri84uri84 Member Posts: 30
edited 2013-07-02 in NAV Three Tier
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!

Comments

  • Marije_BrummelMarije_Brummel Member, Moderators Design Patterns Posts: 4,262
    I would create a while loop on the strlen of the variable evaluating variable
  • Peter76Peter76 Member Posts: 10
    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.
  • tarkmylertarkmyler Member Posts: 57
    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, OR
Sign In or Register to comment.