Extracting number from string

kallileo
2019-07-23
2019-07-26
  • kallileo

    kallileo - 2019-07-23

    I read a text file with saved parameters which has the following structure:

    123
    492
    500
    6091
    etc

    The resulting string looks like the following:
    '123$R$N492$R$N500$R$N6091'

    How can I extract all the numbers without the special characters from the string and assing them to an INT array?

     
  • dFx

    dFx - 2019-07-23

    split your string using the special chars ( Cr Lf ) into a string array.
    Then cast each string item to the desired datatype (int, real …)

     
  • kallileo

    kallileo - 2019-07-23

    The following code worked for me:

    PROGRAM PLC_PRG
    VAR
       parameters: ARRAY[1..4] OF INT;
       splitString : BOOL;
       fileString : STRING := '123$R$N492$R$N500$R$N6091$R$N';
       arINT1 : INT;
       first : STRING;
       fileStringDel : STRING;
       index : INT := 1; 
    END_VAR
    
    IF splitString THEN
       FOR index:=1 TO 4 DO
          arINT1 := FIND(fileString,'$R$N');
          first := LEFT(fileString,arINT1-1);
          fileString := DELETE(fileString,arINT1+1,1);
          parameters[index]:=STRING_TO_INT(first);
       END_FOR
       splitString := FALSE;
       index := 0;   
    END_IF
    

    Unfortunately the maximum editable string size is 255 characters so not many parameters can be saved into one string.

     
  • Comingback4u

    Comingback4u - 2019-07-24

    Try looking at StringUtils library. You can do something like this to have way more than 255 characters:

    PROGRAM ParameterExtractor
    VAR
        sLongString : STRING(1024) := '01234567890$R$N1234567890$R$N2345678901$R$N3456789012$R$N4567890123$R$N5678901234$R$N6789012345';
        sShortString : STRING;
        aParameters : ARRAY[0..MAXPARAMETERS] OF ULINT;
        sSplitChars : STRING := '$R$N';
        iSplitLocation : INT;
        iCount : INT := 0;
    END_VAR
    VAR CONSTANT
        MAXPARAMETERS : INT := 9;
    END_VAR
    //Loop through string until there is no string left or array has hit its limits
        WHILE StrLenA(pstData:= ADR(sLongString)) > 0 DO
            //Find location of first break point
            iSplitLocation := StrFindA(pst1:= ADR(sLongString), pst2:= ADR(sSplitChars), uiSearchStart:= 1) - 1;
            
            //If there is no break point found then set location to size of string
            IF iSplitLocation = -1 THEN
                iSplitLocation := DINT_TO_INT(StrLenA(pstData:= ADR(sLongString)));
            END_IF
          
            //Copy the part of the string that is needed for the parameter.
            StrMidA(
                    pst:= ADR(sLongString), 
                    uiInputBufferSize:= SIZEOF(sLongString), 
                    iLength:= iSplitLocation, 
                    iPosition:= 1, 
                    pstResult:= ADR(sShortString), 
                    uiResultBufferSize:= SIZEOF(sShortString));
            //Make sure string is not empty before using up an array spot.
            IF StrLenA(pstData:= ADR(sShortString)) > 0 THEN
                aParameters[iCount] := STRING_TO_ULINT(sShortString);
                iCount := iCount + 1;
            END_IF
            //Remove copied components from long string
            StrDeleteA(pby:= ADR(sLongString), iLength:= (iSplitLocation + 2), iPosition:= 1);
            //Check to see if more parameters can fit in array. If not exit loop
            IF iCount > MAXPARAMETERS THEN
                iCount := 0;
                EXIT;
            END_IF 
        END_WHILE
    
     
  • kallileo

    kallileo - 2019-07-25

    @Comingback4u

    You code looks much better than mine.
    So by using this library it's possible to manipulate strings longer than 255 characters?

     
  • Comingback4u

    Comingback4u - 2019-07-26

    Yes, In the above example I define the size of the string by declaring "sLongString : STRING(1024) := ..." so we are at 4x the standard size of STRING. With the StringUtil library you specify the size of the buffer using the SIZEOF(sLongString) so you aren't limited to the 255. You can go much higher than 1024 but this was just for example.

     

Log in to post a comment.