• Places
    • Home
    • Graphs
    • Prefixes
  • Admin
    • Users
    • Settings
    • Plugins
    • Statistics
  • CPACK
    • Home
    • List packs
    • Submit pack
  • Repository
    • Load local file
    • Load from HTTP
    • Load from library
    • Remove triples
    • Clear repository
  • Query
    • YASGUI SPARQL Editor
    • Simple Form
    • SWISH Prolog shell
  • Help
    • Documentation
    • Tutorial
    • Roadmap
    • HTTP Services
  • Login

5.2.2 Predicates that operate on strings
All Application Manual Name SummaryHelp

  • 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
sub_string(+String, ?Before, ?Length, ?After, ?SubString)
This predicate is functionally equivalent to sub_atom/5, but operates on strings. Note that this implies the string input arguments can be either strings or atoms. If SubString is unbound (output) it is unified with a string. The following example splits a string of the form <name>=<value> into the name part (an atom) and the value (a string).
name_value(String, Name, Value) :-
    sub_string(String, Before, _, After, "="),
    !,
    sub_atom(String, 0, Before, _, Name),
    sub_string(String, _, After, 0, Value).

The next example defines a predicate that inserts a value at a position. See sub_atom/5 for more examples.

string_insert(Str, Val, At, NewStr) :-
    sub_string(Str, 0, At, A1, S1),
    sub_string(Str, At, A1, _, S2),
    atomics_to_string([S1,Val,S2], NewStr).
ClioPatria (version V3.1.1-51-ga0b30a5)