Presentation is loading. Please wait.

Presentation is loading. Please wait.

String Methods.

Similar presentations


Presentation on theme: "String Methods."— Presentation transcript:

1 String Methods

2 String message = " HELLO you! ";
Strings String myString = "abcDEFMNopqrstYZ"; String message = " HELLO you! "; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 'a' 'b' 'c' 'D' 'E' 'F' 'M' 'N' 'o' 'p' 'q' 'r' 's' 't' 'Y' 'Z' 1 2 3 4 5 6 7 8 9 10 11 12 13 ' ' 'H' 'E' 'L' 'O' 'y' 'o' 'u' '!'

3 String Methods length() charAt(int index) toLowerCase() toUpperCase()
equals(String s) equalsIgnoreCase(String s) returns the length (number of characters) of a string returns character at index position converts the supplied String to lowercase converts the supplied String to uppercase compares 2 Strings – returns a boolean value (true/false) compares 2 Strings (ignoring case) – returns a boolean value (true/false)

4 String Methods replace(char x, char y) trim() substring(int begin)
substring(int begin, int end) concat(String s) returns a new String with all occurrences of the character x replaced with the character y returns a new String with all leading and trailing spaces removed returns a new String starting at begin index to the end of the String returns a new String starting at begin index to (end – 1) index (NB) returns a new String with s appended to the end of the String

5 String Methods Printing out each character in a string individually:
String name = “Martin"; for (int index = 0; index < name.length(); index++) System.out.print(name.charAt(index) + "\t"); System.out.println(); USEFUL: Checking if a character is uppercase (similar for lower case): if (Character.isUpperCase(name.charAt(0))) { System.out.println("'" + name.charAt(0) + "' is uppercase"); }//if else { System.out.println("'" + name.charAt(0) + "' is not uppercase"); }//else

6 String message = " HELLO you! ";
Strings String myString = "abcDEFMNopqrstYZ"; String message = " HELLO you! "; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 'a' 'b' 'c' 'D' 'E' 'F' 'M' 'N' 'o' 'p' 'q' 'r' 's' 't' 'Y' 'Z' 1 2 3 4 5 6 7 8 9 10 11 12 13 ' ' 'H' 'E' 'L' 'O' 'y' 'o' 'u' '!'

7 String myString = ”abcDEFMNopqrstYZ"; String message = " HELLO you! ";
myString.length() myString.charAt(5) message.equals(" hello you! ") myString.toLowerCase() myString.toUpperCase() message.equalsIgnoreCase(" hello you! ") message.replace('L', '*') message.trim() message.substring(8) myString.substring(5,10) message.concat("!!!") myString.endsWith("stYZ") message.startsWith("HELLO") 16 'F' false "abcdefmnopqrstyz" "ABCDEFMNOPQRSTYZ" true " HE**O you! " "HELLO you!" "you! " "FMNop" " HELLO you! !!!"

8 String myString = "abcDEFMNopqrstYZ"; String message = " HELLO you! ";
message.length() message.charAt(5) myString.equals(“abcDEFMNopqrstYZ") message.toLowerCase() message.toUpperCase() myString.equalsIgnoreCase(“ABCDEFMNOPQRSTYZ") myString.replace(‘Z', '*') myString.trim() myString.substring(8) message.substring(5,10) myString.concat("!!!") message.endsWith(“you!") myString.startsWith(“ HELLO") 14 ‘L' true " hello you! ” " HELLO YOU! " " abcDEFMNopqrstY*" "abcDEFMNopqrstYZ" "opqrstYZ" "LO yo" "abcDEFMNopqrstYZ!!!" false


Download ppt "String Methods."

Similar presentations


Ads by Google