All the Java objects available as standard in the Java Virtual Machine can be accessed in a server-side script. It is outside the scope of this document to cover the hundreds of Java objects available. It is recommended that you obtain a Java reference book to see what is available. To give some idea of how Java objects are used the Java String object will be covered in some detail as an example.

The Java String Object

Using the ‘new’ operator creates a ‘Java String’ object. e.g.

var myJavaString = new java.lang.String(string);
if (myJavaString.startsWith(“help")) {
write(“found it");
}

This example creates a Java String object called ‘myJavaString’ with a string argument. The example then goes on to use the ‘startsWith’ method to find out if the string starts with the word ‘help’. The first thing to note is that the ‘new’ operator needs the full package name of the java object, in this case java.lang.String. All Java objects must be created using their full package name, and the package names will be found in a Java reference book.

It is important to realise that a Java String object is not the same as a string object. They might be both derived from the same characters but string object is the literal characters while the Java String object is an object holding the literal characters and a whole host of other things.

If you try to compare a string object with a Java String object (derived from the same data) then it will fail. To make the comparison you must use the Java String ‘toString’ method, which returns the string data. e.g.

var myString = “ABCDEFG";
var myJavaString = new java.lang.String(“ABCDEFG");
if (myString == myJavaString) {
 
write(“never gets here");
}
if (myString == myJavaString.toString()) {
 
write(“always gets here");
}

In the example above the first if statement will be false as the variable ‘myString’ does not equal the object ‘myJavaString’. The second if statement will be true as the object ‘myString’ and the value returned by the Java String ‘toString’ method are the same.

Shown below are some of the more useful methods available in the Java String object. Refer to a Java reference book for a full list of methods available.

Java String Functions

Method

Description

charAt(numeric)

Returns the character at the specified index.

endsWith(String)

Tests if this string ends with the specified suffix.

equals(Object)

Compares this string to the specified object.

equalsIgnoreCase(String)

Compares this String to another object.

indexOf(String)

Returns the index within this string of the first occurrence of the specified substring.

indexOf(String, numeric)

Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

lastIndexOf(String)

Returns the index within this string of the rightmost occurrence of the specified substring.

lastIndexOf(String, numeric)

Returns the index within this string of the last occurrence of the specified substring.

length()

Returns the length of this string.

regionMatches(boolean, numeric, String, numeric, numeric)

Tests if two string regions are equal.

regionMatches(numeric, String, numeric, numeric)

Tests if two string regions are equal.

replace(numeric, numeric)

Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar. Both characters are defined as the numeric ASCII value.

startsWith(String)

Tests if this string starts with the specified prefix.

startsWith(String, numeric)

Tests if this string starts with the specified prefix.

substring(numeric)

Returns a new string that is a substring of this string.

substring(numeric, numeric)

Returns a new string that is a substring of this string.

toLowerCase()

Converts this String to lowercase.

toString()

This object (which is already a string!) is itself returned.

toUpperCase()

Converts this string to uppercase.

trim()

Removes white space from both ends of this string.

Comment on this topic

Topic ID: 150058