-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathStringDemo.java
More file actions
162 lines (134 loc) · 5.22 KB
/
StringDemo.java
File metadata and controls
162 lines (134 loc) · 5.22 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
160
161
162
public class StringDemo {
static String s3;//有默认值 null
//字符串基本操作
public static void test1() {
//定义字符串方式一:
String s = "abcd";
System.out.println(s);
//基本类型 int num = 10 ;
//对象类型: Person per = new Person();
//定义字符串方式二:
String s1 = "helloworld";
System.out.println(s1);
//方式三
String s2 = ""; //方式三 默认生成的s2是 ""
System.out.println(s2.equals(""));//“”
System.out.println(s2 == null);//null
System.out.println("s3" + s3);
//常见的String方法
String str = " hello world ";
boolean flag = str.equals("helloWorlD");
//忽略大小写 比较
boolean flag2 = str.equalsIgnoreCase("helloWorlD");
System.out.println(flag);
System.out.println(flag2);
int len = str.length();//字符串长度
System.out.println(len);
//转为大写
str = str.toUpperCase();
System.out.println(str);
//转为小写
str = str.toLowerCase();
System.out.println(str);
//helloworld
//判断 字符串A 是否存在于 另一个字符串B中,如果存在 ,则返回位置;如果不存在,返回-1
//int position = str.indexOf('o') ; //char ->int
int position = str.lastIndexOf('o');
System.out.println(position);
//str
System.out.println(str);
System.out.println("实际长度" + str.length());
str = str.trim();//去掉首尾空格,不会去掉中间的
System.out.println("去掉首尾空格之后的 长度" + str.length());
}
//验证 邮箱是否 合法 [email protected] 15768995.com
public static boolean isValidateEmail(String email) {
//合法情况
return email.indexOf("@") != -1 && email.indexOf(".") != -1 &&
email.indexOf("@") < email.indexOf(".");
}
//校验 电话是否合法
public static void testSubstring() {
//029-88888888
// String phone = "18055555555" ;
//座机要求: 区号是 3或4位 ,右侧 8位
String phone = "029-123456789";
if (phone.indexOf("-") != -1) {
System.out.println("座机号码");
//phone.substring(start,end) : [start,end)
//phone.substring(start) : 从start到最后
// phone = phone.substring(2,6); //>=2 <6
// System.out.println(phone);
// 区号是 3或4位 ,右侧 8位
// 截取区号
int start = 0;
int end = phone.indexOf("-");
// "029" "0931"
String zone = phone.substring(start, end);
if (zone.length() == 3 || zone.length() == 4) {
System.out.println("区号正确");
} else {
System.out.println("区号有误!");
}
//座机的右侧:"029-12345678" ;
int startRight = end + 1;
String numberStr = phone.substring(startRight);
if (numberStr.length() == 8) {
System.out.println("号码正确");
} else {
System.out.println("号码有误!");
}
} else {
System.out.println("手机号码");
}
}
public static void testSubstring2(String phone) {
//029-88888888
// String phone = "18055555555" ;
//座机要求: 区号是 3或4位 ,右侧 8位
// String phone = "029-12345678" ;
if (phone.indexOf("-") != -1) {
System.out.println("座机号码");
int start = 0;
int end = phone.indexOf("-");
// "029" "0931"
String zone = phone.substring(start, end);
//座机的右侧:"029-12345678" ;
int startRight = end + 1;
String numberStr = phone.substring(startRight);
//正确: 区号正确 且 号码正确
if ((zone.length() == 3 || zone.length() == 4) && (numberStr.length() == 8)) {
System.out.println("座机正确");
} else {
System.out.println("座机有误!");
}
} else {
//18888888888
System.out.println("手机号码");
String first = phone.substring(0, 1);
if (first.equals("1") && phone.length() == 11) {
System.out.println("正确的手机号");
} else {
System.out.println("错误的手机号");
}
}
}
public static void testSplit() {
String phone = "029-12345-678";
String[] ps = phone.split("-");
for (String p : ps) {
System.out.println(p);
}
// System.out.println(ps[0]);
// System.out.println(ps[1]);
// System.out.println(ps[2]);
}
//staitc->static
public static void main(String[] args) {
// boolean result = isValidateEmail("[email protected]");
// System.out.println(result==true ? "合法":"不合法" );
// testSubstring();
// testSubstring2("029-54548787");
testSplit();
}
}