The following lists the most common string operations.
Command Description
"Testing".equals(text1);
Return true
if text1
is equal to
"Testing"
. The check is case-sensitive.
"Testing".equalsIgnoreCase(text1);
Return true
if text1
is equal to "Testing"
. The check is not case-sensitive. For example, it would also be true for"testing"
.
StringBuilder str1 = new StringBuilder();
Define a new StringBuilder
which allows to efficiently add "Strings".
str.charat(1);
Return the character at position 1. (Note: strings are arrays of chars starting with 0)
str.substring(1);
Removes the first characters.
str.substring(1, 5);
Gets the substring from the second to the fifth character.
str.indexOf("Test")
Look for the String
"Test" in String str
. Returns the index of the first occurrence of the specified string.
str.lastIndexOf("ing")
Returns the index of the last occurrence of the specifiedString
"ing" in the String
str.
str.endsWith("ing")
Returns true
if str
ends with String
"ing"
str.startsWith("Test")
Returns true
if String
str
starts with String"Test"
.
str.trim()
Removes leading and trailing spaces.
str.replace(str1, str2)
Replaces all occurrences of str1
by str2
str2.concat(str1);
Concatenates str1
at the end of str2
.
str.toLowerCase()
/ str.toUpperCase()
Converts the string to lower- or uppercase
str1 + str2
Concatenate str1
and str2