String特性 null测试 split测试 正则表达式使用
- 2016-04-15 10:28:00
- admin
- 原创 1357
一、String特性
1、不支持多行字符串;
2、实现CharSequence只读字符序列接口;
3、replace替换所有字符串;
4、replaceAll替换所有正则表达式;
5、intern返回字符串池的等值对象;
二、null测试
public static void testNull() {
String str = null;
System.out.println(str);
System.out.println("string is " + str);
System.out.println(str + " is string");
System.out.println(String.format("format string %s", str));
str = "feinen";
System.out.println(str.equals(null));
}
输出:
null
string is null
null is string
format string null
false
三、split测试
public static void testSplit() {
String str = "";
String[] arr = str.split(";");
System.out.println(arr.length);
str = ";";
arr = str.split(";");
System.out.println(arr.length);
str = "1;";
arr = str.split(";");
System.out.println(arr.length);
}
输出:
1
0
1
四、正则表达式使用
1、\w表示单词字符[a-zA-Z_0-9];
2、matches将整个输入序列与模式匹配;
3、lookingAt将输入序列从头开始与模式匹配;
4、find扫描输入序列以查找与模式匹配的下一个子序列;
public static void testRegex() {
Pattern pattern;
Matcher match;
String emails = "email is aa@webank.com or bb@webank.com";
pattern = Pattern.compile("[\\w][\\w-.]+@[\\w-.]+[\\w]");
match = pattern.matcher(emails);
while (match.find()) {
System.out.println(match.group(0));
System.out.println(match.start());
System.out.println(match.end());
}
pattern = Pattern.compile("email is aa@webank.com or bb@webank.com");
match = pattern.matcher(emails);
if (match.matches()) {
System.out.println(match.group(0));
System.out.println(match.start());
System.out.println(match.end());
}
pattern = Pattern.compile("email");
match = pattern.matcher(emails);
if (match.lookingAt()) {
System.out.println(match.group(0));
System.out.println(match.start());
System.out.println(match.end());
}
pattern = Pattern.compile("mail");
match = pattern.matcher(emails);
if (match.lookingAt()) {
System.out.println(match.group(0));
System.out.println(match.start());
System.out.println(match.end());
}
}
输出:
aa@webank.com
9
22
bb@webank.com
26
39
email is aa@webank.com or bb@webank.com
0
39
email
0
5