String and Character Methods

  1. String Methods
     
    1. charAt
      public class MyCode { public static void main(String[] args) { System.out.println("abc".charAt(0)); System.out.println("abc".charAt(1)); System.out.println("abc".charAt(2)); System.out.println("abc".charAt(3)); // run-time error! } }

      Another Example:

      public class MyCode { public static void main(String[] args) { // The canonical loop-through-a-string String s = "Carpe diem!"; for (int i=0; i<s.length(); i++) System.out.println(s.charAt(i)); } }
    2. compareTo
      public class MyCode { public static void main(String[] args) { System.out.println("abc".compareTo("def")); System.out.println("def".compareTo("abc")); System.out.println("abc".compareTo("ABC")); System.out.println("ABC".compareTo("abc")); System.out.println("ab".compareTo("abc")); System.out.println("abc".compareTo("ab")); System.out.println("abc".compareTo("abc")); } }

      Another Example:

      public class MyCode { public static void main(String[] args) { System.out.println("abc" < "def"); // will not compile! } }

      Fixed:

      public class MyCode { public static void main(String[] args) { System.out.println("abc".compareTo("def") < 0); } }
    3. compareToIgnoreCase
      public class MyCode { public static void main(String[] args) { System.out.println("abc".compareToIgnoreCase("def")); System.out.println("def".compareToIgnoreCase("abc")); System.out.println("abc".compareToIgnoreCase("ABC")); System.out.println("ABC".compareToIgnoreCase("abc")); System.out.println("ab".compareToIgnoreCase("abc")); System.out.println("abc".compareToIgnoreCase("ab")); System.out.println("abc".compareToIgnoreCase("abc")); } }
    4. concat
      public class MyCode { public static void main(String[] args) { String s = "abc"; System.out.println(s.concat("def")); System.out.println(s + "def"); } }
    5. contains
      public class MyCode { public static void main(String[] args) { System.out.println("abc".contains("abc")); System.out.println("abc".contains("ABC")); System.out.println("abc".contains("abcd")); System.out.println("abc".contains("")); } }
    6. endsWith
      public class MyCode { public static void main(String[] args) { System.out.println("abc".endsWith("bc")); System.out.println("abc".endsWith("c")); System.out.println("abc".endsWith("")); System.out.println("abc".endsWith("b")); System.out.println("abc".endsWith("C")); } }
    7. equals
      public class MyCode { public static void main(String[] args) { System.out.println("abc".equals("abc")); System.out.println("abc".equals("def")); System.out.println("abc".equals("ABC")); System.out.println("abc".equals("ab")); System.out.println("abc".equals(null)); } }
    8. equals versus "=="
      public class MyCode { public static void main(String[] args) { // Do not use "==" with Strings -- the results are unpredictable! String ab = "ab"; String cd = "cd"; String s1 = "abcd"; String s2 = ab + cd; String s3 = "ab" + "cd"; String s4 = "ab".concat("cd"); // Note that s1, s2, s3, and s4 all hold the characters "abcd" System.out.println("See how equals works as expected for ALL of them:"); System.out.println(s1.equals(s2)); System.out.println(s1.equals(s3)); System.out.println(s1.equals(s4)); System.out.println("But == only works as expected for SOME of them:"); System.out.println(s1 == s2); System.out.println(s1 == s3); System.out.println(s1 == s4); } }
    9. equalsIgnoreCase
      public class MyCode { public static void main(String[] args) { System.out.println("abc".equalsIgnoreCase("abc")); System.out.println("abc".equalsIgnoreCase("def")); System.out.println("abc".equalsIgnoreCase("ABC")); System.out.println("abc".equalsIgnoreCase("ab")); System.out.println("abc".equalsIgnoreCase(null)); } }
    10. indexOf
      public class MyCode { public static void main(String[] args) { System.out.println("indexOf a char:"); System.out.println("ABC".indexOf('A')); System.out.println("ABC".indexOf('B')); System.out.println("ABC".indexOf('D')); System.out.println(); System.out.println("indexOf a String:"); System.out.println("ABC".indexOf("BC")); System.out.println("ABC".indexOf("CD")); System.out.println(); System.out.println("indexOf, starting from a given index:"); System.out.println("ABCDBC".indexOf("BC",1)); System.out.println("ABCDBC".indexOf("BC",2)); System.out.println("ABCDBC".indexOf("BC",5)); } }
    11. lastIndexOf
      public class MyCode { public static void main(String[] args) { System.out.println("lastIndexOf a char:"); System.out.println("ABCAB".lastIndexOf('A')); System.out.println("ABCAB".lastIndexOf('B')); System.out.println("ABCAB".lastIndexOf('D')); System.out.println(); System.out.println("lastIndexOf a String:"); System.out.println("ABCAB".lastIndexOf("AB")); System.out.println("ABCAB".lastIndexOf("CD")); System.out.println(); System.out.println("lastIndexOf, starting from a given index:"); System.out.println("ABCDBC".lastIndexOf("BC",4)); System.out.println("ABCDBC".lastIndexOf("BC",3)); System.out.println("ABCDBC".lastIndexOf("BC",0)); } }
    12. length
      public class MyCode { public static void main(String[] args) { System.out.println("abcd".length()); System.out.println("a b c d".length()); System.out.println("a\nb\nc\nd".length()); System.out.println("".length()); } }
    13. replace
      public class MyCode { public static void main(String[] args) { System.out.println("Replace char-for-char:"); System.out.println("abcabc".replace('b','z')); System.out.println(); System.out.println("Replace string-for-string:"); System.out.println("abcabc".replace("bc","xyz")); System.out.println("abcabc".replace("bc","")); } }

      Illegal uses of replace:

      public class MyCode { public static void main(String[] args) { System.out.println("abcabc".replace("bc",'d')); // Error! System.out.println("abcabc".replace('b','')); // A different error! } }
    14. startsWith
      public class MyCode { public static void main(String[] args) { System.out.println("abc".startsWith("ab")); System.out.println("abc".startsWith("a")); System.out.println("abc".startsWith("")); System.out.println("abc".startsWith("b")); System.out.println("abc".startsWith("A")); } }
    15. substring
      public class MyCode { public static void main(String[] args) { // Note: substring includes start index but excludes end index System.out.println("ABCD".substring(0,1)); System.out.println("ABCD".substring(0,2)); System.out.println("ABCD".substring(1,2)); System.out.println("ABCD".substring(1,3)); System.out.println(); // Note: if omitted, the default end index is the string's length! String s = "ABCD"; System.out.println(s.substring(2,s.length())); System.out.println(s.substring(2)); } }
    16. toLowerCase
      public class MyCode { public static void main(String[] args) { System.out.println("This Needs 4 Case Changes!".toLowerCase()); } }
    17. toUpperCase
      public class MyCode { public static void main(String[] args) { System.out.println("Carpe Diem!".toUpperCase()); } }
    18. trim
      public class MyCode { public static void main(String[] args) { String s1 = " ab cd "; String s2 = s1.trim(); System.out.println("[" + s1 + "]"); System.out.println("[" + s2 + "]"); } }
    19. Online String API
      See:  http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html
       
  2. Character Methods
    1. isDigit
      public class MyCode { public static void main(String[] args) { System.out.println(Character.isDigit('A')); System.out.println(Character.isDigit('3')); System.out.println(Character.isDigit(3)); // Surprise! } }
    2. isLetter
      public class MyCode { public static void main(String[] args) { System.out.println(Character.isLetter('A')); System.out.println(Character.isLetter('a')); System.out.println(Character.isLetter(' ')); System.out.println(Character.isLetter('\n')); System.out.println(Character.isLetter('3')); System.out.println(Character.isLetter(3)); } }
    3. isLetterOrDigit
      public class MyCode { public static void main(String[] args) { System.out.println(Character.isLetterOrDigit('A')); System.out.println(Character.isLetterOrDigit('a')); System.out.println(Character.isLetterOrDigit(' ')); System.out.println(Character.isLetterOrDigit('\n')); System.out.println(Character.isLetterOrDigit('3')); System.out.println(Character.isLetterOrDigit(3)); } }
    4. isLowerCase
      public class MyCode { public static void main(String[] args) { System.out.println(Character.isLowerCase('A')); System.out.println(Character.isLowerCase('a')); System.out.println(Character.isLowerCase(' ')); System.out.println(Character.isLowerCase('3')); } }
    5. isUpperCase
      public class MyCode { public static void main(String[] args) { System.out.println(Character.isUpperCase('A')); System.out.println(Character.isUpperCase('a')); System.out.println(Character.isUpperCase(' ')); System.out.println(Character.isUpperCase('3')); } }
    6. isWhitespace
      public class MyCode { public static void main(String[] args) { System.out.println(Character.isWhitespace('A')); System.out.println(Character.isWhitespace('a')); System.out.println(Character.isWhitespace(' ')); System.out.println(Character.isWhitespace('3')); System.out.println(Character.isWhitespace('\n')); System.out.println(Character.isWhitespace('\t')); } }
    7. toLowerCase
      public class MyCode { public static void main(String[] args) { System.out.println(Character.toLowerCase('A')); System.out.println(Character.toLowerCase('a')); System.out.println(Character.toLowerCase('#')); System.out.println(Character.toLowerCase('3')); } }
    8. toUpperCase
      public class MyCode { public static void main(String[] args) { System.out.println(Character.toUpperCase('A')); System.out.println(Character.toUpperCase('a')); System.out.println(Character.toUpperCase('#')); System.out.println(Character.toUpperCase('3')); } }
    9. Online Character API
      See: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Character.html