Using trim() to process commands. : String « Data Type « Java Tutorial






import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class UseTrim {
  public static void main(String args[]) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str;

    System.out.println("Enter 'stop' to quit.");
    System.out.println("Enter letter: ");
    do {
      str = br.readLine();
      str = str.trim();

      if (str.equals("I"))
        System.out.println("I");
      else if (str.equals("M"))
        System.out.println("M");
      else if (str.equals("C"))
        System.out.println("C.");
      else if (str.equals("W"))
        System.out.println("W");
    } while (!str.equals("stop"));
  }
}








2.19.String
2.19.1.Using String class
2.19.2.String Literals
2.19.3.String class constructors
2.19.4.Create String with char array
2.19.5.Length of a string
2.19.6.Assign String variable to null
2.19.7.Attempts to use string variable before it has been initialized
2.19.8.toLowerCase and toUpperCase
2.19.9.Comparing Two Strings
2.19.10.Demo for escape
2.19.11.Arrays of Strings: using 'new' operator
2.19.12.Arrays of Strings: Declare an array of String objects where the initial values determine the size of the array
2.19.13.String class substring methods
2.19.14.String Concatenation
2.19.15.String HashCode
2.19.16.Using trim() to process commands.
2.19.17.Remove leading and trailing white space from string
2.19.18.To remove a character
2.19.19.Remove a character at a specified position using String.substring
2.19.20.Get InputStream from a String
2.19.21.Demonstrate toUpperCase() and toLowerCase().
2.19.22.A string can be compared with a StringBuffer