String Wrapper Class
A String is a sequence of characters. In Java, String is a class.
String name = "Moxit";
Important: Strings are immutable. Once created, they cannot be changed.
1. Creating a String
String literal
String greeting = "Hello World";
From a character array
char[] chars = {'H', 'e', 'l', 'l', 'o'};
String text = new String(chars);
System.out.println(text);
Output:
Hello
2. length()
Returns the number of characters.
String text = "Hello";
System.out.println(text.length());
Output:
5
3. charAt()
Returns the character at a specific index.
Index starts from 0.
String text = "Hello";
System.out.println(text.charAt(1));
Output:
e
H e l l o
0 1 2 3 4
4. Concatenation
Using +
String first = "Hello";
String second = "World";
String result = first + " " + second;
System.out.println(result);
Output:
Hello World
Using concat()
String result = "Hello".concat(" World");
System.out.println(result);
5. Text Blocks
Useful for multi-line strings. Available from Java 15.
String html = """
<html>
<body>Hello</body>
</html>
""";
System.out.println(html);
6. String.format()
Creates a formatted string.
String name = "John";
int age = 22;
String result = String.format("Name: %s, Age: %d", name, age);
System.out.println(result);
Output:
Name: John, Age: 22
Common format specifiers:
%s → String
%d → integer
%f → floating-point
7. String → Number
Use parseXXX().
int age = Integer.parseInt("22");
double price = Double.parseDouble("99.5");
System.out.println(age);
System.out.println(price);
Output:
22
99.5
8. Number → String
String.valueOf()
int number = 100;
String text = String.valueOf(number);
System.out.println(text);
toString()
int number = 100;
String text = Integer.toString(number);
9. substring()
Gets part of a string.
String text = "Hello World";
String result = text.substring(6, 11);
System.out.println(result);
Output:
World
Remember:
substring(start, end)→endis exclusive.
You can also use:
String result = text.substring(6);
This gets everything from index 6 to the end.
10. split()
Splits a string into an array.
String text = "Java,Python,JavaScript";
String[] languages = text.split(",");
for (String language : languages) {
System.out.println(language);
}
Output:
Java
Python
JavaScript
11. trim()
Removes whitespace from the beginning and end.
String text = " Hello ";
System.out.println(text.trim());
Output:
Hello
For modern Unicode-aware whitespace handling,
strip()is generally preferred overtrim().
12. toLowerCase() and toUpperCase()
String text = "Hello World";
System.out.println(text.toLowerCase());
System.out.println(text.toUpperCase());
Output:
hello world
HELLO WORLD
13. indexOf()
Finds the first occurrence.
String text = "Hello World";
System.out.println(text.indexOf("o"));
Output:
4
Returns -1 if not found.
14. lastIndexOf()
Finds the last occurrence.
String text = "Hello World";
System.out.println(text.lastIndexOf("o"));
Output:
7
15. contains()
Checks whether a string contains another string.
String text = "Hello World";
System.out.println(text.contains("World"));
Output:
true
16. replace()
Replaces characters or strings.
Character replacement
String text = "Hello";
String result = text.replace('l', 'x');
System.out.println(result);
Output:
Hexxo
String replacement
String text = "I like Java";
String result = text.replace("Java", "Python");
System.out.println(result);
Output:
I like Python
17. subSequence()
Returns a part of the string as a CharSequence.
String text = "Hello World";
CharSequence result = text.subSequence(0, 5);
System.out.println(result);
Output:
Hello
18. Important: Strings Are Immutable
String methods do not change the original string.
String text = "Hello";
text.toUpperCase();
System.out.println(text);
Output:
Hello
You need to store the returned String:
text = text.toUpperCase();
System.out.println(text);
Output:
HELLO
Think of it as:
String
↓
Cannot be changed
↓
Methods create a new String
Quick Revision
| Method | Purpose |
|---|---|
length() |
Gets length |
charAt() |
Gets character |
+ / concat() |
Joins strings |
String.format() |
Formats string |
parseInt() |
String → int |
parseDouble() |
String → double |
String.valueOf() |
Value → String |
substring() |
Gets part of string |
split() |
String → String array |
trim() |
Removes surrounding basic whitespace |
strip() |
Removes Unicode-aware surrounding whitespace |
toLowerCase() |
Converts to lowercase |
toUpperCase() |
Converts to uppercase |
indexOf() |
Finds first occurrence |
lastIndexOf() |
Finds last occurrence |
contains() |
Checks if text exists |
replace() |
Replaces text/characters |
subSequence() |
Gets part as CharSequence |
Most important for DSA
Focus on:
length()
charAt()
substring()
indexOf()
contains()
split()
replace()
toLowerCase()
toUpperCase()
One rule to remember
Stringis immutable, so string operations return a newStringinstead of modifying the original.