Jan 02

StringTest.java Demonstrates various methods of the String class.

/** Taken from Core Web Programming from
 *  Prentice Hall and Sun Microsystems Press,
 *  .
 *  © 2001 Marty Hall and Larry Brown;
 *  may be freely used or adapted.
 */


  public class StringTest {
  public static void main (String[] args) {
    String str = "";
    if (args.length > 0) {
        str = args[0];
    }
    if (str.length()>8) {
      System.out.println("String is "" + str + ""n");
      System.out.println("  charAt(3) ------------------ " +
                         str.charAt(3));
      System.out.println("  compareTo(Moscow) ---------- " +
                         str.compareTo("Moscow"));
      System.out.println("  concat(SuFFiX) ------------- " +
                         str.concat("SuFFiX"));
      System.out.println("  endsWith(hic) -------------- " +
                         str.endsWith("hic"));
      System.out.println("  == Geographic -------------- " +
                         (str == "Geographic"));
      System.out.println("  equals(geographic) --------- " +
                         str.equals("geographic"));
      System.out.println("  equalsIgnoreCase(geographic) " +
                         str.equalsIgnoreCase("geographic"));
      System.out.println("  indexOf('o') --------------- " +
                         str.indexOf('o'));
      System.out.println("  indexOf('i',5) ------------- " +
                         str.indexOf('i',5));
      System.out.println("  indexOf('o',5) ------------- " +
                         str.indexOf('o',5));
      System.out.println("  indexOf(rap) --------------- " +
                         str.indexOf("rap"));
      System.out.println("  indexOf(rap, 5) ------------ " +
                         str.indexOf("rap", 5));
      System.out.println("  lastIndexOf('o') ----------- " +
                         str.lastIndexOf('o'));
      System.out.println("  lastIndexOf('i',5) --------- " +
                         str.lastIndexOf('i',5));
      System.out.println("  lastIndexOf('o',5) --------- " +
                         str.lastIndexOf('o',5));
      System.out.println("  lastIndexOf(rap) ----------- " +
                         str.lastIndexOf("rap"));
      System.out.println("  lastIndexOf(rap, 5) -------- " +
                         str.lastIndexOf("rap", 5));
      System.out.println("  length() ------------------- " +
                         str.length());
      System.out.println("  replace('c','k') ----------- " +
                         str.replace('c','k'));
      System.out.println("  startsWith(eog,1) ---------- " +
                         str.startsWith("eog",1));
      System.out.println("  startsWith(eog) ------------ " +
                         str.startsWith("eog"));
      System.out.println("  substring(3) --------------- " +
                         str.substring(3));
      System.out.println("  substring(3,8) ------------- " +
                         str.substring(3,8));
      System.out.println("  toLowerCase() -------------- " +
                         str.toLowerCase());
      System.out.println("  toUpperCase() -------------- " +
                         str.toUpperCase());
      System.out.println("  trim() --------------------- " +
                         str.trim());
      System.out.println("nString is still "" + str + ""n");
    }
  }
}

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017

Skip to toolbar