Class Notes for String Methods |
|
// A String is a list of characters numbered from 0
// You can add (concatenate) Strings just like numbers
// The length method tells you how many characters the string contains
// 111
// 0123456789012
String s = "Justin Bie Bieber";
int length = s.length();
System.out.println("length = " + length);
// The substring method gives you only a part of the string
String sub1 = s.substring(7);
System.out.println(sub1);
// The advanced substring method give you more control
String sub2 = s.substring(2,9);
System.out.println(sub2);
// The charAt method returns a char at the given place (place must exist)
System.out.println(s.charAt(7));
// The indexOf method searches the string for the first occurrence of whatever // // string you provide
System.out.println(s.indexOf("Bie"));
// The replace methods finds all instances of a char or String and replaces them
System.out.println(s.replace(" ","*"));
System.out.println(s.replace("B","D"));
// The methods toUpperCase and toLowerCase converts the string to upper or lower // case
System.out.println(s.toUpperCase());
System.out.println(s.toLowerCase());
Videos |
|