1. Common String Methods in Java
The Java String class provides many built-in methods for working with text data.
These methods help developers search, modify, compare, split, and process Strings easily.
2. Why String Methods are Used
String methods reduce manual coding and make text processing faster and simpler.
- a. Process user input
- b. Validate data
- c. Search text
- d. Format messages
- e. Extract information
- f. Manipulate characters
3. Where String Methods are Used
String methods are used in almost every Java application.
- a. Web applications
- b. Form validation systems
- c. Banking applications
- d. Chat systems
- e. File processing
- f. Data parsing applications
4. length() Method
The length() method returns the total number of characters in a String.
4.1 Example of length()
public class Main {
public static void main(String[] args) {
String text = "Java Programming";
System.out.println(text.length());
}
}
The output is 16 because spaces are also counted as characters.
length() returns the number of characters, not the memory size.
5. charAt() Method
The charAt() method returns the character present at a specific index.
5.1 Example of charAt()
public class Main {
public static void main(String[] args) {
String language = "Java";
System.out.println(language.charAt(0));
System.out.println(language.charAt(2));
}
}
The first statement prints J and the second statement prints v.
Using an invalid index causes StringIndexOutOfBoundsException.
6. toUpperCase() Method
The toUpperCase() method converts all characters into uppercase letters.
6.1 Example of toUpperCase()
public class Main {
public static void main(String[] args) {
String city = "Bhubaneswar";
System.out.println(city.toUpperCase());
}
}
The output becomes BHUBANESWAR.
7. toLowerCase() Method
The toLowerCase() method converts all characters into lowercase letters.
7.1 Example of toLowerCase()
public class Main {
public static void main(String[] args) {
String country = "INDIA";
System.out.println(country.toLowerCase());
}
}
The output becomes india.
8. trim() Method
The trim() method removes spaces from the beginning and end of a String.
8.1 Example of trim()
public class Main {
public static void main(String[] args) {
String text = " Java ";
System.out.println(text.trim());
}
}
The spaces before and after the word are removed.
trim() does not remove spaces between words.
9. contains() Method
The contains() method checks whether a String contains specific text.
9.1 Example of contains()
public class Main {
public static void main(String[] args) {
String sentence = "Java is powerful";
System.out.println(sentence.contains("Java"));
System.out.println(sentence.contains("Python"));
}
}
The first result is true and the second result is false.
10. startsWith() and endsWith() Methods
These methods check how a String begins or ends.
10.1 Example of startsWith()
public class Main {
public static void main(String[] args) {
String website = "java.com";
System.out.println(website.startsWith("java"));
}
}
The output is true because the String starts with java.
10.2 Example of endsWith()
public class Main {
public static void main(String[] args) {
String file = "notes.pdf";
System.out.println(file.endsWith(".pdf"));
}
}
The output is true because the file name ends with .pdf.
11. substring() Method
The substring() method extracts a portion of a String.
11.1 substring() with Start Index
When only one index is provided, extraction starts from that index and continues until the end.
public class Main {
public static void main(String[] args) {
String text = "Programming";
System.out.println(text.substring(3));
}
}
The output is gramming because extraction starts from index 3.
11.2 substring() with Start and End Index
The ending index is excluded from the result.
public class Main {
public static void main(String[] args) {
String text = "Programming";
System.out.println(text.substring(0, 7));
}
}
The output is Program.
Providing invalid indexes causes StringIndexOutOfBoundsException.
12. replace() Method
The replace() method replaces characters or text inside a String.
12.1 Replacing Characters
public class Main {
public static void main(String[] args) {
String text = "banana";
System.out.println(text.replace('a', 'o'));
}
}
The output becomes bonono.
12.2 Replacing Words
public class Main {
public static void main(String[] args) {
String message = "I like Java";
System.out.println(message.replace("Java", "Python"));
}
}
The word Java is replaced with Python.
replace() returns a new String because Strings are immutable.
13. split() Method
The split() method divides a String into multiple parts.
13.1 Splitting by Space
public class Main {
public static void main(String[] args) {
String sentence = "Java is easy";
String[] words = sentence.split(" ");
for(String word : words) {
System.out.println(word);
}
}
}
The sentence is divided into separate words.
13.2 Splitting CSV Data
public class Main {
public static void main(String[] args) {
String data = "Apple,Mango,Banana";
String[] fruits = data.split(",");
for(String fruit : fruits) {
System.out.println(fruit);
}
}
}
The comma is used as the separator.
14. indexOf() Method
The indexOf() method returns the position of a character or word.
14.1 Finding Character Position
public class Main {
public static void main(String[] args) {
String word = "Programming";
System.out.println(word.indexOf('g'));
}
}
The output is the index of the first occurrence of g.
14.2 Finding Word Position
public class Main {
public static void main(String[] args) {
String text = "Java Programming";
System.out.println(text.indexOf("Programming"));
}
}
The output is the starting position of the word Programming.
14.3 Character Not Found
public class Main {
public static void main(String[] args) {
String word = "Java";
System.out.println(word.indexOf('x'));
}
}
The output is -1 because the character does not exist.
15. equals() Method
The equals() method compares actual String content.
15.1 Example of equals()
public class Main {
public static void main(String[] args) {
String s1 = "Java";
String s2 = "Java";
System.out.println(s1.equals(s2));
}
}
The output is true because both Strings contain the same text.
16. equalsIgnoreCase() Method
The equalsIgnoreCase() method compares Strings without checking uppercase and lowercase differences.
16.1 Example of equalsIgnoreCase()
public class Main {
public static void main(String[] args) {
String s1 = "JAVA";
String s2 = "java";
System.out.println(s1.equalsIgnoreCase(s2));
}
}
The output is true because case differences are ignored.
The == operator compares references, not actual String content.
17. compareTo() Method
The compareTo() method compares two Strings alphabetically.
17.1 Example of compareTo()
public class Main {
public static void main(String[] args) {
String a = "Apple";
String b = "Banana";
System.out.println(a.compareTo(b));
}
}
The method returns a negative value because Apple comes before Banana alphabetically.
17.2 Equal Strings
public class Main {
public static void main(String[] args) {
String a = "Java";
String b = "Java";
System.out.println(a.compareTo(b));
}
}
The output is 0 because both Strings are equal.
17.3 Greater String
public class Main {
public static void main(String[] args) {
String a = "Zoo";
String b = "Apple";
System.out.println(a.compareTo(b));
}
}
The method returns a positive value because Zoo comes after Apple alphabetically.
18. isEmpty() Method
The isEmpty() method checks whether a String contains zero characters.
18.1 Example of isEmpty()
public class Main {
public static void main(String[] args) {
String text = "";
System.out.println(text.isEmpty());
}
}
The output is true because the String has no characters.
A String containing spaces is not considered empty.
19. join() Method
The join() method combines multiple Strings using a separator.
19.1 Example of join()
public class Main {
public static void main(String[] args) {
String result = String.join("-", "Java", "Python", "C++");
System.out.println(result);
}
}
The output becomes Java-Python-C++.
20. valueOf() Method
The valueOf() method converts other data types into Strings.
20.1 Converting Integer to String
public class Main {
public static void main(String[] args) {
int number = 100;
String text = String.valueOf(number);
System.out.println(text);
}
}
The integer value is converted into a String.
20.2 Converting Boolean to String
public class Main {
public static void main(String[] args) {
boolean status = true;
String result = String.valueOf(status);
System.out.println(result);
}
}
The boolean value is converted into text format.
21. Common Beginner Mistakes
21.1 Using == Instead of equals()
public class Main {
public static void main(String[] args) {
String a = new String("Java");
String b = new String("Java");
System.out.println(a == b);
}
}
The output is false because == compares references.
21.2 Forgetting String Immutability
public class Main {
public static void main(String[] args) {
String text = "java";
text.toUpperCase();
System.out.println(text);
}
}
The original String remains unchanged because the returned value was not stored.
21.3 Invalid Index Usage
public class Main {
public static void main(String[] args) {
String word = "Java";
System.out.println(word.charAt(10));
}
}
This code throws StringIndexOutOfBoundsException because the index does not exist.
22. Important Points to Remember
- a. String methods simplify text processing
- b. Most String methods return new objects
- c. equals() compares content
- d. == compares references
- e. indexOf() returns -1 when data is not found
- f. substring() excludes the ending index
- g. compareTo() performs alphabetical comparison
Java String methods provide powerful tools for handling and processing text efficiently. Understanding how these methods work helps beginners write cleaner, faster, and more professional Java programs while avoiding common String-related mistakes.
