Java String Methods Codehs String Construction The numbers

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

. to. Upper. Case word. to. Upper. Case(); Where “word” is the string variable. Eg. string word = “hello”; string uppercase = word. to. Upper. Case(); uppercase = “HELLO”;

. char. At word. char. At(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. char. At(i); return x; }

. is. Digit Character. is. Digit(x); Where x is the character variable. Often used in if statements. Eg. if(Character. is. Digit(x)){ x = true; }else{ x = false; }

Character. to. Upper. Case (and Character. to. Lower. Case) Character. to. Upper. Case(x); Where x is the character variable to be converted to upper or lower case. Eg. char x = a; Char y = Character. to. Upper. Case(x); y = A;

Character. to. String(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. to. String(x); longword += word;

Character. is. Letter. Or. Digit(x); Where x is the character variable. Usually used in an if statement or boolean. Eg. char x = ‘ 5’; Boolean digit = Character. is. Letter. Or. Digit(x); digit = true;
- Slides: 10