Key Concepts

Review core concepts you need to learn to master this subject

length() String Method in Java

String str = "Codecademy"; System.out.println(str.length()); // prints 10

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

concat() String Method in Java

String str = "Codecademy"; System.out.println(str.length()); // prints 10

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.

String Method equals() in Java

String str = "Codecademy"; System.out.println(str.length()); // prints 10

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().

indexOf() String Method in Java

String str = "Codecademy"; System.out.println(str.length()); // prints 10

In Java, the indexOf() string method returns the first occurence 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.

charAt() String Method in Java

String str = "Codecademy"; System.out.println(str.length()); // prints 10

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.

toUpperCase() and toLowerCase() String Methods

String str = "Codecademy"; System.out.println(str.length()); // prints 10

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.

String Methods
Lesson 1 of 1
  1. 1
    As you may recall, a String, which is widely used in Java, is an object that represents a sequence of characters. It is a great way to store information. Because character strings are so vital to…
  2. 2
    In Java, the length() string method returns the length ⁠— total number of characters ⁠— of a String. Suppose we have a String called str, str.length() would return its length. Take a look at this…
  3. 3
    The concat() method concatenates one string to the end of another string. Concatenation is the operation of joining two strings together. Suppose we have a String called str1 and another String ca…
  4. 4
    With objects, such as Strings, we can’t use the primitive equality operator == to check for equality between two strings. To test equality with strings, we use a built-in method called equals(). F…
  5. 5
    If we want to know the index of the first occurence of a character in a string, we can use the indexOf() method on a string. Remember that the indices in Java start with 0: String letters = “ABCD…
  6. 6
    The charAt() method returns the character located at a String’s specified index. For example: String str = “qwer”; System.out.println(str.charAt(2)); It would output e because that’s what’s at…
  7. 7
    There may be times when we only want a part of a string. In such cases, we may want to extract a substring from a string. The substring() method does exactly that. For example: String line = “…
  8. 8
    There will be times when we have a word in a case other than what we need it in. Luckily, Java has a couple String methods to help us out: - toUpperCase(): returns the string value converted to up…
  9. 9
    Congratulations! 🙌 We have learned some of the string methods that come with the String class: - length() - concat() - indexOf() - charAt() - equals() / equalsIgnoreCase() - substring() - toUppe…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo