0% found this document useful (0 votes)
20 views4 pages

A 4

The document discusses common String methods in Java like length(), charAt(), substring(), toUpperCase(), toLowerCase(), and replace(). It provides examples of how to use each method. It also presents a program to check if a given string is a palindrome using two pointer variables and comparing characters from both ends. The program removes whitespace, converts to lowercase, and returns true if all characters match or false otherwise.

Uploaded by

Mohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

A 4

The document discusses common String methods in Java like length(), charAt(), substring(), toUpperCase(), toLowerCase(), and replace(). It provides examples of how to use each method. It also presents a program to check if a given string is a palindrome using two pointer variables and comparing characters from both ends. The program removes whitespace, converts to lowercase, and returns true if all characters match or false otherwise.

Uploaded by

Mohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Certainly!

Here are some commonly used methods of the String class in Java, along with
examples:

1. `length()`: Returns the length of the string.

String str = "Hello World";


int length = str.length(); // length will be 11

2. `charAt(index)`: Returns the character at the specified index.


String str = "Hello";
char ch = str.charAt(1); // ch will be

3. `substring(startIndex, endIndex)`: Returns a new string that is a substring of the original string,
starting from startIndex (inclusive) and ending at endIndex (exclusive).

String str = "Hello World";


String substr = str.substring(6, 11); // substr will be "World"

4. `toUpperCase()`: Converts all characters in the string to uppercase.


String str = "hello";
String uppercaseStr = str.toUpperCase(); // uppercaseStr will be "HELLO"

5. `toLowerCase()`: Converts all characters in the string to lowercase.


String str = "HELLO";
String lowercaseStr = str.toLowerCase(); // lowercaseStr will be "hello"
6. `replace(oldChar, newChar)` or `replace(oldString, newString)`: Replaces occurrences of a
specified character or string with another character or string.
String str = "Hello World";
String newStr = str.replace('o', '0'); // newStr will be "Hell0 W0rld"

String str2 = "Hello World";


String newStr2 = str2.replace("World", "Universe"); // newStr2 will be "Hello Universe"

These are just a few examples of the many methods available in the String class. Each method
serves a specific purpose and allows you to manipulate and work with strings effectively in
Java

Here's a simple Java program to check if a given string is a palindrome::-


public class PalindromeChecker {
public static boolean isPalindrome(String str) {
// Remove whitespace and convert to lowercase
String processedStr = str.replaceAll("\\s+", "").toLowerCase();

// Check if the string is a palindrome


int left = 0;
int right = processedStr.length() - 1;

while (left < right) {


if (processedStr.charAt(left) != processedStr.charAt(right)) {
return false;
}

left++;
right--;
}

return true;
}

public static void main(String[] args) {


String input = "level";

if (isPalindrome(input)) {
System.out.println("The string is a palindrome.");
} else {
System.out.println("The string is not a palindrome.");
}
}
}

In this program, the `isPalindrome` method takes a string as input and checks if it is a
palindrome. It removes any whitespace and converts the string to lowercase to ignore case
sensitivity. Then, it uses two pointers (`left` and `right`) to compare characters from the
beginning and end of the string, moving towards the center. If any pair of characters doesn't
match, the method returns `false`. If all characters match, it returns `true`.
In the `main` method, an example input string "level" is used to demonstrate the usage of the
`isPalindrome` method. The program prints either "The string is a palindrome." or "The string is
not a palindrome." based on the result of the palindrome check.

You might also like