Java 11 String Methods Tutorial 🎯

beginner
24 min

Java 11 String Methods Tutorial 🎯

Welcome to our comprehensive guide on Java 11 String Methods! In this tutorial, we'll explore various methods available for manipulating and working with strings in Java. By the end of this lesson, you'll have a solid understanding of string methods, their uses, and practical applications. Let's dive in!

Introduction to Java Strings 📝

Before we delve into string methods, let's quickly recap what a string is in Java. A string is a sequence of characters enclosed in double quotes, e.g., "Hello, World!".

String Methods Overview 💡

Strings in Java are instances of the String class, which offers a rich set of methods for manipulating text. Some commonly used methods include:

  • length(): Returns the length of the string.
  • charAt(int index): Returns the character at the specified index.
  • substring(int beginIndex, int endIndex): Returns a new string that is a substring of the original string.
  • indexOf(String substring): Returns the index within the string where the specified substring begins.
  • concat(String str): Concatenates two strings.
  • replace(char oldChar, char newChar): Replaces each occurrence of the old character with the new character.
  • toUpperCase(): Converts the entire string to uppercase.
  • toLowerCase(): Converts the entire string to lowercase.
  • trim(): Removes leading and trailing whitespace.

Working with String Methods 🎯

Length Method 📝

The length() method returns the number of characters in a string. Here's an example:

java
String greeting = "Hello, World!"; int length = greeting.length(); System.out.println("The length of the greeting is: " + length);

Output:

The length of the greeting is: 13

CharAt Method 💡

The charAt(int index) method returns the character at the specified index. Remember that indices in Java start at 0.

java
String greeting = "Hello, World!"; char firstCharacter = greeting.charAt(0); System.out.println("The first character of the greeting is: " + firstCharacter);

Output:

The first character of the greeting is: H

Substring Method 🎯

The substring(int beginIndex, int endIndex) method returns a new string that is a substring of the original string.

java
String greeting = "Hello, World!"; String substring = greeting.substring(7, 13); System.out.println("The substring from index 7 to 12 is: " + substring);

Output:

The substring from index 7 to 12 is: World

IndexOf Method 💡

The indexOf(String substring) method returns the index within the string where the specified substring begins.

java
String greeting = "Hello, World!"; int index = greeting.indexOf("World"); System.out.println("The index of 'World' is: " + index);

Output:

The index of 'World' is: 7

Concat Method 🎯

The concat(String str) method concatenates two strings.

java
String greeting = "Hello, World!"; String additionalGreeting = "Nice to meet you!"; String combinedGreeting = greeting.concat(" ").concat(additionalGreeting); System.out.println("The combined greeting is: " + combinedGreeting);

Output:

The combined greeting is: Hello, World! Nice to meet you!

Replace Method 💡

The replace(char oldChar, char newChar) method replaces each occurrence of the old character with the new character.

java
String greeting = "Hello, World!"; String replacedGreeting = greeting.replace('l', 'o'); System.out.println("The replaced greeting is: " + replacedGreeting);

Output:

The replaced greeting is: Heo, Oroldo!

ToUpperCase and toLowerCase Methods 🎯

The toUpperCase() method converts the entire string to uppercase, while toLowerCase() converts it to lowercase.

java
String greeting = "Hello, World!"; String uppercaseGreeting = greeting.toUpperCase(); String lowercaseGreeting = greeting.toLowerCase(); System.out.println("The uppercase greeting is: " + uppercaseGreeting); System.out.println("The lowercase greeting is: " + lowercaseGreeting);

Output:

The uppercase greeting is: HELLO, WORLD! The lowercase greeting is: hello, world!

Trim Method 💡

The trim() method removes leading and trailing whitespace.

java
String greeting = " Hello, World! "; String trimmedGreeting = greeting.trim(); System.out.println("The trimmed greeting is: " + trimmedGreeting);

Output:

The trimmed greeting is: Hello, World!

Quiz 📝

Quick Quiz
Question 1 of 1

Which method can be used to remove leading and trailing whitespace from a string in Java?

That's it for our comprehensive guide on Java 11 String Methods! Now you have a solid understanding of various string methods, their uses, and practical applications. Happy coding! 🎉