-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringTest.java
More file actions
27 lines (23 loc) · 1.55 KB
/
Copy pathStringTest.java
File metadata and controls
27 lines (23 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package JavaStrings;
public class StringTest {
public static void main(String[] args) {
String str = "This is a test.";
// Some String methods:
System.out.println("Length of str: " + str.length());
System.out.println("The character at position 3: " + str.charAt(3));
System.out.println("The substring from 4 to 9: " + str.substring(4, 9));
System.out.println("The index of the first 's': " + str.indexOf('s'));
System.out.println("The index of the beginning of the " + "substring \"test\": " + str.indexOf("test"));
System.out.println("The string in uppercase: " + str.toUpperCase());
System.out.println("The string in lowercase: " + str.toLowerCase());
System.out.println("Does the string start with \"This\"? " + str.startsWith("This"));
System.out.println("Does the string end with \"test.\"? " + str.endsWith("test."));
System.out.println("Does the string contain \"is\"? " + str.contains("is"));
System.out.println("Replace 'a' with 'X': " + str.replace('a', 'X'));
System.out.println("Replace \"test\" with \"toast\": " + str.replace("test", "toast"));
System.out.println("Remove leading and trailing whitespace: " + (" " + str + " ").trim());
// Strings Regex Methods:
// System.out.println("Remove leading whitespace: " + (" " + str + " ").trim().replaceFirst("^\\s+", ""));
// System.out.println("Remove trailing whitespace: " + (" " + str + " ").trim().replaceFirst("\\s+$", ""));
}
}