Java String Methods - Detailed Overview
1. String Creation
• String s1 = "Hello"; // String literal
• String s2 = new String("Hello"); // Using new keyword
2. String Length
• length() → Returns number of characters in the string.
String str = "Java";
System.out.println(str.length()); // 4
3. Character Access
• charAt(int index) → Returns character at given index.
String s = "Java";
System.out.println(s.charAt(2)); // v
4. Substring
• substring(int beginIndex)
• substring(int beginIndex, int endIndex)
String s = "HelloWorld";
System.out.println(s.substring(5)); // World
System.out.println(s.substring(0, 5)); // Hello
5. String Comparison
• equals(String str)
• equalsIgnoreCase(String str)
1
• compareTo(String str)
• compareToIgnoreCase(String str)
String a = "Java", b = "java";
System.out.println(a.equals(b)); // false
System.out.println(a.equalsIgnoreCase(b));// true
System.out.println(a.compareTo(b)); // -32
6. Searching in Strings
• contains(CharSequence s)
• startsWith(String prefix)
• endsWith(String suffix)
• indexOf(String s)
• lastIndexOf(String s)
String s = "Programming";
System.out.println(s.contains("gram")); // true
System.out.println(s.startsWith("Pro")); // true
System.out.println(s.endsWith("ing")); // true
System.out.println(s.indexOf("g")); // 3
System.out.println(s.lastIndexOf("g")); // 10
7. Case Conversion
• toUpperCase()
• toLowerCase()
String s = "Java";
System.out.println(s.toUpperCase()); // JAVA
System.out.println(s.toLowerCase()); // java
8. Trim & Strip
• trim() → Removes leading and trailing spaces.
• strip() → Removes all Unicode whitespaces.
2
String s = " Hello World ";
System.out.println(s.trim()); // "Hello World"
9. Replace
• replace(char oldChar, char newChar)
• replace(CharSequence target, CharSequence replacement)
• replaceAll(String regex, String replacement)
• replaceFirst(String regex, String replacement)
String s = "Java Programming";
System.out.println(s.replace("Java", "C++")); // C++ Programming
System.out.println(s.replaceAll("a", "@")); // J@v@ Progr@mming
10. Splitting & Joining
• split(String regex)
• join(CharSequence delimiter, elements...)
String s = "a,b,c";
String[] arr = s.split(",");
for(String x : arr) System.out.println(x); // a b c
String joined = String.join("-", "Java", "Python", "C++");
System.out.println(joined); // Java-Python-C++
11. Value Conversion
• valueOf(int/float/boolean/char/…)
• toCharArray()
int num = 100;
String s = String.valueOf(num);
System.out.println(s + 50); // 10050
3
12. Checking Empty or Blank
• isEmpty()
• isBlank() (Java 11+)
String s1 = "";
System.out.println(s1.isEmpty()); // true
System.out.println(" ".isBlank()); // true
13. Interning
• intern() → Stores string in string pool.
String s1 = new String("Hello");
String s2 = "Hello";
System.out.println(s1 == s2); // false
System.out.println(s1.intern() == s2); // true
14. Formatting
• format(String format, Object... args)
String result = String.format("Name: %s, Age: %d", "Arun", 25);
System.out.println(result); // Name: Arun, Age: 25