Java Strings and String Methods – Complete Guide with Examples
Learn everything about Java Strings, including creation, manipulation, and commonly used string methods with detailed examples for beginners and advanced learners.
Strings and String Methods in Java – Complete Detailed Tutorial
A String in Java is an object that represents a sequence of characters.
Strings are widely used in text processing, user input, file handling, and data manipulation.
1. Creating Strings in Java
1.1 Using String Literal
String str1 = "Hello World";
- Stored in String Pool
- Reuses memory if same string exists
1.2 Using new Keyword
String str2 = new String("Hello Java");
- Stored in Heap memory
- Creates a new object every time
2. String Immutability
- Strings are immutable, meaning cannot be changed once created
- Any modification creates a new string
Example:
String str = "Hello";
str.concat(" World");
System.out.println(str); // Hello
- Use str = str.concat(" World"); to update the string
3. Commonly Used String Methods
| MethodDescriptionExample | ||
length() | Returns length of string | "Hello".length() → 5 |
charAt(index) | Returns character at index | "Hello".charAt(1) → 'e' |
concat(str) | Concatenates string | "Hi".concat(" Java") → "Hi Java" |
equals(str) | Compares strings | "Hi".equals("Hi") → true |
equalsIgnoreCase(str) | Case-insensitive compare | "hi".equalsIgnoreCase("HI") → true |
toUpperCase() | Converts to uppercase | "hello".toUpperCase() → "HELLO" |
toLowerCase() | Converts to lowercase | "HELLO".toLowerCase() → "hello" |
trim() | Removes leading/trailing spaces | " Java ".trim() → "Java" |
substring(start, end) | Extracts substring | "Hello".substring(1,4) → "ell" |
replace(old, new) | Replaces characters | "Hello".replace('l','p') → "Heppo" |
split(regex) | Splits string into array | "a,b,c".split(",") → ["a","b","c"] |
contains(str) | Checks if string contains substring | "Hello".contains("ll") → true |
startsWith(prefix) | Checks start | "Java".startsWith("Ja") → true |
endsWith(suffix) | Checks end | "Java".endsWith("va") → true |
indexOf(ch) | Returns first index | "Hello".indexOf('l') → 2 |
lastIndexOf(ch) | Returns last index | "Hello".lastIndexOf('l') → 3 |
4. String Comparison
==operator: compares references (memory address)equals()method: compares content
String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");
System.out.println(s1 == s2); // true
System.out.println(s1 == s3); // false
System.out.println(s1.equals(s3)); // true
5. String Concatenation
- Using
+operator:
String s = "Hello" + " Java";
- Using
concat()method:
String s = "Hello".concat(" Java");
6. String Conversion
- String → int
String str = "100";
int num = Integer.parseInt(str);
- int → String
int num = 100;
String str = String.valueOf(num);
- String → double
double d = Double.parseDouble("3.14");
- String → char array
char[] chars = "Hello".toCharArray();
7. StringBuilder and StringBuffer
- Strings are immutable
- For mutable strings, use StringBuilder or StringBuffer
Example:
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
System.out.println(sb); // Hello World
- StringBuffer → thread-safe (synchronized)
- StringBuilder → faster (non-synchronized)
8. Example Program – Strings in Java
public class StringDemo {
public static void main(String[] args) {
String str = " Java Programming ";
System.out.println("Original String: '" + str + "'");
System.out.println("Length: " + str.length());
System.out.println("Trimmed: '" + str.trim() + "'");
System.out.println("Uppercase: " + str.toUpperCase());
System.out.println("Lowercase: " + str.toLowerCase());
System.out.println("Substring (5,16): " + str.substring(5,16));
System.out.println("Replace 'a' with '@': " + str.replace('a','@'));
System.out.println("Contains 'Program': " + str.contains("Program"));
// Splitting string
String[] words = str.trim().split(" ");
System.out.println("Words:");
for(String word : words) {
System.out.println(word);
}
// StringBuilder example
StringBuilder sb = new StringBuilder("Hello");
sb.append(" Java");
System.out.println("StringBuilder: " + sb);
}
}
Sample Output:
Original String: ' Java Programming '
Length: 20
Trimmed: 'Java Programming'
Uppercase: ' JAVA PROGRAMMING '
Lowercase: ' java programming '
Substring (5,16): 'Java Program'
Replace 'a' with '@': ' J@v@ Progr@mming '
Contains 'Program': true
Words:
Java
Programming
StringBuilder: Hello Java
9. Summary
- String → sequence of characters, immutable
- Use methods like
length(),charAt(),substring(),replace(),split() - Compare strings with equals(), not
== - Use StringBuilder/StringBuffer for mutable strings
- Strings are essential for text processing in Java