Class SubstringFunction

  • All Implemented Interfaces:
    Function

    public class SubstringFunction
    extends java.lang.Object
    implements Function

    4.2 string substring(string,number,number?)

    The substring function returns the substring of the first argument starting at the position specified in the second argument with length specified in the third argument. For example, substring("12345",2,3) returns "234". If the third argument is not specified, it returns the substring starting at the position specified in the second argument and continuing to the end of the string. For example, substring("12345",2) returns "2345".

    More precisely, each character in the string (see [3.6 Strings]) is considered to have a numeric position: the position of the first character is 1, the position of the second character is 2 and so on.

    NOTE: This differs from Java and ECMAScript, in which the String.substring method treats the position of the first character as 0.

    The returned substring contains those characters for which the position of the character is greater than or equal to the rounded value of the second argument and, if the third argument is specified, less than the sum of the rounded value of the second argument and the rounded value of the third argument; the comparisons and addition used for the above follow the standard IEEE 754 rules; rounding is done as if by a call to the round function. The following examples illustrate various unusual cases:

    • substring("12345", 1.5, 2.6) returns "234"

    • substring("12345", 0, 3) returns "12"

    • substring("12345", 0 div 0, 3) returns ""

    • . substring("12345", 1, 0 div 0) returns ""

    • substring("12345", -42, 1 div 0) returns "12345"

    substring("12345", -1 div 0, 1 div 0) returns ""
    Author:
    bob mcwhirter (bob @ werken.com)
    See Also:
    Section 4.2 of the XPath Specification
    • Constructor Summary

      Constructors 
      Constructor Description
      SubstringFunction()
      Create a new SubstringFunction object.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object call​(Context context, java.util.List args)
      Returns a substring of an XPath string-value by character index.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SubstringFunction

        public SubstringFunction()
        Create a new SubstringFunction object.
    • Method Detail

      • call

        public java.lang.Object call​(Context context,
                                     java.util.List args)
                              throws FunctionCallException
        Returns a substring of an XPath string-value by character index.
        Specified by:
        call in interface Function
        Parameters:
        context - the context at the point in the expression when the function is called
        args - a list that contains two or three items
        Returns:
        a String containing the specifed character subsequence of the original string or the string-value of the context node
        Throws:
        FunctionCallException - if args has more than three or less than two items