An overview of Strings and String methods

There are simple variable types such as 

     char     ch = 'A';
     int      i = 0;
     double   pi = 3.14159;

and there are variables that have a class type, such as 

     String   sentence = "Java string example";
String variables have a number of methods available for accessing and processing their contents. Many of these methods are identified below. One of the more confusing things about Strings is that the length of a String is defined as the number of characters in the String, but the characters are identified by their positions starting at position 0.

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 1:
Sample Program
public static void main(String[] args) {

      String sentence = "Java string example";

      int    len = sentence.length(); 

      System.out.println("Example: sentence = " + sentence);
      System.out.println("The length of sentence is " + sentence.length());
      System.out.println();
      System.out.println("The last character of String sentence is at location " + (sentence.length()-1)); 
      System.out.println("The last character of String sentence is " + sentence.charAt(len - 1));
  }
                   
Program Output
Example: sentence = Java string example
The length of sentence is 19

The last character of String sentence is at location 18
The last character of String sentence is e
                   
Method Discussion
  • length() returns the length of the String, which is the number of characters in the string. The character postions start at 0 and continue to length() - 1.
  • charAt(n) returns the single character that is At postion n of the string.
    charAt(0) will return the first character in the non-empty string and
    charAt(length() - 1) will return the last character in the sting.
  • In the program example an integer variable len is used to contain the length of the String sentence. This helps to keep the program lines shorter and makes them easier to read.
  • printing the string "\n" adds a new line to the output which can be used instead of using System.out.println();

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.
Sample Program
Program code
    Java code:
    String sentence = "Java string example";

    int c = sentence.compareTo("a excellent example");
    System.out.println("c = " + c + "\n");

      Program Output:
      c = -23
    Java code:
    String newSentence = sentence.concat(" of concatentation!");
    System.out.println("newSentence = " + newSentence + "\n");
      Program Output:
      newSentence = Java string example of concatentation!
    Java code:
    boolean e = sentence.equals("JAVA STRING EXAMPLE");
    System.out.println("e = " + e + "\n");
      Program Output:
      e = false
    Java code:
    boolean eIC = sentence.equalsIgnoreCase("JAVE STRING EXAMPLE");
    System.out.println("EIC = " + eIC + "\n");
      Program Output:
      EIC = false
    Java code:
    int i = sentence.indexOf("ing");
    System.out.println("i = " + i+ "\n");
      Program Output:
      i = 8
    Java code:
    int lio = sentence.lastIndexOf("a");
    System.out.println("lio = " + lio + "\n");
      Program Output:
      lio = 14
    Java code:
    String lc = sentence.toLowerCase();
    System.out.println("lc = " + lc + "\n");
      Program Output:
      lc = java string example
    Java code:
    String uc = sentence.toUpperCase();
    System.out.println("uc = " + uc + "\n");
      Program Output:
      uc = JAVA STRING EXAMPLE
    Java code:
    String rs = sentence.replace("a","A");
    System.out.println("rs = " + rs + "\n");
      Program Output:
      rs = JAvA string exAmple