Java: Strings
String Methods:
String txt = "Hello World"; System.out.println(txt.toUpperCase()); // Outputs "HELLO WORLD" System.out.println(txt.toLowerCase()); // Outputs "hello world"
String txt = "Please locate where 'locate' occurs!"; System.out.println(txt.indexOf("locate")); // Outputs 7 System.out.println(txt.indexOf("p")); // Outputs 0 System.out.println(txt.indexOf("l")); // Outputs 1
String firstName = "John "; String lastName = "Doe"; System.out.println(firstName.concat(lastName));
String x = "10"; String y = "20"; String z = x + y; // z will be 1020 (a String)
String x = "10"; int y = 20; String z = x + y; // z will be 1020 (a String)
Special Characters
| Escape character | Result | Description |
|---|---|---|
| \' | ' | Single quote |
| \" | " | Double quote |
| \\ | \ | Backslash |
| Code | Result | |
|---|---|---|
| \n | New Line |
String txt = "Hello\nWorld!"; // Outputs Hello |
| \r | Carriage Return |
String txt = "Hello\rWorld!"; // Outputs Hello |
| \t | Tab |
String txt = "Hello\tWorld!"; // Outputs Hello World! |
| \b | Backspace |
String txt = "Hel\blo World!"; // Outputs Helo World! |

浙公网安备 33010602011771号