String Methods Flashcards

1
Q

charAt() Method

Returns char

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

length() Method

Returns an int value

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

startsWith()

returns boolean

A

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);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

endsWith() Method

returns boolean

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

substring() Method

returns new string

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

indexOf() Method

returns an int value

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

.lastIndexOf();

returns an int value

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

concat() Method

returns a string

A

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 +

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

replace() Method

returns a new string

A

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

replaceAll() Method

A

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

replaceFirst()

A

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

contains() Method

returns boolean

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

toLowerCase() Method and toUpperCase() Method

A

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

isEmpty() Method

returns a boolean value

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

isBlank() Method

returns a boolean value

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

trim() Method

returns a string value

A

Returns: A String value, which is a copy of the string, without leading and trailing whitespace

Removes spaces only before and after the String

String str = “ Welcome to Yoll Academy! “;

System.out.println(str.length());

str = str.trim();

System.out.println(str.length());

System.out.println(str);

String myStr = “ Hello World! “;

System.out.println(myStr);

System.out.println(myStr.trim());

Hello World!

Hello World!

17
Q

equals() Method

returns a boolean value

A

The equals() method compares two strings, and returns true if the strings are equal, and false if not.

Compare strings to find out if they are equal:

String myStr1 = “Hello”;

String myStr2 = “Hello”;

String myStr3 = “Another String”;

System.out.println(myStr1.equals(myStr2)); // Returns true because they are equal System.out.println(myStr1.equals(myStr3)); // false

18
Q

Arrays Class Methods

A

Arrays.toString(); → converts given array to a string
○ String a = Arrays.toString(counties);

● Arrays.sort(); → sorts the complete array in ascending order
○ Arrays.sort(counties);

● Arrays.equals(); → checks if both the arrays are equal or not
○ Arrays.equals(arrayfr, myArr2);

● Arrays.copyOf(); → creates new array using a copy of another array. You can specify the new size.
○ String allCities[] = Arrays.copyOf(citiesArr, 10); //10 → specifies the size

19
Q

equalsIgnoreCase() Method

A
20
Q

split() Method

A

.split(String str) → is used for splitting a String into its substrings based on the given delimiter

○ mostly used to break sentence into words → .split(“ ”)