String Methods Flashcards

1
Q

length() String Method in Java

A

In Java, the length() string method returns the total number of characters - the length - of a String.

EX.

String str = “Codeacademy”;

System.out.println(str.length());
//prints 10

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

concat() String Method in Java

A

In Java, the concat() string method is used to append one String to the end of another String. This method returns a String representing the text of the combined strings.

EX.

String s1 = “Hello”;
String s2 = “ World!”;

String s3 = s1.concat(s2);
//concatenation strings s1 and s2

System.out.println(s3);
// prints “Hello World!”

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

String Method equals() in Java

A

In Java, the equals() string method tests for equality between two Strings.

equals() compares the contents of each String. If all of the characters between the two match, the method returns true. If any of the characters do not match, it returns false.

Additionally, if you want to compare two strings without considering upper/lower cases, you can use .equalsIgnoreCase().

EX.

String s1 = “Hello”;
String s2 = “World”;

System.out.println(s1.equals(“Hello”));
// prints true

System.out.println(s2.equals(“Hello”));
//prints false

System.out.println(s2.equalsIgnoreCase(“world”));
//prints true

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

indexOf() String Method in Java

A

In Java, the indesOf() string method returns the first occurrence of a character or a substring in a String. The character/substring that you want to find the index of goes inside of the ().

if indexOf() cannot find the character or substring it will return -1.

EX.

String str = “Hello World!”;

System.out.println(str.indexOf(“1”));
//prints 2

System.out.println(str.indexOf(“Wor”));
//prints 6

System.out.println(str.indexOf(“z”));
// prints -1

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

charAt() String Method in Java

A

In Java, the charAt() string method returns the character of a String at a specified index. The index value is passed inside of the (), and should lie between 0 and length() - 1.

EX.

String str = “This is a string”;

System.out.println(str.charAt(0));
//prints ‘T’

System.out.println(str.charAt(15));
//prints ‘g’

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

toUpperCase() and toLowerCase() String Methods

A

In Java, we can easily convert a String to upper and lower case with the help of a few string methods:

  • toUpperCase() returns the string value converted to uppercase.
  • toLowerCase() returns the string value converted to lowercase

EX.

String str = “Hello World!”;

String uppercase = str.toUpperCase();
// uppercase = “HELLO WORLD!”

String lowercase = str.toLowerCase();
// lowercase = “hello world!”

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