Chapter 5: Strings
Character Data Type
- Character type represents a single character
- To declare we use ‘char’
- A char literal is enclosed in single quotation marks,
- Example:
Char letter = ‘A’
Unicode Format
- Java characters use Unicode, a 16-but encoding scheme to support interchange, processing, and
display of written text in world’s diverse languages.
- Unicode takes 2 bytes, preceded by \u, expressed in 4 hex-numbers from ‘\u0000’ to ‘\uFFFF’
- Unicode can represent 65535 + 1 characters.
Escape Sequences for Special Characters
Casting between char and Numeric Types
- int i = 'a'; // Same as int i = (int)'a';
- char c = 97; // Same as char c = (char)97;
Comparing and Testing Characters
- Often we need to test whether a character is a number, a letter, an uppercase letter or a
lowercase letter
- eg. Let variable ch as a char data tye
if (ch >= ‘A’ && ch <= ‘Z’)
System.out.println(ch + “ is an uppercase letter”);
else if (ch >= ‘a’ && ch <= ‘z’)
System.out.println(ch + “ is a lowercase letter”);
else if (ch >= ‘0’ && ch <= ‘9’)
System.out.println(ch + “ is a numeric character”);
The Character Class
Reading a character from the Console
- use nextLine() or next() method to read string and then invoke the charAt(0) method.
- For example:
Constructing a String:
- String message = “Welcome to Java”;
String message = new String(“Welcome to Java”);
String s = new String();
Simple methods for String objects
Finding String Length
- Finding a string length using the length() method:
String message = “Welcome to Java”;
System.out.println(“The length of “ + message + “ is “ + message.length());
Getting Characters from a String
- The method charAt(index) is used. The index is between 0 to the string length-1.
- For example, message.charAt (index)
Indices 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
message W e l c o m e t o J a v a
message.charAt(0) message.length() is 15 message.charAt(14)
String Concatenation
- Use concat method:
String s3 = s1.concat(s2); //s1 and s2 into s3
String s3 = s1 + s1; // same effect
- Operator + can also concatenate a non-string with string ( at least one must be string)
- Can also use augmented += operator
- Example:
int i = 1;
int j = 2;
System.out.println(“i + j is” + i + j);
Reading a String from the Console
- Use next() method on a Scanner object. This method read a string that end with whitespace
character.
- For example:
Scanner input = new Scanner(System.in);
System.out.print(“Enter three words separated by spaces: “);
String s1 = input.next();
String s2 = input.next();
String s3 = input.next();
System.out.println(“s1 is “ + s1);
System.out.println(“s2 is “ + s2);
System.out.println(“s3 is “ + s3);
- Use nextLine() method to read entire line of text. This method reads a string that ends with the
Enter key pressed. (line-based input)
- For example
Scanner input = new Scanner(System.in);
System.out.print(“Enter a message: “);
String msg = input.nextLine();
System.out.println(“The message entered is “ + msg);
- Caution (to avoid input error, don’t use line-based input after token-based input)
Reading a Character from the Console
- We can use nextLine() method to read string then invoe the charAt(0) method.
- For example:
Scanner input = new Scanner(System.in);
System.out.print(“Enter a character: “);
String st = input.nextLine();
Char ch = st.charAt(0);
System.out.println(“The character entered is “ + ch);
Comparing Strings
Obtaining Substrings
- You can obtain a substring from a string using the substring method in the String class.
Extracting Substrings
Finding a Character or a Substring in a String
“Welcome to Java”. indexOf(‘W’) returns 0
“Welcome to Java”. indexOf(‘x’) returns -1
- Return the index of the first character in the string that matches the specified character
“Welcome to Java”. indexOf(‘o’ , 5) returns 9
- Returns index of first character ‘o’ in string starting from the index 5
“Welcome to Java”. indexOf(“come”) returns 3
- Returns index of first character of substring
"Welcome to Java".indexOf("Java", 5) returns 11.
"Welcome to Java".indexOf("java", 5) returns -1.
- Returns index of the first character of substring starting from the index 5
“Welcome to Java”. lastIndexOf(‘a’)
- Returns 14
Conversion between Strings and Numbers
- To convert numeric string into a number
int intValue = Integer.parseInt(intString);
- To convert a string into a double value
Double doubleValue = Double.parseDouble(doubleString);
- To convert a number into a string
String s = number + “”;
Formatting Console Output
- General syntax
System.out.printf(format, item1, item2, …, itemn);
Format is a string that may consist of substring and format specifiers
- Frequently-Used Specifiers
- Specifying Width and Precision (use – in front to make It left justified)
Common Mathematical Functions
- Java provides many useful method in Math class for performing common mathematical
functions.
- Methods in Math class
Class constants:
PI
Class methods:
Trigonometric Methods
Exponent Methods
Rounding Methods
Min, max, abs, random method