Java Reference
In-Depth Information
If you want to know which character is at a certain position, you can use the charAt()
method:
name.charAt(1);
<< "e"
This tells us that the character "e" is at position 1 . If you were thinking that it should be
"H" , this is because the first letter is classed as being at position 0 (you'll find that count-
ing usually starts at zero in programming!).
If you want to find where a certain character or substring appears in a string, we can use
the indexOf() method:
name.indexOf("H");
<< 0
If a character doesn't appear in the string, -1 will be returned:
name.indexOf("a");
<< -1
If we want the last occurrence of a character or substring, we can use the lastIn-
dexOf() method:
name.lastIndexOf("e");
<< 7
The concat() method can be used to concatenate two or more strings together:
"JavaScript".concat("Ninja");
<< "JavaScriptNinja"
"Hello".concat(" ","World","!");
<< "Hello World!"
A shortcut for string concatenation is to use the + symbol to add the two strings together:
Search WWH ::




Custom Search