-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstSample.java
More file actions
54 lines (46 loc) · 1.84 KB
/
FirstSample.java
File metadata and controls
54 lines (46 loc) · 1.84 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
package FirstSample;
/**
* This the first sample program in Core Java Chapter 3
*
* @author Gary Cornell
* @version 1.01 1997-03022
*/
public class FirstSample {
public static void main(String[] args) {
String greeting = "Hello";
int length = greeting.length();
char first = greeting.charAt(0); // first is 'H'
char last = greeting.charAt(4); // last is '0'
// 得到实际的长度,即码点数量
int cpCount = greeting.codePointCount(0, greeting.length());
System.out.println("We will not use 'Hello World'");
System.out.println(String.join(" / ", "S", "M", "L", "XL"));
System.out.println(greeting.substring(0, 3) + "p!");
System.out.println("Hello".equals(greeting));
System.out.println("Hello".equalsIgnoreCase("hello"));
System.out.println(greeting.compareTo("Hello") == 1);
System.out.println("length = " + length
+ ", fist char is " + first
+ ", last char is " + last);
System.out.println(cpCount);
// 得到第 i 个码点
for (int i = 0; i < length; i++) {
int index = greeting.offsetByCodePoints(0, i);
int cp = greeting.codePointAt(index);
System.out.print(cp + " / ");
}
System.out.println();
int[] codePoints = greeting.codePoints().toArray();
String str = new String(codePoints, 0, codePoints.length);
for (int i = 0; i < length; i++)
System.out.print(codePoints[i] + " / ");
System.out.println("\n" + str);
char ch = ' ';
StringBuilder builder = new StringBuilder();
builder.append(ch);
builder.append(str);
System.out.println(ch + str);
String completedString = builder.toString();
System.out.println(completedString);
}
}