string - ..... This returns true if they contain the same...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
The String class In Java, a string is an object of the String class defined in java.lang package. A string can be instantiated as follows: String string1 = new String("this is a string"); Or String string1 = "this is a string"; The complete list of methods can be found in Java API specification. To know the length of a given string, we can use the method length: string1.length(); To obtain the character at the specified index of a given string: string1.charAt(index); where index is an integer. To see if this string contains the same characters as another string, string2, equals method is used: if (string1.equals(string2))
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ..... This returns true if they contain the same characters and false otherwise. To compare this string with another string, string2, compareTo method can be used: if (string1.compareTo(string2) < 0) .... This method returns a negative integer if string1 is lexically before, it returns zero if they are equal, and it returns a positive number if string1 is lexically after string2. To extract a substring of this string starting from index offset and extending through endIndex-1: string1.substring(offset, endIndex); To remove leading and trailing white space in a string, we can use trim() method: string1 = string1.trim();...
View Full Document

Ask a homework question - tutors are online