Chapter 3: Strings in Java
Introduction to Strings
A string is a sequence of characters.
String is a class but can be used like a data type.
Strings are immutable and cannot be changed.
String Creation
Method 1: Using new keyword
String name;
name = new String("Harry");
Method 2: Direct assignment (Recommended)
String name = "Harry";
Different Ways to Print in Java
1. System.out.print()
No newline at the end
System.out.print("Hello ");
System.out.print("World");
// Output: Hello World
2. System.out.println()
Prints a newline at the end
System.out.println("Hello");
System.out.println("World");
// Output:
// Hello
// World
3. System.out.printf()
Formatted printing
System.out.printf("Name: %s, Age: %d", "John", 25);
// Output: Name: John, Age: 25
Format Specifiers:
%d for int%f for float%c for char%s for string4. System.out.format()
Same as printf()
String Methods
String methods operate on Java Strings. They can be used to find length, convert to lowercase, etc.
String name = "Harry";
// 01234 (index positions)
1. length()
Returns length of String
name.length() // Returns 5
2. toLowerCase()
Returns new String with lowercase characters
name.toLowerCase() // Returns "harry"
3. toUpperCase()
Returns new String with uppercase characters
name.toUpperCase() // Returns "HARRY"
4. trim()
Removes leading and trailing spaces
String str = " Harry ";
str.trim() // Returns "Harry"
5. substring(int start)
Returns substring from start to end
name.substring(3) // Returns "ry"
6. substring(int start, int end)
Returns substring from start to end (exclusive)
name.substring(1, 4) // Returns "arr"
7. replace(char old, char new)
Returns new string after replacing characters
name.replace('r', 'p') // Returns "Happy"
8. startsWith(String prefix)
Returns true if string starts with prefix
name.startsWith("Ha") // Returns true
9. endsWith(String suffix)
Returns true if string ends with suffix
name.endsWith("ry") // Returns true
10. charAt(int index)
Returns character at given index
name.charAt(2) // Returns 'r'
11. indexOf(String str)
Returns index of first occurrence
name.indexOf("ar") // Returns 1
name.indexOf("z") // Returns -1
12. equals(String str)
Returns true if strings are equal (case sensitive)
name.equals("Harry") // Returns true
name.equals("harry") // Returns false
Escape Sequence Characters
Sequence of characters after backslash '\' = Escape sequence characters.
Escape sequence characters consist of more than one character but represent one character when used within strings.
| Escape Sequence | Description | Example |
|---|---|---|
\n |
Newline | "Hello\nWorld" |
\t |
Tab | "Hello\tWorld" |
\' |
Single quote | "It\'s working" |
\" |
Double quote | "He said \"Hello\"" |
\\ |
Backslash | "C:\\Users\\Name" |
Practice Set
1. Convert string to lowercase
public class StringLowercase {
public static void main(String[] args) {
String str = "HELLO WORLD";
System.out.println(str.toLowerCase());
}
}
2. Replace spaces with underscores
public class ReplaceSpaces {
public static void main(String[] args) {
String str = "Hello World Java";
String result = str.replace(' ', '_');
System.out.println(result); // Hello_World_Java
}
}
3. Fill letter template
public class LetterTemplate {
public static void main(String[] args) {
String letter = "Dear <|name|>, Thanks a lot";
String name = "Harry";
String result = letter.replace("<|name|>", name);
System.out.println(result);
}
}
Chapter 3 - Practice Set
1. String to Lowercase
Write a Java program to convert a string to lowercase.
2. Replace Spaces
Write a Java program to replace spaces with underscores.
3. Letter Template
Write a Java program to fill in a letter template which looks like: letter = "Dear <|name|>, Thanks a lot". Replace <|name|> with a string (some name).
4. Detect Multiple Spaces
Write a Java program to detect double and triple spaces in a string.
5. Format with Escape Sequences
Write a program to format the following letter using escape sequence characters: letter = "Dear Harry, This Java Course is nice. Thanks"