[Go to site: main page, start]

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

String Methods in Java

The document provides a comprehensive list of string methods in Java, detailing their functionalities with examples. Key methods include length(), charAt(), substring(), equals(), and others, which are essential for text manipulation. It emphasizes that strings are immutable and highlights common methods useful for text processing and formatting.

Uploaded by

ktanish791
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)
2 views4 pages

String Methods in Java

The document provides a comprehensive list of string methods in Java, detailing their functionalities with examples. Key methods include length(), charAt(), substring(), equals(), and others, which are essential for text manipulation. It emphasizes that strings are immutable and highlights common methods useful for text processing and formatting.

Uploaded by

ktanish791
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

String Methods in Java (Complete List with Examples)

Strings in Java are objects of the String class, and the class provides many
built-in methods for text manipulation. Let’s go through the most important
ones step by step.

1. length()

Returns the number of characters in the string.

String s = "Tanish";
[Link]([Link]()); // Output: 6

2. charAt(int index)

Returns the character at the given index (0-based).

String s = "Hello";
[Link]([Link](1)); // Output: e

3. substring(int start, int end)

Extracts part of the string.

String s = "Programming";
[Link]([Link](0, 6)); // Output:
Progra

4. equals(String another)

Compares two strings (case-sensitive).

String s1 = "Java";
String s2 = "Java";
[Link]([Link](s2)); // Output: true

5. equalsIgnoreCase(String another)
Compares two strings ignoring case.

String s1 = "JAVA";
String s2 = "java";
[Link]([Link](s2)); //
Output: true

6. compareTo(String another)

Compares strings lexicographically.

String s1 = "Apple";
String s2 = "Banana";
[Link]([Link](s2)); // Output: -1
(Apple < Banana)

7. toUpperCase() / toLowerCase()

Converts string to uppercase or lowercase.

String s = "Java";
[Link]([Link]()); // Output: JAVA
[Link]([Link]()); // Output: java

8. trim()

Removes leading and trailing spaces.

String s = " Hello World ";


[Link]([Link]()); // Output: Hello World

9. startsWith() / endsWith()

Checks if string starts or ends with given substring.

String s = "Programming";
[Link]([Link]("Pro")); // true
[Link]([Link]("ing")); // true
10. contains()

Checks if string contains a sequence.

String s = "Hello Java";


[Link]([Link]("Java")); // true

11. indexOf() / lastIndexOf()

Finds position of character or substring.

String s = "Banana";
[Link]([Link]("a")); // 1
[Link]([Link]("a")); // 5

12. replace()

Replaces characters or substrings.

String s = "Hello World";


[Link]([Link]("World", "Java")); //
Output: Hello Java

13. split()

Splits string into array based on delimiter.

String s = "apple,banana,grape";
String[] fruits = [Link](",");
for(String f : fruits) {
[Link](f);
}

Output:

apple
banana
grape

14. isEmpty()
Checks if string is empty.

String s = "";
[Link]([Link]()); // true

15. valueOf()

Converts other types to string.

int num = 100;


String s = [Link](num);
[Link](s); // Output: "100"

16. concat()

Joins two strings.

String s1 = "Hello";
String s2 = "World";
[Link]([Link](" " + s2)); // Output:
Hello World

Key Points for Exams

• Strings are immutable.


• Common methods: length(), charAt(), substring(),
equals(), toUpperCase(), trim(), replace(), split().
• Useful for text processing, searching, and formatting.

Tanish, since you asked for all methods with examples, I’ve covered the
most frequently used and exam-relevant ones. Do you want me to also
prepare a summary table (method → purpose → example → output) for
quick revision before exams?

You might also like