-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString.java
More file actions
159 lines (91 loc) · 6.24 KB
/
String.java
File metadata and controls
159 lines (91 loc) · 6.24 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import java.nio.charset.Charset;
import java.util.*;
public class first {
public static void main(String[] args) {
String str = "Hello, World!";
char[] charArray = {'J', 'a', 'v', 'a'};
StringBuffer stringBuffer = new StringBuffer("StringBuffer");
System.out.println("charAt(1): " + str.charAt(1));
System.out.println("codePointAt(1): " + str.codePointAt(1));
System.out.println("codePointBefore(1): " + str.codePointBefore(1));
System.out.println("codePointCount(0, 5): " + str.codePointCount(0, 5));
System.out.println("compareTo(): " + str.compareTo("Hello"));
System.out.println("compareToIgnoreCase(): " + str.compareToIgnoreCase("hello, world!"));
System.out.println("concat():" + str.concat("!!"));
System.out.println("contains: " + str.contains("World"));
System.out.println("contentEquals(): " + str.contentEquals("Hello, World!"));
System.out.println("contentEquals(new StringBuffer()): " + str.contentEquals(new StringBuffer("Hello, World!")));
System.out.println("copyValueOf(charArray): " + String.copyValueOf(charArray));
System.out.println("copyValueOf(charArray, 1, 2): " + String.copyValueOf(charArray, 1, 2));
System.out.println("endsWith(): " + str.endsWith("!"));
System.out.println("equals(): " + str.equals("Hello, World!"));
System.out.println("equalsIgnoreCase(): " + str.equalsIgnoreCase("hello, world!"));
// static String format(Locale l, String format, Object... args)
System.out.println("format(): " + String.format(Locale.US, "Hello %s", "World"));
System.out.println("format(): " + String.format("Hello %s", "World"));
System.out.println("getBytes(): " + new String(str.getBytes()));
System.out.println("getBytes(Charset.forName(\"UTF-8\")): " + new String(str.getBytes(Charset.forName("UTF-8"))));
try {
System.out.println("getBytes(\"UTF-8\"): " + new String(str.getBytes("UTF-8")));
} catch (Exception e) {
e.printStackTrace();
}
char[] dst = new char[5];
str.getChars(0, 5, dst, 0);
System.out.println("getChars(0, 5, dst, 0): " + new String(dst));
System.out.println("hashCode(): " + str.hashCode());
System.out.println("indexOf('o'): " + str.indexOf('o'));
System.out.println("indexOf('o', 5): " + str.indexOf('o', 5));
System.out.println("indexOf(\"World\"): " + str.indexOf("World"));
System.out.println("indexOf(\"World\", 5): " + str.indexOf("World", 5));
System.out.println("intern(): " + str.intern());
System.out.println("isEmpty(): " + str.isEmpty());
System.out.println("join(): " + String.join(", ", "one", "two", "three"));
System.out.println("join(): " + String.join(", ", List.of("one", "two", "three")));
System.out.println("lastIndexOf('o'): " + str.lastIndexOf('o'));
System.out.println("lastIndexOf('o', 5): " + str.lastIndexOf('o', 5));
System.out.println("lastIndexOf(\"World\"): " + str.lastIndexOf("World"));
System.out.println("lastIndexOf(\"World\", 5): " + str.lastIndexOf("World", 5));
System.out.println("length(): " + str.length());
System.out.println("matches(): " + str.matches(".World."));
// int offsetByCodePoints(int index, int codePointOffset)
System.out.println("offsetByCodePoints(0, 5): " + str.offsetByCodePoints(0, 5));
// boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
System.out.println("regionMatches(true, 0, \"hello,\", 0, 5): " + str.regionMatches(true, 0, "hello,", 0, 5));
// boolean regionMatches(int toffset, String other, int ooffset, int len)
System.out.println("regionMatches(0, Hello, 0, 5): " + str.regionMatches(0, "Hello,", 0, 5));
// String replace(char oldChar, char newChar)
System.out.println("replace('o', 'O'): " + str.replace('o', 'O'));
// String replace(CharSequence target, CharSequence replacement)
System.out.println("replace(World, Java): " + str.replace("World", "Java"));
// String replaceAll(String regex, String replacement)
System.out.println("replaceAll(World, Java): " + str.replaceAll("World", "Java"));
// String replaceFirst(String regex, String replacement)
System.out.println("replaceFirst(World, Java): " + str.replaceFirst("World", "Java"));
String[] splitStr = str.split(",");
System.out.println("split(,): " + String.join("|", splitStr));
String[] splitStrLimit = str.split(",", 2);
System.out.println("split(\",\", 2): " + String.join("|", splitStrLimit));
System.out.println("startsWith(hello): " + str.startsWith("Hello"));
System.out.println("startsWith(World, 7): " + str.startsWith("World", 7));
System.out.println("subSequence(0, 5): " + str.subSequence(0, 5));
System.out.println("substring(7): " + str.substring(7));
System.out.println("substring(0, 5): " + str.substring(0, 5));
System.out.println("toCharArray(): " + new String(str.toCharArray()));
System.out.println("toLowerCase(): " + str.toLowerCase());
System.out.println("toLowerCase(Locale.US): " + str.toLowerCase(Locale.US));
System.out.println("toString(): " + str.toString());
System.out.println("toUpperCase(): " + str.toUpperCase());
System.out.println("toUpperCase(Locale.US): " + str.toUpperCase(Locale.US));
System.out.println("trim(): " + " Hello, World! ".trim());
System.out.println("valueOf(true): " + String.valueOf(true));
System.out.println("valueOf('a'): " + String.valueOf('a'));
System.out.println("valueOf(charArray): " + String.valueOf(charArray));
System.out.println("valueOf(charArray, 1, 2): " + String.valueOf(charArray, 1, 2));
System.out.println("valueOf(1.23): " + String.valueOf(1.23));
System.out.println("valueOf(1.23f): " + String.valueOf(1.23f));
System.out.println("valueOf(123): " + String.valueOf(123));
System.out.println("valueOf(123L): " + String.valueOf(123L));
System.out.println("valueOf(new Object()): " + String.valueOf(new Object()));
}
}