Options

count number of a substring within a string

OliverTOliverT Member Posts: 37
is there any function to count the number of substrings within a string (something like "substr_count" in php) or do i have to write my own function for this?

Comments

  • Options
    afarrafarr Member Posts: 287
    I don't think there is a built-in function, but something like the following should work:

    StrTemp := Str;
    SubStrCount := 0;
    SubStrLen := STRLEN(SubStr);
    SubStrPos := STRPOS(StrTemp,SubStr);

    WHILE SubStrPos > 0 DO
    BEGIN
    SubStrCount := SubStrCount + 1;
    StrTemp := DELSTR(StrTemp,SubStrPos,SubStrLen);
    SubStrPos := STRPOS(StrTemp, SubStr);
    END;
    Alastair Farrugia
  • Options
    OliverTOliverT Member Posts: 37
    yes, i already thought that i have to program it myself (the "how" is not the problem ;) ). i was just surprised that a functions for this doesn't exist.
  • Options
    OliverTOliverT Member Posts: 37
    here is the code for this function ;)

    Function "SubStrCount"

    Parameters
    String (Text, 250)
    Substring (Text, 250)

    Return Value
    Number (Integer)
    WHILE STRPOS(String,Substring) > 0 DO BEGIN
        Number := Number + 1;
        String := COPYSTR(String,STRPOS(String,SubString)+STRLEN(SubString));
    END;
    

    i hope you can need it 8)
Sign In or Register to comment.