Java String Methods - Codehs
String Construction The numbers refer to the place of the Character within the string. Note it starts with 0, not 1.
.substring word.substring(0,1); Where “word” is the string variable (eg. “hello”) and (0, 1) is the first character place. word.substring(length – 1); Where (length – 1) is the last character in the string.
.length word.length(); Where “word” is the string variable, this will output an integer. Eg. string word = “hello”; int x = word.length(); X = 5
.toUpperCase word.toUpperCase(); Where “word” is the string variable. Eg. string word = “hello”; string uppercase = word.toUpperCase(); uppercase = “HELLO”;
.charAt word.charAt(i); Where “word” is the string variable. Often used in a for loop. Eg. String word = “hello”; for(i = 0; i < length; i++){ char x = word.CharAt(i); return x; }
.isDigit Character.isDigit(x); Where x is the character variable. Often used in if statements. Eg. if(Character.isDigit(x)){ x = true; }else{ x = false; }
Character.toUpperCase (and Character.toLowerCase) Character.toUpperCase(x); Where x is the character variable to be converted to upper or lower case. Eg. char x = a; Char y = Character.toUpperCase(x); y = A;
Character.toString Character.toString(x); Where x is the character variable. This is used in a for loop to add characters to a string. Eg. string longword = “”; char word = Character.toString(x); longword += word;
Character.isLetterOrDigit Character.isLetterOrDigit(x); Where x is the character variable. Usually used in an if statement or boolean. Eg. char x = ‘5’; Boolean digit = Character.isLetterOrDigit(x); digit = true;