Number of options in comma separated string.

zulqzulq Member Posts: 204
Hi,
I would like to know how to get the number of substrings in a string with comma separated values. For example:
str := 'abcde,jhedk,kehhd,eieje,dkdkfj'
This string has five strings with numbers 1-5. Thus if I want to get the last substring I just have to use selectstr(5,str). Now I want to be able to know the last number of the given string and thus loop through the end to get all substrings. Is there any way to get this number.


Thanks.
Few years ago we were not existing and few years to come we would be in the grave! So what will benefit us in the grave?

Answers

  • zulqzulq Member Posts: 204
    Anyone out there knows how to do this?
    Few years ago we were not existing and few years to come we would be in the grave! So what will benefit us in the grave?
  • SPost29SPost29 Member Posts: 148
    CountSubStrings(String : Text[250];Character : Text[1]) CharCount : Integer
    // This function returns the count of elements in the string
    // Character is the delimiter you are looking for ','
    CharCount := 0; // local Var
    CharPos := 0; // local Var
    CountingString := ''; // local Var

    CountingString := TrimString(String,Character); //Call to 2nd function below

    CharPos := STRPOS(String,Character);

    REPEAT
    CountingString := COPYSTR(CountingString,CharPos+1);
    CharCount := CharCount + 1;
    CharPos := STRPOS(CountingString,Character);
    UNTIL CharPos = 0;

    EXIT(CharCount + 1);


    TrimString(String : Text[250];Character : Text[1]) TrimmedString : Text[250]
    //This removes the Character from the beginning and/or end of the string and returns the trimmed string

    TrimmedString := String;
    IF (STRPOS(String,Character) = 1) THEN
    TrimmedString := COPYSTR(String,2);
    IF (COPYSTR(String,STRLEN(String)) = Character) THEN
    TrimmedString := COPYSTR(TrimmedString,1,STRLEN(TrimmedString)-1);
  • zulqzulq Member Posts: 204
    Thanks SPost29.
    Few years ago we were not existing and few years to come we would be in the grave! So what will benefit us in the grave?
  • rsnavrsnav Member Posts: 5
    You also might try this one.

    Str := 'abcde,jhedk,kehhd,eieje,dkdkfj';
    MESSAGE('%1',STRLEN(DELCHR(UPPERCASE(Str),'=','ABCDEFGHIJKLMNOPQRSTUVWXYZ')) + 1);

    If your option string contains any other chars besides a-z you should append them to the pattern string.
Sign In or Register to comment.