In the String sentence above the first character is J which begins at position 0.
The length of String sentence is 19. The position of the final 'e' is 18.
Example 2: |
Sample Program |
public static void main(String[] args) {
String sentence = " Java string example ";
String word = "";
int len = sentence.length();
System.out.println("Example: sentence = " + sentence);
System.out.println("The length of sentence is " + sentence.length());
System.out.println();
sentence = sentence.trim();
System.out.println("The trimmed sentence = " + sentence);
System.out.println("The length of the trimed sentence " + sentence.length() + "\n");
word = sentence.substring(12);
System.out.println("\nsubstring of sentence beginning at character 12 = " + word + "\n");
word = sentence.substring(8,11);
System.out.println("\nsubstring(8,11) of sentence " + sentence + " = " + word + "\n");
}
|
Program Output |
Example: sentence = Java string example
The length of sentence is 29
The trimmed sentence = Java string example
The length of the trimed sentence 19
substring of sentence beginning at character 12 = example
substring(8,11) of sentence Java string example = ing
|
Method Discussion |
- trim() remove all leading and trailing spaces. At the beginning of the program there are
5 leading spaces and 5 trailing spaces, then trim is called to deleted them.
- substring(start) returns a string that begins at the index specified by start and continues to the end of the string. In the trimmed sentence, the word example begins at character 12.
word = sentence.substring(12) copies the sentence beginning at character 12 and copies the rest of the string
and puts it into the variable word.
- substring(start,end) returns a string that begins at the index specified by start, and continue to the
character before end (but does not return the character at end
sentence.substring(8,11) returns the characters at positions 8,9, and 10 - which is ing in the string
"Java string example".
|
Addtional Examples |
int c = sentence.compareTo("a excellent example");
More compareTo examples
int comp = 0;
String s1 = "This is the fist string";
String s2 = "zzzzzzzzz"; // lower case zzzz
String s3 = "AAAAAAA";
String s4 = "ZZZZZZZZ"; // upper case Z
comp = s1.compareTo(s2);
System.out.println("Compare " + s1 + " to " + s2 + " results: " + comp);
comp = s2.compareTo(s1);
System.out.println("Compare " + s2 + " to " + s1 + " results: " + comp);
comp = s2.compareTo(s3);
System.out.println("Compare " + s2 + " to " + s3 + " results: " + comp);
comp = s3.compareTo(s4);
System.out.println("Compare " + s3 + " to " + s4 + " results: " + comp);
comp = s4.compareTo(s4);
System.out.println("Compare " + s4 + " to " + s4 + " results: " + comp);
Output:
Compare This is the fist string to zzzzzzzzz results: -38
Compare zzzzzzzzz to This is the fist string results: 38
Compare zzzzzzzzz to AAAAAAA results: 57
Compare AAAAAAA to ZZZZZZZZ results: -25
Compare ZZZZZZZZ to ZZZZZZZZ results: 0
|
Compares sentence with the string "a excellent example" to see which comes first alphabetically (lexicographic order). The compareTo method returns an integer:
- 0 : means the strings are the same
- < 0 : means that the string in the variable sentence is first
- > 0 : means that the string "a excellent example" would be first alphabetically
|
String newSentence = sentence.concat(" of concatenation!");
|
The variable newSentence gets a copy of the contents of sentence then the string " of concatenation!" is added to the end of the newSentence.
|
boolean e = sentence.equals("JAVA STRING EXAMPLE");
|
The variable e is assigned the value of false because the string "JAVA STRING EXAMPLE" is not equal to the string "Java string example"
|
boolean eIC = sentence.equalsIgnoreCase("JAVA STRING EXAMPLE");
|
The variable eIC is assigned the value of true because the string "JAVA STRING EXAMPLE" is equal to the string "Java string example" when upper and lower case is ignored.
|
int i = sentence.indexOf("ing");
|
The indexOf function will return the first occurance of the sting that is being searched for. In the variable sentence the string "ing" starts at postion 8.
|
int lio = sentence.lastIndexOf("a");
|
The integer variable lio will contain the index of sentence were the last occurance of the character a is
located. In these example this will be in position 14 in the word example.
|
String lc = sentence.toLowerCase();
|
The String lc will be a lower case version of the String sentence. The String sentence only has the first letter in upper case, so the only difference between these two Strings will be the first letter.
|
String uc = sentence.toUpperCase();
|
The string uc will be an upper case versions of the String sentence.
|
String rs = sentence.replace("aeiou","AEIOU");
|
The String rs will be a copy of the String sentence, but for every lower case vowel in sentence the corresponding vowel in rs will be in upper case.
|