4.22 Analysing and Constructing Atoms
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
      • Built-in Predicates
        • Analysing and Constructing Atoms
          • atom_codes/2
          • atom_chars/2
          • char_code/2
          • number_chars/2
          • number_codes/2
          • atom_number/2
          • name/2
          • term_to_atom/2
          • atom_to_term/3
          • atom_concat/3
          • atomic_concat/3
          • atomic_list_concat/2
          • atomic_list_concat/3
          • atom_length/2
          • atom_prefix/2
          • sub_atom/5
          • sub_atom_icasechk/3
    • Packages
Availability:built-in
[ISO]sub_atom(+Atom, ?Before, ?Length, ?After, ?SubAtom)
ISO predicate for breaking atoms. It maintains the following relation: SubAtom is a sub-atom of Atom that starts at (0-based index) Before, has Length characters, and Atom contains After characters after the match. The implementation minimises non-determinism and creation of atoms. This is a flexible predicate that can do search, prefix- and suffix-matching, etc.

Pick out a sub-atom of length 3 starting a 0-based index 2:

?- sub_atom(aaxyzbbb, 2, 3, After, SubAtom).
After = 3,
SubAtom = xyz.

On backtracking, matches are delivered in order left-to-right (i.e. Before increases monotonically):

?- sub_atom('xATGATGAxATGAxATGAx', Before, Length, After, 'ATGA').
Before = 1, Length = 4, After = 14 ;
Before = Length, Length = 4, After = 11 ;
Before = 9, Length = 4, After = 6 ;
Before = 14, Length = 4, After = 1 ;
false.

See also sub_string/5, the corresponding predicate for SWI-Prolog strings.