1.补充Integer
int num
= 100;
Integer integer
= new Integer(num
);
String s
= integer
.tostring();
System
.out
.println(s
);
String str
= num
+"";
String str1
= String
.valueof(num
);
Integer integer
= new Integer("100000");
int i
= integer
.intvalue();
System
.out
.println(i
);
2.检验qq号的规则
Scanner sc
= new Scanner(System
.in
);
System
.out
.println("请输入你的qq号");
String str
= sc
.nextline
;
if(str
.length
>=5&&str
.length
<=15&&){
if(!str
.startWith
=="0"){
for(i
=0;i
<str
.length
;i
++){
Char ch
= str
.CharAt(i
);
if(ch
>="0"&&ch
<="9"){
System
.out
.println("qq号正确");
}else{
System
.out
.println("qq号错误");
}
}
}
3.验证手机号规则
Scanner sc
= new Scanner(System
.in
);
System
.out
.println("请输入你的手机号");
String str
= sc
.nextline
;
if(str
.lenth
==11&&str
.startWith(13)||startWith(15)||startWith(17)||startWith(18)){
for(i
=0;i
<str
.length
;i
++){
Char ch
= str
.CharAt(i
);
if(ch
>="0"&&ch
<="9"){
System
.out
.println("手机号正确");
}else{
System
.out
.println("手机号错误");
}
}
}
4.正则表达式的学习
String regx
= "a";
boolean b
= "a".matches(regx
);
boolean c
= "a".equals(regx
);
System
.out
.println(b
)
regx
= "[a,b,c]"
regx
= "[1,2,3,4,5,6,7,8,9]";
regx
= "[^0-9]"
regx
= "[A-Za-z0-9]";
regx
= ".";
regx
= "\\";
regx
= ".."
regx
= "\\
|;
regx
= "\\d";
regx
= "\\w";
regx
= "a*";
regx
= "[a-z]";
regx
= "[a-zA-Z0-9]+";
regx
= "[a-z]{5}";
regx
= "[a-z]{2,}";
regx
= "[0-9]{5-9}";
练习1
private static boolean check(String phoneNumber
){
String number
= "[1][3,5,7,8][0-9]{9}";
return phoneNumber
.mathes(number
);
练习2
private static boolean check(String eamil
){
String number
= "[a-zA-Z]\\w{5,17}@[a-z1-9]{2,10}\\.(com|net|cn|org)"
return eamil
.mathes(number
);
练习3
校验身份证
String str
="[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]"
5.split方法的介绍
String str
= "username=张三";
int index
= str
.indexof("=")
String substring
= str
.substring(0,index
);
System
.out
.println(substring
);
String substring1
= str
.substring(index
+ 1);
System
.out
.println(substring1
);
String
[] split
= str
.split("=");
System
.out
.println(split
[0]);
System
.out
.println(split
[1]);
String str
= "asdfasdf1234234asdfasdf12343asdfasdf";
String str1
= "[0-9]";
String
[] split1
= str
.split(str1
);
System
.out
.println(Arrays
.toString(split1
));
练习题
public class Demo{
public static void main(String
[] args
){
String strs
= "91 27 46 38 50";
String
[] strs
= str
.split(" ");
int[] arr
= new int[strs
.length
];
for(int i
= 0; i
<strs
.length
;i
++){
arr
[i
] = Integer
.parseInt(strs
[i
]);
}
StringBuilder sb
= new StringBuilder();
for(int i
= 0 ; i
<arr
.length
;i
++){
sb
.append(arr
[i
].append(" "));
}
String s
= sb
.toString().trim();
System
.out
.println(s
);
}
}
6.根据正则替换的方法
public class MyTest{
pubic
static void main(String
[] args
){
String s
= "奥巴马和普京是朋友".replace(
"奥巴马",
"*")
.replace(
"普京",
"*")
String s
= "奥巴马和普京是朋友".replaceAll("奥巴马",
"*")
String s1
= "121321awewrw12324sdfsdf131312sdfsd";
String s1
= s1
.replaceAll("[0-9]","")
}
}
7.常见对象(Pattern和Matcher的概述
Pattern p
= Pattern
.compile("a*b*");
Matcher m
= p
.matcher("aaaaab");
boolean b
= m
.matches
System
.out
.println(b
);
boolean matches
= "aaaaab".matches("a*b*");
System
.out
.println(b
);
String str
= "da jia ting wo shuo ,jin tian yao xia yu ,bu shang wan zi xi";
String str1
= "[a-z]{3}";
Pattern p
= Pattern
.compile(str1
);
Matcher m
= p
.matcher(str
);
boolean b
= m
.find();
if(b
){
String g
= matcher
.group();
System
.out
.print(g
);
}
转载请注明原文地址: https://win8.8miu.com/read-4177.html