Table of Contents

String Handling

This set of utility functions remove specified characters from sequences


  • func compress(sequence pSource, integer pRep)    Replaces embedded whitespace with pRep
  • func compress_chars(sequence pSource, sequence pChars, integer pRep)    Replaces embedded sequences of pChars with pRep
  • func lookup(object pItem, sequence pSource, sequence pTarget)   Returns the corresponding element.
  • func set_whitespace(sequence pChars)   Defines what is "whitespace"
  • func split(sequence pSource, object pDelim)   Returns the undelimited substrings
  • func trim(sequence pSource)    Removes any whitespace chars from both ends of pSource
  • func trim_all(sequence pSource)    Removes all whitespace chars from pSource
  • func trim_all_chars(sequence pSource, sequence pChars)   Removes all the matching characters.
  • func trim_chars(sequence pSource, sequence pChars)    Removes any pChars from both ends of pSource
  • func trim_left(sequence pSource)    Removes any whitespace chars from the left of pSource
  • func trim_left_chars(sequence pSource, sequence pChars)   Removes the leftmost set of characters
  • func trim_right(sequence pSource)    Removes any whitespace chars from the right of pSource
  • func trim_right_chars(sequence pSource, sequence pChars)   Removes the rightmost set of characters


    Table of Contents

    [func]
    compress
    (sequence pSource, integer pRep)

    Replaces embedded whitespace with pRep

    Returns: The original sequence with the matching characters replaced.

    Category: String Handling

    pSource is the sequence from which to replace characters.
    pRep is the delimiter character or string. The pSource string is first trimmed of any whitespace, then sets of one or more whitespace characters are replaced by a single pRep character.

    Example:

      sequence result
      result = compress("\t\tif  abc  = def  then xyz()  \t", ' ')
      ? result  -- Should display "if abc = def then xyz()"
    

    See Also: compress_chars, lookup, set_whitespace, split, trim, trim_all, trim_all_chars, trim_chars, trim_left, trim_left_chars, trim_right, trim_right_chars



    Table of Contents

    [func]
    compress_chars
    (sequence pSource, sequence pChars, integer pRep)

    Replaces embedded sequences of pChars with pRep

    Returns: The original sequence with the matching characters replaced.

    Category: String Handling

    pSource is the sequence from which to replace characters.
    pChars is the sequence containing which characters can be replaced.
    pRep is the replacement character. The pSource string is first trimmed of any pChars, then sets of one or more pChars is replaced by a single pRep character.

    Example:

      sequence result
      result = compress_chars("\t\tif  abc  = def  then xyz()  \t", " \t", ',')
      ? result  -- Should display "if,abc,=,def,then,xyz()"
    

    See Also: compress, lookup, set_whitespace, split, trim, trim_all, trim_all_chars, trim_chars, trim_left, trim_left_chars, trim_right, trim_right_chars



    Table of Contents

    [func]
    lookup
    (object pItem, sequence pSource, sequence pTarget)

    Returns the corresponding element.

    Returns: OBJECT: The corresponding element or 0/{} if not found.

    Category: String Handling

    This searches pSource for pItem and if found, it returns the corresponding element from pTarget.
    If pItem is not found in pSource, then what is returned depends on a few things.
    If pTarget is longer than pSource then the last element in pTarget is returned. This implements a way to return a default value of your choosing.
    If pTarget is not longer than pSource then if the first element of pTarget is an atom then zero is returned otherwise an empty sequence is returned.

    Examples:

           x = lookup('a', "cat", "dog") --> 'o'
           x = lookup('d', "cat", "dogx") --> 'x'
           x = lookup('d', "cat", "dog") --> 0
           x = lookup("ape", {"ant","bear","cat"}, {"spider","seal","dog"}) --> ""
           x = lookup("ant", {"ant","bear","cat"}, {"spider","seal","dog"}) --> "spider"
    

    See Also: compress, compress_chars, set_whitespace, split, trim, trim_all, trim_all_chars, trim_chars, trim_left, trim_left_chars, trim_right, trim_right_chars



    Table of Contents

    [func]
    set_whitespace
    (sequence pChars)

    Defines what is "whitespace"

    Returns: The current whitespace set of characters.

    Category: String Handling

    pChars is the new set of whitespace characters.
    Initially, the set of whitespace characters is
    Space, Tab, LineFeed, Carriage Return, Form Feed and Vertical Tab
    These correspond to ASCII 32, 9, 10, 13, 12, 11

    Note: If pChars is an empty sequence, the current set will not be changed. It will still be returned though.

    Example:

           sequence oldWS, result
           oldWS = set_whitespace(",.()[]{}")
           result = trim_all("123 Smith St., Collingwood, 3728, (VIC).")
           ? result -- Should display "123 Smith St Collingwood 3728 VIC"
           oldWS = set_whitespace(oldWS)
    

    See Also: compress, compress_chars, lookup, split, trim, trim_all, trim_all_chars, trim_chars, trim_left, trim_left_chars, trim_right, trim_right_chars



    Table of Contents

    [func]
    split
    (sequence pSource, object pDelim)

    Returns the undelimited substrings

    Returns: SEQUENCE: A set of sequences, one per delimited substring in pSource

    Category: String Handling

    pSource is the sequence from which to extract the substrings.
    pDelim is the delimiter character, delimiter string, or a set of single character delimiters.

    Note 1 - if pDelim is an atom and not equal to '{' then sections of text enclosed in matching '{' and '}' are not scanned.
    Note 2 - if pDelim is a single string inside a sequence, then it is considered to be a set of delimiter characters and any one of them can delimiter the pSource. The results in this case are that each delimitered text is followed by the sequence containing the character that caused the delimitation. A final empty sequence means that no delimiter was found to end the pSource text.

    Example:

      sequence result
      result = split("if abc = def then xyz()", ' ')
      ? result  -- Should be {"if","abc","=","def","then","xyz()"}
    

    -- Example of embedded delimiter... result = split("event=Click,font={Courier,12}", ',') ? result -- Should be {"event=Click","font={Courier,12}"}

    -- Example of delimiter set... result = split("event=Click,font={Courier,12}", {"=,{}"}) ? result -- Should be {"event","=","Click",",","font","=","","{","Courier",",","12","}"}

    ----------------------------------------------------

    See Also: compress, compress_chars, lookup, set_whitespace, trim, trim_all, trim_all_chars, trim_chars, trim_left, trim_left_chars, trim_right, trim_right_chars



    Table of Contents

    [func]
    trim
    (sequence pSource)

    Removes any whitespace chars from both ends of pSource

    Returns: The original sequence with the matching characters removed.

    Category: String Handling

    pSource is the sequence from which to remove whitespace.
    Example:

           sequence result
           result = trim("  abc def  ")
           ? result  -- Should display "abc def"
    

    See Also: compress, compress_chars, lookup, set_whitespace, split, trim_all, trim_all_chars, trim_chars, trim_left, trim_left_chars, trim_right, trim_right_chars



    Table of Contents

    [func]
    trim_all
    (sequence pSource)

    Removes all whitespace chars from pSource

    Returns: The original sequence with the matching characters removed.

    Category: String Handling

    pSource is the sequence from which to remove whitespace.
    Example:

           sequence result
           result = trim_all("  abc def  ")
           ? result  -- Should display "abcdef"
    

    See Also: compress, compress_chars, lookup, set_whitespace, split, trim, trim_all_chars, trim_chars, trim_left, trim_left_chars, trim_right, trim_right_chars



    Table of Contents

    [func]
    trim_all_chars
    (sequence pSource, sequence pChars)

    Removes all the matching characters.

    Returns: The original sequence with the matching characters removed.

    Category: String Handling

    pSource is the sequence from which to remove characters.
    pChars is the sequence containing which characters can be removed.
    Example:

           sequence result
           result = trim_all_chars("321abc9def123", "1234567890")
           ? result  -- Should display "abcdef"
    

    See Also: compress, compress_chars, lookup, set_whitespace, split, trim, trim_all, trim_chars, trim_left, trim_left_chars, trim_right, trim_right_chars



    Table of Contents

    [func]
    trim_chars
    (sequence pSource, sequence pChars)

    Removes any pChars from both ends of pSource

    Returns: The original sequence with the matching characters removed.

    Category: String Handling

    pSource is the sequence from which to remove whitespace.
    Example:

           sequence result
           result = trim("123abc9def321", "3459")
           ? result  -- Should display "12abcdef21"
    

    See Also: compress, compress_chars, lookup, set_whitespace, split, trim, trim_all, trim_all_chars, trim_left, trim_left_chars, trim_right, trim_right_chars



    Table of Contents

    [func]
    trim_left
    (sequence pSource)

    Removes any whitespace chars from the left of pSource

    Returns: The original sequence with the matching characters removed.

    Category: String Handling

    pSource is the sequence from which to remove whitespace.
    Example:

           sequence result
           result = trim_left("  abc def  ")
           ? result  -- Should display "abc def  "
    
    ----------------------------------------------------

    See Also: compress, compress_chars, lookup, set_whitespace, split, trim, trim_all, trim_all_chars, trim_chars, trim_left_chars, trim_right, trim_right_chars



    Table of Contents

    [func]
    trim_left_chars
    (sequence pSource, sequence pChars)

    Removes the leftmost set of characters

    Returns: The original sequence with the matching characters removed.

    Category: String Handling

    pSource is the sequence from which to remove characters.
    pChars is the sequence containing which characters can be removed.
    Example:

           sequence result
           result = trim_left_chars("321abc9def123", "1234567890")
           ? result  -- Should display "abc9def123"
    

    See Also: compress, compress_chars, lookup, set_whitespace, split, trim, trim_all, trim_all_chars, trim_chars, trim_left, trim_right, trim_right_chars



    Table of Contents

    [func]
    trim_right
    (sequence pSource)

    Removes any whitespace chars from the right of pSource

    Returns: The original sequence with the matching characters removed.

    Category: String Handling

    pSource is the sequence from which to remove whitespace.
    Example:

           sequence result
           result = trim_right("  abc def  ")
           ? result  -- Should display "  abc def"
    

    See Also: compress, compress_chars, lookup, set_whitespace, split, trim, trim_all, trim_all_chars, trim_chars, trim_left, trim_left_chars, trim_right_chars



    Table of Contents

    [func]
    trim_right_chars
    (sequence pSource, sequence pChars)

    Removes the rightmost set of characters

    Returns: The original sequence with the matching characters removed.

    Category: String Handling

    pSource is the sequence from which to remove characters.
    pChars is the sequence containing which characters can be removed.
    Example:

           sequence result
           result = trim_left_chars("321abc9def123", "1234567890")
           ? result  -- Should display "321abc9def"
    

    See Also: compress, compress_chars, lookup, set_whitespace, split, trim, trim_all, trim_all_chars, trim_chars, trim_left, trim_left_chars, trim_right