String Methods Flashcards
charAt() Method
Returns char
Returns: A char value at the specified index of this string.
The first char value is at index 0
The charAt() method returns the character at the specified index in a string.
The index of the first character is 0, the second character is 1, and so on.
String myStr = “Hello”;
char result = myStr.charAt(0);
System.out.println(result);
H
Throws:IndexOutOfBoundsException - if index is negative or not less than the length of the specified string
length() Method
Returns an int value
Returns: An int value, representing the length of the string
The length() method returns the length of a specified string.
Note: The length of an empty string is 0.
String txt = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
System.out.println(txt.length());
26
startsWith()
returns boolean
The startsWith() method returns true if a string starts with a specified string.
Otherwise it returns false.
The startsWith() method is case sensitive.
string.startsWith(searchValue, start)
search Value: Required The string to search for
start: Optional Start position. Default is 0.
Start at position 0:
let text = "Hello world, welcome to the universe."; text.startsWith("Hello");
Start at position 6:
let text = "Hello world, welcome to the universe."; text.startsWith("world", 7);
endsWith() Method
returns boolean
The endsWith() method checks whether a string ends with the specified character(s).
Returns: A boolean value:
true - if the string ends with the specified character(s)
false - if the string does not end with the specified character(s)
String myStr = “Hello”;
System.out.println(myStr.endsWith(“Hel”)); // false
System.out.println(myStr.endsWith(“llo”)); // true
System.out.println(myStr.endsWith(“o”)); // true
substring() Method
returns new string
Returns a string containing the extracted characters.
String sentence = “Students are loving the codeboard assignments - said no one ever”;
String partialString = sentence.substring(0, 8); // Will extract from 0 to 7 - “Students”
The substring() method extracts characters, between two indices (positions), from a string, and returns the substring.
The substring() method extracts characters from start to end (exclusive).
The substring() method does not change the original string.
If start is greater than end, arguments are swapped: (4, 1) = (1, 4).
Start or end values less than 0, are treated as 0.
string. substring(start, end)
start: Required. Start position. First character is at index 0.
end: Optional. End position (up to, but not including. If omitted: the rest of the string.
It will cut from the beginning of the string up to and including the number specified.
indexOf() Method
returns an int value
Returns: An int value, representing the index of the first occurrence of the character in the string, or -1 if it never occurs
String myStr = “Hello planet earth, you are a great planet.”; System.out.println(myStr.indexOf(“e”, 5));
10
String myStr = “Hello planet earth, you are a great planet.”; System.out.println(myStr.indexOf(“planet”));
6
- returns the index of the [first occurrence] of the input String
0123456789012345678901234567890123456789012345678901234567890123456
indexOf(String str)
indexOf(String str, int fromIndex)
indexOf(int char)
indexOf(int char, int fromIndex)
str : A String value, representing the string to search for
fromIndex : An int value, representing the index position to start the search from
char : An int value, representing a single character, e.g ‘A’, or a Unicode value
.lastIndexOf();
returns an int value
The lastIndexOf() method returns the position of the last occurrence of specified character(s) in a string.
Returns the index of the [last occurrence] of the input String
// It still start counting from the left and counts from 0
String sentence = “I did all my codeboard assignments and the codeboard exploded! What is a codeboard anyways?”;
int num = sentence.lastIndexOf(“codeboard”);
System.out.println(num); // 73
lastIndexOf(String str)
lastIndexOf(String str, int fromIndex)
lastIndexOf(int char)
lastIndexOf(int char, int fromIndex)
str : A String value, representing the string to search for
fromIndex : An int value, representing the index position to start the search from
char : An int value, representing a single character, e.g ‘A’, or a Unicode value
String myStr = “Hello planet earth, you are a great planet.”; System.out.println(myStr.lastIndexOf(“e”, 5));
1
concat() Method
returns a string
Returns: A String, representing the text of the combined strings
String firstName = “John “;
String lastName = “Doe”;
System.out.println(firstName.concat(lastName));
John Doe
The concat() method appends (concatenate) a string to the end of another string.
public String concat(String string2)
string2: A String, representing the string that should be appended to the other string
// concat is very limited compared to +
replace() Method
returns a new string
Returns: A new String, where the specified character has been replaced by the new character(s)
The replace() method searches a string for a specified character, and returns a new string where the specified character(s) are replaced.
public String replace(char searchChar, char newChar)
searchChar: A char, representing the character that will be replaced by the new character
newChar: A char, representing the character to replace the searchChar with
String str = “ "We generate fear while we sit, we overcome them by watching." - Dr. Henry Link”;
// replace watching with action
str = str.replace(“watching”, “action”);
System.out.println(str);
str = str.replace(“We”, “You”);
System.out.println(str); // It is case-sensitive –> Only the first We will change
// We can use it with a String the contains a single character
// Notice how it replaces all the occurrences that match.
str = str.replace(“e”, “_”);
System.out.println(str);
replaceAll() Method
Replaces each substring of this string that matches the given regular expression with the given replacement
String str = “ "We generate fear while we sit, we overcome them by watching." - Dr. Henry Link”;
// It can also replace all occurrences and it is case-sensitive
str = str.replaceAll(“we”, “you”);
System.out.println(str); // It is more powerful than replace
String str = “I want 1 slices of honey cake and 2 cups of tea.”;
// To replace the integers with underscore _, we have to do it step by step
// str = str.replace(“3”, “_”);
// str = str.replace(“2”, “_”);
// System.out.println(str);
// replaceAll is powerful, because it accepts Regular Expressions (regex)
str = str.replaceAll(“[0-9]”, “_”);
System.out.println(str);
replaceFirst()
Replaces the first occurrence of a substring that matches the given regular expression with the given replacement.
replaces only the first occurrence, it is also case-sensitive
String str = “hello there, hello”;
str = str.replaceFirst(“hello”, “Hey”);
System.out.println(str);
contains() Method
returns boolean
The contains() method checks whether a string contains a sequence of characters.
Returns true if the characters exist and false if not.
It checks if a String contains a given sequence of characters
String str = “Electric cars are the best”;
boolean result = str.contains(“Electric”);
System.out.println(result); // true
result = str.contains(“electric”); // It is case-sensitive
System.out.println(result); // false
result = str.contains(“ “);
System.out.println(result); // true
result = str.contains(“ the”);
System.out.println(result); // true
result = str.contains(“T”);
System.out.println(result); // false
// If you want to check within a specific range, you have to be creative and do method chaining
System.out.println(str.substring(15).contains(“best”)); // true
// This is asking if the substring from 15 to the end of the original string contains the word best
toLowerCase() Method and toUpperCase() Method
Returns: A String value, representing the new string converted to lower case
The toLowerCase() method converts a string to lower case letters.
The toUpperCase() method converts a string to upper case letters.
String txt = “Hello World”;
System.out.println(txt.toUpperCase());
System.out.println(txt.toLowerCase());
String str = “Electric cars are THE best”;
str = str.toUpperCase();
System.out.println(str);
str = str.toLowerCase();
System.out.println(str);
// contains, indexOf, startsWith —> Q: is there a IgnoreCase variation
boolean result = str.toLowerCase().contains(“electric”);
System.out.println(result);
String strLowerCase = str.toLowerCase();
int idx = strLowerCase.indexOf(“the”);
System.out.println(idx);
isEmpty() Method
returns a boolean value
The isEmpty() method checks whether a string is empty or not.
This method returns true if the string is empty (length() is 0), and false if not.
String str1 = “”; // This is an empty String
String str2 = “apple”;
String str3 = “ “;
System.out.println(str1.isEmpty()); // true
System.out.println(str2.isEmpty()); // false
System.out.println(str3.isEmpty()); // false - space is a character
String myStr1 = “Hello”;
String myStr2 = “”;
System.out.println(myStr1.isEmpty());
System.out.println(myStr2.isEmpty());
false
true
isBlank() Method
returns a boolean value
String str1 = “”; // This is an empty String
String str2 = “apple”;
String str3 = “ “;
System.out.println(str1.isBlank()); // true - isBlank and isEmpty
System.out.println(str2.isBlank()); // false - neither blank nor empty
System.out.println(str3.isBlank()); // true - it is blank, but it is not empty