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!
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!".
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.The length() method returns the number of characters in a string. Here's an example:
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
The charAt(int index) method returns the character at the specified index. Remember that indices in Java start at 0.
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
The substring(int beginIndex, int endIndex) method returns a new string that is a substring of the original string.
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
The indexOf(String substring) method returns the index within the string where the specified substring begins.
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
The concat(String str) method concatenates two strings.
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!
The replace(char oldChar, char newChar) method replaces each occurrence of the old character with the new character.
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!
The toUpperCase() method converts the entire string to uppercase, while toLowerCase() converts it to lowercase.
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!
The trim() method removes leading and trailing whitespace.
String greeting = " Hello, World! ";
String trimmedGreeting = greeting.trim();
System.out.println("The trimmed greeting is: " + trimmedGreeting);Output:
The trimmed greeting is: Hello, World!
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! 🎉