The String class methods operate on and alter String values. Unlike the Math class, they are not called from the class. Instead, they are called from a String variable. This means that a String variable must first be declared and initialized prior to calling a String method.
Many of the String methods operate using indicies which is the number order of the characters. String character counting starts at zero.
Additionally, String method are non-mutators. In other words, the String method returns an altered copy of the String, but does not alter the original String. The String is said to be immutable.
Below, you will find examples of several of the more prominent String methods. For all examples, assume that var str="programming".
Length
a property (not method) that returns the number of characters in the string.
str.length;
11
Find a string in a string
returns the starting index of the first occurrence of a string in str. If the parameter string is not found, the method returns -1.There also exists a lastIndexOf method which does the same in reverse AND a version of indexOf that takes a second parameter where it starts the search
str.indexOf("gram");
str.indexOf("js");
3 -1
Index Character
returns the character at the specified index
str.charAt(4);
r
Case Changes
returns a copy of the string with all of the character alter to either upper or lower case
str.toUpperCase();
str.toLowerCase();
PROGRAMMING programming
String Parts
returns a substring of the string starting at the first value. If only one value is given, the substring is from that point (inclusive) to the end of the string. If a second value is given, the substring goes up to, but not including, the character at the second value.
str.substring(7);
str.substring(3,7);
ming gram
Below are some examples of the String methods in action:
let name="Sally;"
let uname=name.toUpperCase();
document.getElementById('output').innerHTML+=name+":::"+uname;
The output from this code is Sally:::SALLY. Remember, that the String methods are NOT mutators. They return an altered COPY of the original.
let answer=window.prompt("Do you want to continue? (YES or NO)");
let achar=answer.charAt(0).toLowerCase();
At the end of this segment, the achar variable will hold either 'y' or 'n', assuming the user input was YES or NO. In line(3), the answer input is reduced to its first character (index = 0) and then converted to lower case.
let word="programming";
let firstM=word.indexOf("m");
let firstG=word.indexOf("g");
let smaller=Math.min(firstM, firstG);
let larger=Math.max(firstM, firstG);
let between=word.substring(smaller+1, larger);
window.alert(between);
This example combines two String methods with two Math methods. In lines (2) and (3), the indexOf method finds the first occurrence of an m(index=6) and of a g(index=3). The Math methods then find which of the values is smaller and which is larger in lines (4) and (5). Finally, in line (6), the substring method is called from smaller+1 (so as not to include the letter) up to but not including the larger - resulting in an output of "ra".