Obtains the substring of a string or text, from the initial position until the indicated final position (or end of the text if not indicated) of the string or text.
 

1 string.substring

<string.substring>
    <string /> !
    <start /> !
    <end /> ?
</string.substring>

Exceptions

java.lang.String index out of range: ...

One of the indicated positions is out of range, it is negative or greater to the length of the text.

requires 3 arguments, received: ...

The 3 entry parameters has not been specified.

invalid number ...

One of the indicated positions is not a number.

Remarks

This function should include three parameters. The first should be the variable which contains the original string. The second parameter indicates the initial position, from which the substring is obtained. The third should be the final position in which the selection of the substring ends, excluding this last position. The use of this position can generate exceptions if the index which marks the beginning and end of the substring interval are not treated well. In the case that the beginning of substring is lower than 0 or greater than the length of the string, or the end of the substring is greater that yhr length of the data, this will produce the following exception:

Copy
java.lang.StringIndexOutOfBoundsException: String index out of
                range: [NUMBER]

In this case, you should check that the index are inside of the allowed range. A fail that can happen is to indicate a final number of string, greater than the length of tha data. This can be corrected using the function <string.length> to determine the length of tha data and like this the final of the substring or as protection.

Example

Obtains a substring.

Copy
<xsql-script name='string_substring1'>
    <body>
        <set name='a'>123456789</set>
        <set name='b'>
            <string.substring>
                <a/><number>1</number><number>5</number>
            </string.substring>
        </set>
        <println><b/></println>
    </body>
</xsql-script>

Returns:

Copy
2345
Example

Error trying to get a substring.

Copy
<xsql-script name='string_substring2'>
   <body>
       <set name='a'>123456789</set>
       <set name='b'>
           <string.substring>
               <a/><number>1</number><number>20</number>
           </string.substring>
       </set>
       <println><b/></println>
   </body>
</xsql-script>

The end of the substring is gretaer to the length of the data and for this the following exception is produced:

Copy
java.lang.StringIndexOutOfBoundsException: String
                        index out of range: [NUMBER]
Example

Obtains a substring through the position 3 until the end of the string.

Copy
<xsql-script name='string_substring3'>
   <body>
       <set name='a'>123456789</set>
       <set name='b'>
           <string.substring>
               <a/>
               <number>3</number>
           </string.substring>
       </set>
       <println><b/></println>
   </body>
</xsql-script>

Returns:

Copy
456789
 

1.1 Diferences with SUBSTR of Databases

The function <substr> of XSQL-Script does not work in the same way that the function SUBSTR of databases. <substr> of XSQL-Script:

Copy
<string.substring><field/><number>start</number><number>end</number>]
  • field: field name.
  • start: number which indicates the first character from which you start counting, being the first position the 0.
  • end: number which indicates the last character to be counted, excluding this last position. SUBSTR od databases: This function receives three parameters.
Copy
SUBSTR(field, start, length)
  • field: field name.
  • start: number which indicates the first character from which you start counting, being the first position the 1.
  • length: number of characters that you should select counting the character indicated by the start position.

The following select shows some example of use of the function SUBSTR of databases and below the equivalence with the use of the function <substr> of XSQL-Script.

Copy
select nomdia, SUBSTR(nomdia, 9, 3), SUBSTR(nomdia, 10, 2), SUBSTR(nomdia, 8, 20)
  from cdiary

    namday                          substr(nomdia,9,3)      substr(nomdia,10,2)     substr(nomdia,8,20)
1   GENERAL DIARY                   ENE                     NE                      GENERAL
2   LOGISTICS DIARY                 E L                      L                      LOGISTICS
3   CASH DIARY                      E C                      C                      CASH
4   AAA
5   AMORTIZATION DIARY              E A                      A                      AMORTIZATIONS
6   PURCHASES DIARY                 E C                      C                      PURCHASSES
7   SALES DIARY                     E V                      V                      SALES

Equivalence in <substr> of XSQL-Script:

Copy
<set name='nomdia'>GENERAL DIARY</set>
<string.substring><namday/><number>8</number><number>11</number></string.substring>   <!-- substr(nomdia,8,11)='ENE'  -->
<string.substring><namday/><number>9</number><number>11</number></string.substring>   <!-- substr(nomdia,9,11)='NE'   -->

<string.substring><nomdia/><number>7</number><number>19</number></string.substring>

> java.lang.StringIndexOutOfBoundsException: String index out of range: 19

<string.substring><namday/><number>7</number><string.length><namday /></string.length></string.substring>
<!-- substr(nomdia,7,namday.length())='GENERAL' -->

info

The function <string.substring> can generate an exception java.lang.StringIndexOutOfBoundsException: String index out of range: [NUMBER] if the indicated index for the beginning and the end of substring will be out of the allowed range, so the beginning is lower to 0 or the end is greater than the length of the string.