Table of contents | |
Creating Strings | |
Concatenating Strings | |
Comparing Strings | |
Manipulating Strings | |
Sample Problems | |
Conclusion |
When programming in Java, one frequently encounters the need to manipulate and work with text data. Java provides a built-in class called String that is specifically designed to handle textual data. The String class is part of the Java Standard Library and offers a wide range of methods for manipulating and working with strings.
In this article, we will explore the various features and capabilities of the String class in Java. We will cover string creation, concatenation, comparison, manipulation, and more. By the end, you will have a solid understanding of how to effectively work with strings in Java.
In Java, you can create a string by enclosing a sequence of characters within double quotes. Here's an example:
String greeting = "Hello, World!";
You can also create an empty string using the empty string literal "":
String emptyString = "";
String concatenation is the process of combining two or more strings together. In Java, you can concatenate strings using the + operator. Here's an example:
String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName;
System.out.println(fullName); // Output: John Doe
You can also use the concat() method to concatenate strings:
String firstName = "John";
String lastName = "Doe";
String fullName = firstName.concat(" ").concat(lastName);
System.out.println(fullName); // Output: John Doe
To compare strings in Java, you can use the equals() method. This method compares the contents of two strings and returns true if they are equal, and false otherwise. Here's an example:
String str1 = "apple";
String str2 = "orange";
String str3 = "apple";
System.out.println(str1.equals(str2)); // Output: false
System.out.println(str1.equals(str3)); // Output: true
To perform a case-insensitive comparison, you can use the equalsIgnoreCase() method:
String str1 = "apple";
String str2 = "Apple";
System.out.println(str1.equalsIgnoreCase(str2)); // Output: true
The String class in Java provides several methods to manipulate strings. Here are a few commonly used methods:
Let's see some examples of these methods in action:
String str = "Hello, World!";
System.out.println(str.length()); // Output: 13
System.out.println(str.charAt(0)); // Output: H
System.out.println(str.substring(7, 12)); // Output: World
System.out.println(str.toUpperCase()); // Output: HELLO, WORLD!
System.out.println(str.toLowerCase()); // Output: hello, world!
Now that we have covered the basics of the String class, let's solve a few sample problems to reinforce our understanding.
Problem 1: Write a program to count the number of vowels in a given string.
String str = "Hello, World!";
int vowelCount = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
vowelCount++;
}
}
System.out.println("Number of vowels: " + vowelCount);
Problem 2: Write a program to check if a given string is a palindrome (reads the same forwards and backwards).
String str = "madam";
boolean isPalindrome = true;
for (int i = 0; i < str.length() / 2; i++) {
if (str.charAt(i) != str.charAt(str.length() - 1 - i)) {
isPalindrome = false;
break;
}
}
if (isPalindrome) {
System.out.println("Palindrome");
} else {
System.out.println("Not a palindrome");
}
In this article, we have explored the String class in Java and learned how to create, concatenate, compare, and manipulate strings. The String class provides a rich set of methods that make working with textual data in Java a breeze. By understanding the various methods and features of the String class, you can efficiently handle and manipulate strings in your Java programs.
60 videos|37 docs|12 tests
|
|
Explore Courses for EmSAT Achieve exam
|