0% found this document useful (0 votes)
3 views2 pages

Java String Methods CheatSheet

The document provides an overview of various Java String methods, including length and character access, comparison, searching, substrings, case conversion, trimming, splitting, conversion, interning, and formatting. Each section includes code examples demonstrating the usage of these methods. This serves as a comprehensive guide for understanding and utilizing Java String functionalities.
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)
3 views2 pages

Java String Methods CheatSheet

The document provides an overview of various Java String methods, including length and character access, comparison, searching, substrings, case conversion, trimming, splitting, conversion, interning, and formatting. Each section includes code examples demonstrating the usage of these methods. This serves as a comprehensive guide for understanding and utilizing Java String functionalities.
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/ 2

Java String Methods with Examples

1. Length and Character Access


String str = "Hello World";
System.out.println(str.length()); // 11
System.out.println(str.charAt(0)); // 'H'
System.out.println(str.codePointAt(1)); // 101 (Unicode of 'e')

2. Comparison
String a = "Hello";
String b = "hello";
System.out.println(a.equals(b)); // false
System.out.println(a.equalsIgnoreCase(b)); // true
System.out.println(a.compareTo(b)); // -32
System.out.println(a.compareToIgnoreCase(b)); // 0

3. Searching
String str = "Java programming";
System.out.println(str.contains("gram")); // true
System.out.println(str.indexOf("a")); // 1
System.out.println(str.lastIndexOf("a")); // 13
System.out.println(str.startsWith("Java")); // true
System.out.println(str.endsWith("ing")); // true

4. Substrings and Replacement


String str = "Hello Java";
System.out.println(str.substring(6)); // "Java"
System.out.println(str.substring(0, 5)); // "Hello"
System.out.println(str.replace('a', 'o')); // "Hello Jovo"
System.out.println(str.replaceAll("a", "@")); // "Hello J@v@"
System.out.println(str.replaceFirst("a", "@"));// "Hello J@va"

5. Case Conversion
String str = "HeLLo";
System.out.println(str.toLowerCase()); // "hello"
System.out.println(str.toUpperCase()); // "HELLO"

6. Trimming and Empty Checks


String str = " Hello ";
String emptyStr = "";
Java String Methods with Examples

System.out.println(str.trim()); // "Hello"
System.out.println(emptyStr.isEmpty()); // true
System.out.println(" ".isBlank()); // true (Java 11+)

7. Splitting and Joining


String names = "John,Jane,Jim";
String[] arr = names.split(",");
for(String name : arr) {
System.out.println(name); // John
Jane
Jim
}
String joined = String.join("-", arr);
System.out.println(joined); // "John-Jane-Jim"

8. Conversion
String str = "Java";
char[] chars = str.toCharArray(); // ['J','a','v','a']
System.out.println(String.valueOf(123)); // "123"
byte[] bytes = str.getBytes();
System.out.println(Arrays.toString(bytes)); // Byte values

9. Interning and Identity


String a = new String("test");
String b = a.intern();
String c = "test";
System.out.println(a == c); // false
System.out.println(b == c); // true

10. Formatting
String name = "Vinoth";
int age = 21;
String formatted = String.format("Name: %s, Age: %d", name, age);
System.out.println(formatted); // "Name: Vinoth, Age: 21"

You might also like