1 Definition
A server side script
string is zero or more characters written inside quotes.
1.1 Simple string
In XSQL you can set a text into a variable using the tag <string>.
In server side script
this is unnecessary. Only needs to use quotes (simple or double)
<xsql-script> <body> <set name='m_result'> <string trim='true'> THIS IS A STRING </string> </set> <println><m_result /></println> </body> </xsql-script>
THIS IS A STRING
var mStrSample = 'THIS IS A STRING WITH SINGLE QUOTES'; return mStrSample;
THIS IS A STRING WITH SINGLE QUOTES
In server side script
you can indistinctly use a single quote, a double quote or a grave accent to quote a string.
<xsql-script> <body> <println> <string trim='true'> This is a string </string> </println> </body> </xsql-script>
This is a string
var mStrSngQuote = 'This is a string with single quotes'; var mStrDblQuote = "This is a string with double quotes"; var mStrGrvAccent = `This is a string with grave accent`; console.log(mStrSngQuote); console.log(mStrDblQuote); console.log(mStrGrvAccent);
This is a string with single quotes
This is a string with double quotes
This is a string with grave accent
1.2 Escape sequences
In some cases strin must be written within quotes. In server side script
this problem is solved using the backslash escape character.
<xsql-script> <body> <println> <string trim='true'> THE NAME OF THE PROGRAM WAS "OVERLOAD" </string> </println> <println> <string trim='true'> THE FILE DIRECTION IS home\carpet\subcarpet </string> </println> </body> </xsql-script>
THE NAME OF THE PROGRAM WAS "OVERLOAD"
THE FILE DIRECTION IS carpet\subcarpet
var mStrText1 = "THE NAME OF THE PROGRAM WAS \"OVERLOAD\""; console.log(mStrText1); var mStrText2 = "THE FILE DIRECTION IS carpet\\subcarpet"; console.log(mStrText2);
THE NAME OF THE PROGRAM WAS "OVERLOAD"
THE FILE DIRECTION IS carpet\subcarpet
Exists other escape sequences, which are described below
Operator XSQL | Equivalent server side script
|
Description |
---|---|---|
<string.nl> | \n | New Line |
<string.ht> | \t | Horizontal tabulator |
Example of adding a new line :
<xsql-script> <body> <println> <string trim='true'> THE DIRECTION IS:<string.nl/>Calle Los Fresnos 301 </string> </println> </body> </xsql-script>
THE DIRECTION IS:
Calle Los Fresnos 301
var mStrText = "THE DIRECTION IS:\nCalle Los Fresnos 301"; console.log(mStrText);
THE DIRECTION IS:
Calle Los Fresnos 301
Example of adding an horizontal tabulator:
<xsql-script> <body> <println> <string trim='true'> THE EMAIL IS:<string.ht/>info@test.com </string> </println> </body> </xsql-script>
THE EMAIL IS: info@test.com
var mStrText = "THE EMAIL IS:\tinfo@test.com"; console.log(mStrText);
THE EMAIL IS: info@test.com
1.3 Breaking long code lines
To maintain the readability of the code, it is preferable to avoid lines of code longer than 80 characters. To do this, it is necessary to breaking long code lines.
<xsql-script> <body> <println> <string trim='true'> The 50 yen coin is a denomination of Japanese yen. These coins were first minted in 1955. </string> </println> </body> </xsql-script>
The 50 yen coin is a denomination of Japanese yen. These coins were first minted in 1955.
var mStrText1 = "The 50 yen coin is a denomination of Japanese yen. \ These coins were first minted in 1955."; console.log(mStrText1);
The 50 yen coin is a denomination of Japanese yen. These coins were first minted in 1955.
1.4 Concatenate string
XSQL allows to join two or more strings inside the tag <string>.
In server side script
is possible to concatenate text using the plus symbol (+) between strings.
<xsql-script> <body> <println> <string> <string>THIS IS A STRING </string><string>CONCATENATE WITH ANOTHER STRING</string> </string> </println> </body> </xsql-script>
THIS IS A STRING CONCATENATE WITH ANOTHER STRING
var mStrText = 'A CONCATENATED STRING'; var mStrConcat = 'THIS IS ANOTHER ' + 'EXAMPLE OF ' + mStrText ; console.log(mStrConcat);
THIS IS ANOTHER EXAMPLE OF A CONCATENATED STRING
1.5 String with variables
One or more variables can concatenate in a string.
In SQL this can do in the scope of tag string, meanwhile in server side script
must be quoted using grave accent (``),
and to call the variable, use the symbol "$" followed by the braces and the name of the variable
<xsql-script> <body> <set name='m_text1'> <string trim='true'> THIS IS A STRING </string> </set> <set name='m_text2'> <string trim='true'> WITH ANOTHER STRING </string> </set> <println><string><m_text1 /> CONCATENATE <m_text2 /></string></println> </body> </xsql-script>
THIS IS A STRING CONCATENATE WITH ANOTHER STRING
var mStrString1 = 'THIS IS A STRING'; var mStrString2 = 'WITH ANOTHER STRING'; var mStrConcat = `${mStrString1} CONCATENATE ${mStrString2}`; console.log(mStrConcat);
THIS IS A STRING CONCATENATE WITH ANOTHER STRING
1.6 String with numberic variables
In both XSQL and server side script
it is possible to join a numeric variable to a string, obtaining another
string as a result.
In server side script
it must be quoted using grave accent (``), and to call the variable,
use the symbol "$" followed by the braces and the name of the variable
<xsql-script> <body> <set name='m_number' type='integer'>15</set> <set name='m_result'><string>THE RESULT IS <m_number /></string></set> <println><m_result /></println> </body> </xsql-script>
THE RESULT IS 15
var mIntNumber = 5; var mStrString = `THE RESULT IS ${mIntNumber}`; console.log(mStrString);
THE RESULT IS 5
1.7 String with a date
Similarly to the previous example, in XSQL and server side script
is possible join a date
type variable to a string, obtaining another string as a result.
In server side script
it is also possible define different formats to display the date.
<xsql-script> <body> <set name='m_fecha' type='date'>4-7-2020</set> <set name='m_result'><string>THE DATE IS <m_fecha /></string></set> <println><m_result /></println> </body> </xsql-script>
THE DATE IS 04-07-2020
var mStrFecha = new Ax.util.Date(2020,7,4).toISOString(); console.log(`THE DATE IS ${mStrFecha}`);
THE DATE IS 2020-07-04
1.8 String with an expression
While in XSQL can assemble an string with an expression inside, in server side script
can use a ternary
operator or another conditional statement to assemble a string.
The following example shows the use of the server side script
ternary operator.
<xsql-script> <body> <set name='m_exists'><true /></set> <set name='m_result'> <add> <string>THIS IS A STRING </string> <ifthen> <expr><m_exists /></expr> <string>WITH AN EXISTING EXPRESSION</string> <string>WITH A FAIL</string> </ifthen> </add> </set> <println><m_result /></println> </body> </xsql-script>
THIS IS A STRING WITH AN EXISTING EXPRESSION
var mBoolExists = true; var mStrResult = 'THIS IS A STRING ' ; mStrResult += mBoolExists ? 'WITH AN EXISTING EXPRESSION' : 'WITH A FAIL'; console.log(mStrResult);
THIS IS A STRING WITH AN EXISTING EXPRESSION
2 String methods
Methods simplify work with strings.
2.1 The length method
Both XSQL and server side script
provide methods that returns the length of a string (number of characters).
<xsql-script> <body> <set name='m_txt'>This is a string of 33 characters</set> <println><string.length><m_txt /></string.length></println> </body> </xsql-script>
33
var mStrTxt = 'This is a string of 33 characters'; var mIntLength = mStrTxt.length; console.log(mIntLength);
33
2.2 The concat method
Allows to join two or more strings
<xsql-script> <body> <set name='m_string1'>THIS IS A STRING OF CHARACTERS. </set> <set name='m_string2'>A TEST STRING.</set> <set name='m_result'> <add> <m_string1 /> <m_string2 /> </add> </set> <println><m_result /></println> </body> </xsql-script>
THIS IS A STRING OF CHARACTERS. A TEST STRING.
var mStrString = "THIS IS A STRING OF CHARACTERS. " var mStrString2 = "A TEST STRING."; var mStrResult = mStrString.concat(mStrString2); console.log(mStrResult);
THIS IS A STRING OF CHARACTERS. A TEST STRING.
2.3 The slice method
This method extracts a part of a string and returns the extracted part in a new string.
<xsql-script> <body> <set name='m_string'>THIS IS A STRING OF CHARACTERS. A TEST STRING.</set> <set name='m_result'> <string.substring> <m_string/><number>8</number><number>17</number> </string.substring> </set> <println><m_result /></println> </body> </xsql-script>
A STRING
let mStrString = "THIS IS A STRING OF CHARACTERS. A TEST STRING."; let mStrResult = mStrString.slice(8, 17); console.log(mStrResult);
A STRING
In the slice method, if a parameter is negative, the position is counted from the end of the string.
<xsql-script> <body> <set name='m_string'>THIS IS A STRING OF CHARACTERS. A TEST STRING.</set> <set name='m_result'> <string.substring> <m_string/> <add><string.length><m_string/></string.length><number>-38</number></add> <add><string.length><m_string /></string.length><number>-30</number></add> </string.substring> </set> <println><m_result /></println> </body> </xsql-script>
A STRING
var mStrString = "THIS IS A STRING OF CHARACTERS. A TEST STRING."; var mStrNegResult = mStrString.slice(-38, -30); console.log(mStrNegResult);
A STRING
2.4 The substring method
Like the slice method, extracts a part of a string and returns the extracted part in a new string.
Substring cannot accept negative indexes.
<xsql-script> <body> <set name='m_string'>THIS IS A STRING OF 33 CHARACTERS</set> <set name='m_result'> <string.substring> <m_string/><number>9</number><number>22</number> </string.substring> </set> <println><m_result /></println> </body> </xsql-script>
STRING OF 33
var mStrTxt = 'THIS IS A STRING OF 33 CHARACTERS'; var mStrResult = mStrTxt.substring(9, 22); console.log(mStrResult);
STRING OF 33
2.5 The substr method
Similar to slice and substring, with the difference that the second parameter specifies the length of the new string.
<xsql-script> <body> <set name='m_string'>THIS IS A STRING OF CHARACTERS. A TEST STRING.</set> <set name='m_result'> <string.substring> <m_string/> <number>8</number> <add><number>8</number><number>9</number></add> </string.substring> </set> <println><m_result /></println> </body> </xsql-script>
A STRING
var mStrString = "THIS IS A STRING OF CHARACTERS. A TEST STRING."; var mStrResult = mStrString.substr(8, 9); console.log(mStrResult);
A STRING
2.6 The split method
Divide this string around of the coincidences of the given regular expression.
<xsql-script> <body> <set name='arr'><string.split pattern=''>DOCUMENT</string.split></set> <println><arr /></println> </body> </xsql-script>
[D,O,C,U,M,E,N,T]
var mStrTxt = "DOCUMENT"; var mStrTxt2 = mStrTxt.split(""); console.log(mStrTxt2);
[D, O, C, U, M, E, N, T]
2.7 The replace method
This method in both languages allow returns a new string which results of replacing all the appearances of old char in this string with char.
<xsql-script> <body> <set name='m_text' type='string'>THIS IS A PURCHASE DOCUMENT</set> <set name='m_result'> <string.replace> <m_text /> <string>PURCHASE</string> <string>SALES</string> </string.replace> </set> <println><m_result /></println> </body> </xsql-script>
THIS IS A SALES DOCUMENT
var mStrText = "THIS IS A PURCHASE DOCUMENT"; var mStrResult = mStrText.replace("PURCHASE", "SALES"); console.log(mStrResult);
THIS IS A SALES DOCUMENT
2.8 To upper case method
With this method provided in XSQL and server side script
can convert a string to upper case.
<xsql-script> <body> <set name='m_text'>This is a string of characters.</set> <println><string.toUpperCase><m_text /></string.toUpperCase></println> </body> </xsql-script>
THIS IS A STRING OF CHARACTERS.
var mStrText = "This is a string of characters."; var mStrResult = mStrText.toUpperCase(); console.log(mStrResult);
THIS IS A STRING OF CHARACTERS.
2.9 To lower case method
Like the previous method, this one can convert a string into lower case.
<xsql-script> <body> <set name='m_text'>THIS IS A STRING OF CHARACTERS.</set> <println><string.toLowerCase><m_text /></string.toLowerCase></println> </body> </xsql-script>
this is a string of characters.
var mStrText = "This is a string of characters."; var mStrResult = mStrText.toLowerCase(); console.log(mStrResult);
this is a string of characters.
2.10 The trim method
With the trim method, can return a copy of the text with the initial and final spaces omitted.
<xsql-script> <body> <set name='m_text'> TYPE CODE </set> <set name='m_result'><string>[<string.trim><m_text /></string.trim>]</string></set> <println><m_result /></println> </body> </xsql-script>
[TYPE CODE]
let mStrText = " TYPE CODE "; let mStrResult = '[' + mStrText.trim() + ']'; console.log(mStrResult);
[TYPE CODE]
2.11 The lpad method
The lpad method allows to return the text complete by the left with the fill value until obtain a text of
the indicated length.
In server side script
, the String java class provide a convenient method for left padding. In this case
it is neccesary to use its static format method.
<xsql-script> <body> <set name='m_text' type='string'>10</set> <set name='m_size'>6</set> <set name='m_fill'>0</set> <set name='m_result'><string.lpad><m_text/><m_size/><m_fill/></string.lpad></set> <println><m_result /></println> </body> </xsql-script>
000010
var mIntSize = 6; var mStrText = new Ax.lang.String('10'); var mStrResult = mStrText.lpad('0', mIntSize); console.log(mStrResult);
000010
2.12 The rpad method
The rpad method allows to return the text complete by the right with the fill value until obtain a text of
the indicated length.
In server side script
, the String class in Java provides a convenient method for right padding. In this case
it is neccesary to use its static format method.
<xsql-script> <body> <set name='m_text' type='string'>COMPANY NAME</set> <set name='m_size'>15</set> <set name='m_fill'>*</set> <set name='m_result'><string.rpad><m_text/><m_size/><m_fill/></string.rpad></set> <println><m_result /></println> </body> </xsql-script>
COMPANY NAME***
var mIntSize = 15; var mStrText = new Ax.lang.String('COMPANY NAME'); var mStrResult = mStrText.rpad('*', mIntSize); console.log(mStrResult);
COMPANY NAME***
2.13 The charAt method
This method returns the character at a specified index (position) in a string:
<xsql-script> <body> <set name='m_text' type='string'>hello</set> <set name='m_result'><string.charAt><m_text />1</string.charAt></set> <println><m_result /></println> </body> </xsql-script>
e
var mStrText ='hello'; var mStrResult = mStrText.charAt(1); console.log(mStrResult);
e
2.14 The index-Of method
This method returns the position of the first coincidence in which the value of indicated argument is found. If the argument is not found in the string, the script will return -1 as result.
<xsql-script> <body> <set name='m_text'>THIS IS A STRING OF CHARACTERS.</set> <set name='m_scope'>STRING</set> <println><string.indexOf><m_text /><m_scope /></string.indexOf></println> </body> </xsql-script>
10
var mStrString = "THIS IS A STRING OF CHARACTERS."; var mIntPos = mStrString.indexOf("STRING"); console.log(mIntPos)
10
2.15 The last index-Of method
This method returns the position of the last coincidence in which the value of indicated argument is found. If the argument is not found in the string, the script will return -1 as result.
<xsql-script> <body> <set name='m_text'>THIS IS A STRING OF CHARACTERS. A TEST STRING.</set> <set name='m_scope'>STRING</set> <println><string.lastIndexOf><m_text /><m_scope /></string.lastIndexOf></println> </body> </xsql-script>
39
var mStrString = "THIS IS A STRING OF CHARACTERS. A TEST STRING."; var mIntPos = mStrString.lastIndexOf("STRING"); console.log(mIntPos);
39
3 Number Format
NumberFormat helps you to format and parse numbers for any locale. Your code can be completely independent of the locale conventions for decimal points, thousands-separators, or even the particular decimal digits used, or whether the number format is even decimal.
3.1 Without decimals
It is possible to generate from an informed number, a string whose format does not admit decimals.
<xsql-script name='number_format'> <body> <set name = 'first'> <number.format format='000000'> 12 </number.format> </set> <set name = 'second'> <number.format format='000000'> 11 </number.format> </set> <set name = 'numberResult'> <string><first/><second/></string> </set> <println><numberResult/></println> </body> </xsql-script>
000012000011
var mNf = new Ax.text.NumberFormat("us"); var mLine = new Ax.text.Line(12) .add( 0, mNf.format(12, "000000")) .add( 6, mNf.format(11, "000000")) .toString(); console.log(mLine);
000012000011
If a decimal number is reported, the "number format" function only takes the integer.
<xsql-script name='number_format'> <body> <set name = 'first'> <number.format format='000000'> 12.15 </number.format> </set> <set name = 'second'> <number.format format='000000'> 11.00 </number.format> </set> <set name = 'numberResult'> <string><first/><second/></string> </set> <println><numberResult/></println> </body> </xsql-script>
000012000011
var mNf = new Ax.text.NumberFormat("us"); var mLine = new Ax.text.Line(12) .add( 0, mNf.format(12.15, "000000")) .add( 6, mNf.format(11.00, "000000")) .toString(); console.log(mLine);
000012000011
3.2 With decimals
It is possible to generate from an informed number, a string whose format does admit decimals.
<xsql-script name='number_format'> <body> <set name = 'first'> <number.format format='000000' maximumfractiondigits='2' minimumfractiondigits='0'> 1234.123 </number.format> </set> <set name = 'second'> <number.format format='000000.0' maximumfractiondigits='0' minimumfractiondigits='2'> 1234.128 </number.format> </set> <set name = 'third'> <number.format format='000000.0000' maximumfractiondigits='4' minimumfractiondigits='4'> 1234.128 </number.format> </set> <println><string><first/></string></println> <println><string><second/></string></println> <println><string><third/></string></println> </body> </xsql-script>
001234.12
001234.13
001234.1280
var mNf = new Ax.text.NumberFormat("us"); console.log(mNf.format(1234.123, "000000.00")); console.log(mNf.format(1234.128, "000000.00")); console.log(mNf.format(1234.128, "000000.0000"));
001234.12
001234.13
001234.1280
If a number without decimals is reported, the "number format" function fills the decimals with zeros.
<xsql-script name='number_format'> <body> <set name = 'first'> <number.format format='000000' maximumfractiondigits='2' minimumfractiondigits='2'> 1234 </number.format> </set> <set name = 'second'> <number.format format='000000.0' maximumfractiondigits='0' minimumfractiondigits='3'> 1234 </number.format> </set> <set name = 'third'> <number.format format='000000.0000' maximumfractiondigits='4' minimumfractiondigits='4'> 1234.1 </number.format> </set> <println><string><first/></string></println> <println><string><second/></string></println> <println><string><third/></string></println> </body> </xsql-script>
001234.00
001234.000
001234.1000
var mNf = new Ax.text.NumberFormat("us"); console.log(mNf.format(1234, "000000.00")); console.log(mNf.format(1234, "000000.000")); console.log(mNf.format(1234.1, "000000.0000"));
001234.00
001234.000
001234.1000
3.3 Format US
In the United States, the comma (,) represents the thousands separator and the period (.), the decimal separator.
<xsql-script name='number_format_sample1'> <body> <set name='number'> <number.format format='###,###.00' maximumfractiondigits='2' minimumfractiondigits='2'> 12987410.12 </number.format> </set> <println>us:<number/></println> </body> </xsql-script>
us:12,987,410.12
var NumberFormatUs = new Ax.text.NumberFormat("us"); console.log("us:" + NumberFormatUs.format(12987410.12, "###,###.00"));
us:12,987,410.12
In the server-side script you can put more number formats (as in the following example: Spanish and French)
var NumberFormatEs = new Ax.text.NumberFormat("es"); console.log("es:" + NumberFormatEs.format(12987410.12, "###,###.00")); var NumberFormatFr = new Ax.text.NumberFormat("fr"); console.log("fr:" + NumberFormatFr.format(12987410.12, "###,###.00"));
es:12.987.410,12
fr:12 987 410,12
3.4 Number negatives
It is possible to generate from a reported number, a string whose format admits negative numbers.
<xsql-script name='number_format'> <body> <set name = 'first'> <number.format format='000000'> -12 </number.format> </set> <set name = 'second'> <number.format format='000000.0' maximumfractiondigits='0' minimumfractiondigits='2'> -1234.128 </number.format> </set> <set name='formatUS'> <number.format format='###,###.00' maximumfractiondigits='2' minimumfractiondigits='2'> -12987410.12 </number.format> </set> <println><first/></println> <println><second/></println> <println>us:<formatUS/></println> </body> </xsql-script>
-000012
-001234.13
us:12,987,410.12
var mNf = new Ax.text.NumberFormat("us"); var NumberFormatUs = new Ax.text.NumberFormat("us"); console.log(mNf.format(-12, "000000")); console.log(mNf.format(-1234.128, "000000.00")); console.log("us:" + NumberFormatUs.format(12987410.12, "###,###.00"));
-000012
-001234.13
us:12,987,410.12