Home | Computer Science
String
Definition
String functions in Java allow for manipulation of string data, while escape characters are used to include special characters in strings.
String Functions in Java
Common string functions:
- toLowerCase() : Converts a string to lowercase.
- toUpperCase() : Converts a string to uppercase.
- length() : Returns the length of a string.
- indexOf() : Finds the position of the first occurrence of a substring.
- concat() : Concatenates two strings.
Escape Characters in Java
Common escape characters:
- \' : Single quote.
- \" : Double quote.
- \\ : Backslash.
- \n : New Line.
- \t : Tab.
- \b : Backspace.
Program
Input:
// Java program demonstrating string functions and escape characters
public class StringFunctionsExample {
public static void main(String[] args) {
String str = "Hello World";
// String Functions
System.out.println("Original string: " + str);
System.out.println("Lowercase: " + str.toLowerCase());
System.out.println("Uppercase: " + str.toUpperCase());
System.out.println("Length: " + str.length());
System.out.println("Position of 'World': " + str.indexOf("World"));
String str2 = " Goodbye";
System.out.println("Concatenated string: " + str.concat(str2));
// Escape Characters
System.out.println("He said, \"Hello!\"");
System.out.println("I\'m here");
System.out.println("This is a new line\nAnd this is another line");
}
}
OUTPUT