AX.lang.String extends native String java class with some useful methods. You can also get native Java class String by invoking getObject(). All methods supported by java String object are available thru this interface.

1 The String class

You can generate Java Strings by using the String constructor.

Copy
<script>
    var s = new Ax.lang.String("SAMPLE TEXT FOR STRING");
    if (s.getObject().matches(".*PLE.*FOR.*")) {
        console.log("ORIGINAL TEXT CONTAINS PLE AND FOR");
    }
    
    var t = s.getObject().substring(0, 6);
    console.log("SUBSTR: " + t);
    
    if (t.endsWith("PLE")) {
        console.log("SUBSTR TEXT ENDS WITH 'PLE'");
    }
</script>
ORIGINAL TEXT CONTAINS PLE AND FOR
SUBSTR: SAMPLE
SUBSTR TEXT ENDS WITH 'PLE'

2 Encoding conversion

Being able to access to native Java String Class, allows to use in JavaScript lots of functions available for java strings. As example, we expose method for string encoding conversion:

Copy
<script>
    var s = new Ax.lang.String("ÑÑ");
    var bytes = s.getBytes("UTF-8");

    console.log(new Ax.lang.String(bytes, "ISO-8859-1"));
    console.log(new Ax.lang.String(bytes));
</script>
ÃÃ
ÑÑ

3 lpad

Even the String class in Java doesn’t provide a convenient method for left padding it's simple to do it using its static format method.

Copy
<script>
    var length = 12; 
    var source = new Ax.lang.String('hello'); 
    var target = source.lpad('0', length);
    console.log(source);
    console.log(target);
</script>
hello
0000000hello

4 rpad

For right padding is very similar. The only change is the netagive prefix on number.

Copy
<script>
    var length = 12; 
    var source = new Ax.lang.String('hello'); 
    var target = source.rpad('0', length);
    console.log(source);
    console.log(target);
</script>
hello
hello0000000

5 trim

This method allows to trim spaces on both the left and right side of string.

Copy
<script>
    var source = new Ax.lang.String('  hello  '); 

    console.log("[" + source.trim() + "]");
</script>
[hello]

6 ltrim

This method allows to trim spaces only on the left of string.

Copy
<script>
    var source = new Ax.lang.String('  hello  '); 

    console.log("[" + source.ltrim() + "]");
</script>
[hello  ]

7 rtrim

This method allows to trim spaces only on the right of string.

Copy
<script>
    var source = new Ax.lang.String('  hello  '); 

    console.log("[" + source.rtrim() + "]");
</script>
[  hello]

8 isEmpty

Returns true if, and only if, length of string is 0.

Copy
<script>
    var source1 = new Ax.lang.String('  '); 
    var source2 = new Ax.lang.String(''); 

    console.log("[" + source1.isEmpty() + "]");
    console.log("[" + source2.isEmpty() + "]");
</script>
[false]
[true]

9 isBlank

Returns true if length of string is 0 or string only contains blanks.

Copy
<script>
    var source1 = new Ax.lang.String('  '); 
    var source2 = new Ax.lang.String(''); 

    console.log("[" + source1.isBlank() + "]");
    console.log("[" + source2.isBlank() + "]");
</script>
[true]
[true]

10 format

The most common way of formatting a string in java is using String.format(). If there were a “java sprintf” then this would be it.

10.1 Using static method

The normal way to access format is by using the static method of the String class.

Copy
<script>
    var s = Ax.lang.String.format("%s = %d", "joe", 35);
    console.log(s);
</script>
joe = 35

10.2 Using instance method

This method uses an instance of String, the calls the format with the arguments.

Copy
<script>
    var s = new Ax.lang.String('%s = %d').format('joe', 35);
    console.log(s);
</script>
joe = 35

11 mask

Mask a string with the provided mask

Copy
<script>
    var masked  = new Ax.lang.String("         123456").mask("######.########");
    console.log(masked);
</script>
######.##123456

12 replace

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".

Copy
<script>
    console.log(new Ax.lang.String('ABA').replace('B', 'A'));
</script>
AAA

13 replaceAll

Replaces each substring of this string that matches the given regular expression with the given replacement.

Copy
<script>
    console.log(new Ax.lang.String('AA11AA').replaceAll('[1-9]', 'A'));
</script>
AAAAAA

14 wrap

Wraps a single line of text, identifying words by white space up to the limit.

Copy
<script>
    var str = new Ax.lang.String("This is a sentence that we're using to test the wrap method");
    console.log("");
    console.log("--- 10 ---");
    console.log(str.wrap(10));
    console.log("");
    console.log("--- 20 ---");
    console.log(str.wrap(20));
    console.log("");
    console.log("--- 30 ---");
    console.log(str.wrap(30));		
</script>
--- 10 ---
This is a
sentence
that we're
using to
test the
wrap
method

--- 20 ---
This is a sentence
that we're using to
test the wrap method

--- 30 ---
This is a sentence that we're
using to test the wrap method

15 encodeXML

This method allows to encode the string for XML processing.

Copy
<script>
    var source = new Ax.lang.String('1 > 2'); 

    console.log("[" + source.encodeXML() + "]");
</script>
[1&amp;2]