commonly used String methods in Java,

Here's a complete list of commonly used String methods in Java, along with what they do and how to use them in programming:


✅ 1. length()

Returns the number of characters in the string.

String str = "Java";
System.out.println(str.length());  // 4

✅ 2. charAt(int index)

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

System.out.println(str.charAt(1));  // 'a'

✅ 3. substring(int startIndex)

Returns substring from given index to end.

System.out.println(str.substring(1));  // "ava"

✅ 4. substring(int start, int end)

Returns substring from start to (end - 1).

System.out.println(str.substring(1, 3));  // "av"

✅ 5. indexOf(char/String)

Finds the index of first occurrence of character/substring.

System.out.println(str.indexOf('a'));    // 1

✅ 6. lastIndexOf(char/String)

Finds the index of last occurrence of character/substring.

System.out.println(str.lastIndexOf('a'));  // 3

✅ 7. toLowerCase() / toUpperCase()

Converts the string to all lowercase or uppercase.

System.out.println(str.toLowerCase());  // "java"
System.out.println(str.toUpperCase());  // "JAVA"

✅ 8. trim()

Removes leading and trailing white spaces.

String s = "  hello  ";
System.out.println(s.trim());  // "hello"

✅ 9. equals(String anotherString)

Checks if two strings are exactly equal (case-sensitive).

System.out.println("java".equals("Java"));  // false

✅ 10. equalsIgnoreCase(String another)

Checks if two strings are equal, ignoring case.

System.out.println("java".equalsIgnoreCase("Java"));  // true

✅ 11. contains(String)

Checks if the string contains a given sequence.

System.out.println(str.contains("av"));  // true

✅ 12. startsWith() / endsWith()

Checks if string starts or ends with the given substring.

System.out.println(str.startsWith("Ja"));  // true
System.out.println(str.endsWith("va"));    // true

✅ 13. replace(char old, char new)

Replaces all occurrences of a character with another.

System.out.println(str.replace('a', 'o'));  // "Jovo"

✅ 14. replaceAll(String regex, String replacement)

Replaces all substrings matching regex.

String s = "one1two2three3";
System.out.println(s.replaceAll("\\d", ""));  // "onetwothree"

✅ 15. split(String regex)

Splits the string by the given delimiter (regex).

String text = "java,selenium,testng";
String[] arr = text.split(",");

✅ 16. isEmpty() / isBlank() (Java 11+)

  • isEmpty() → true if length == 0
  • isBlank() → true if empty or only spaces (Java 11+)
"".isEmpty();       // true
"   ".isBlank();    // true

✅ 17. concat(String)

Adds another string at the end.

System.out.println("Hello".concat(" World"));  // "Hello World"

✅ 18. compareTo(String)

Lexicographical comparison (returns +, 0, - values).

System.out.println("apple".compareTo("banana"));  // negative

✅ 19. toCharArray()

Converts the string to a character array.

char[] chars = "abc".toCharArray();

✅ 20. valueOf(anyType)

Converts any data type to string.

String s = String.valueOf(123);  // "123"

๐Ÿ”„ Summary Table

Method Purpose
length() Get string length
charAt(i) Get character at position
substring() Extract part of string
indexOf() First position of character
lastIndexOf() Last position of character
toUpperCase() Convert to uppercase
toLowerCase() Convert to lowercase
trim() Remove extra spaces
equals() Compare strings (case-sensitive)
equalsIgnoreCase() Compare strings ignoring case
contains() Check if string contains substring
replace() Replace characters
replaceAll() Replace using regex
split() Split by delimiter
isEmpty() Check if string is empty
isBlank() Check if string is blank
concat() Join strings
compareTo() Compare lexicographically
toCharArray() Convert string to char array
valueOf() Convert value to string


Comments

Popular posts from this blog

10 automation test cases for https://www.saucedemo.com/ (Sauce Demo)

Java Roadmap for SDET Superstars!

Mastering XPath in Selenium 4 ๐Ÿš€ – Supported Functions & Axes Explained