5.2.2 Predicates that operate on strings
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
      • SWI-Prolog extensions
        • The string type and its double quoted syntax
          • Predicates that operate on strings
            • atom_string/2
            • number_string/2
            • term_string/2
            • term_string/3
            • string_chars/2
            • string_codes/2
            • string_bytes/3
            • text_to_string/2
            • string_length/2
            • string_code/3
            • get_string_code/3
            • string_concat/3
            • split_string/4
            • sub_string/5
            • atomics_to_string/2
            • atomics_to_string/3
            • string_upper/2
            • string_lower/2
            • read_string/3
            • read_string/5
            • open_string/2
    • Packages
Availability:built-in
[det]split_string(+String, +SepChars, +PadChars, -SubStrings)
Break String into SubStrings. The SepChars argument provides the characters that act as separators and thus the length of SubStrings is one more than the number of separators found if SepChars and PadChars do not have common characters. If SepChars and PadChars are equal, sequences of adjacent separators act as a single separator. Leading and trailing characters for each substring that appear in PadChars are removed from the substring. The input arguments can be either atoms, strings or char/code lists. Compatible with ECLiPSe. Below are some examples:

A simple split wherever there is a‘.':

?- split_string("a.b.c.d", ".", "", L).
L = ["a", "b", "c", "d"].

Consider sequences of separators as a single one:

?- split_string("/home//jan///nice/path", "/", "/", L).
L = ["home", "jan", "nice", "path"].

Split and remove white space:

?- split_string("SWI-Prolog, 7.0", ",", " ", L).
L = ["SWI-Prolog", "7.0"].

Only remove leading and trailing white space (trim the string):

?- split_string("  SWI-Prolog  ", "", "\s\t\n", L).
L = ["SWI-Prolog"].

In the typical use cases, SepChars either does not overlap PadChars or is equivalent to handle multiple adjacent separators as a single (often white space). The behaviour with partially overlapping sets of padding and separators should be considered undefined. See also read_string/5.