Next Previous Contents

66. strchop

Synopsis

Chop or split a string into substrings.

Usage

Integer_Type strchop (String_Type str, Integer_Type delim, Integer_Type quote)

Description

The strchop function may be used to split-up a string str that consists of substrings delimited by the character specified by delim. If the integer quote is non-zero, it will be taken as a quote character for the delimiter. The function returns to the stack a variable number of objects. The top stack object represents the number of substrings that are on the stack. For example,

          strchop ("apples,oranges,pears", ',', 0);
will return the integer 3 to the top of the stack followed by the three strings: "apples", "oranges", and "pears".
Example

       define list_to_array (list)
       {
          variable n, i, s, a;
          n = strchop (list, ',', '\\');
          a = String_Type [n];
          for (i = 0; i < n; i++)
            a[i] = ();             % substring is on stack
          return a;
       }
This defines a function that converts a comma-separated list of strings to string array containing the substrings.
Notes

Since this function returns a variable number of values to the stack, it cannot chop up arbitrarily long strings that consist of many substrings.

The function strchopr should be used if it is desired to have the string chopped-up in the reverse order.

See Also

strchopr, extract_element, create_delimited_string


Next Previous Contents