包装类
package com.api.integer;
//包装类
public class Test {
public static void main(String[] args) {
int a = 10;
Integer b = 11;
System.out.println(a);
System.out.println(b);
System.out.println("====================");
Integer c = a;
Integer d = 100;
int e = d;
System.out.println(c);
System.out.println(e);
double f = 99.5;
Double g = f;
double h = g;
System.out.println(h);
System.out.println("===========================================");
//int a1 = null; null is for reference type
Integer a2 = null;
Integer a3 = 0;
System.out.println(a2);
System.out.println("==========================================");
Integer b1 = 23;
String s = b1.toString();
System.out.println(s + 1);
String s1 = Integer.toString(b1);
System.out.println(s1 + 1);
//可以直接+字符串得到字符串类型
String s2 = b1 + "";
System.out.println(s2 + 1);
System.out.println("====================================================");
String m = "23";
int n = Integer.parseInt(m);
System.out.println(n+1);
int n2 = Integer.valueOf(m);
System.out.println(n2+1);
String m1 = "99.9";
double n1 = Double.parseDouble(m1);
System.out.println(n1 + 0.1);
double n3 = Double.valueOf(m1);
System.out.println(n3+0.1);
}
}
正则表达式
package com.api.regex;
//a sharp contrast
public class RegexDemo01 {
public static void main(String[] args) {
System.out.println(checkQQ("766845135"));
System.out.println(checkQQ("123456"));
System.out.println(checkQQ("a1231233"));
System.out.println(checkQQ(null));
System.out.println(checkQQ("12345"));
System.out.println(check2("766845135"));
System.out.println(check2("123456"));
System.out.println(check2("a1231233"));
System.out.println(check2(null));
System.out.println(check2("12345"));
}
public static boolean check2(String qq) {
return qq != null && qq.matches("\\d{6,20}");
}
public static boolean checkQQ(String qq){
if (qq == null || qq.length() < 6 || qq.length() > 20 ){
return false;
}
for (int i = 0; i < qq.length(); i++) {
char a = qq.charAt(i);
if (a < '0' || a > '9'){
return false;
}
}
return true;
}
}
package com.api.regex;
//regex rule
public class RegexDemo02 {
public static void main(String[] args) {
//java.util.regex.Pattern
System.out.println("z".matches("[abc]"));
System.out.println("a".matches("[abc]"));
System.out.println("z".matches("[^abc]"));
System.out.println("a".matches("[^abc]"));
System.out.println("aa".matches("[abc]"));
System.out.println("aa".matches("[abc]+"));
System.out.println("=============================================");
System.out.println("2".matches("\\d"));
System.out.println("a".matches("\\d"));
System.out.println("222".matches("\\d"));
System.out.println("2".matches("\\D"));
System.out.println("a".matches("\\D"));
System.out.println("===============================================");
System.out.println("a".matches("\\w"));
System.out.println("2".matches("\\w"));
System.out.println("_".matches("\\w"));
System.out.println("aa".matches("\\w"));
System.out.println("你".matches("\\w"));
System.out.println("你".matches("\\W"));
System.out.println("==============================================");
System.out.println("2442fsfs".matches("\\w{6,}"));
System.out.println("2412f".matches("\\w{6,}"));
System.out.println("===============================================");
System.out.println("35as".matches("[a-zA-Z0-9]{4,}"));
System.out.println("23_F".matches("[a-zA-Z0-9]{4,}"));
System.out.println("23dF".matches("[\\w&&[^_]]{4,}"));
System.out.println("23_F".matches("\\w&&[^_]{4,}"));
}
}
package com.api.regex;
//案例:校验输入的信息
import java.util.Scanner;
public class RegexTest3 {
public static void main(String[] args) {
//checkPhone();
checkMoney();
}
public static void checkMoney() {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("请您输入您的金额:");
String phone = scan.next();
//[](只匹配一个字符) 和 ()的区别 && 取交集
if (phone != null && phone.matches("\\d+(\\.\\d+)?")){
System.out.println("输入正确");
break;
}else {
System.out.println("输入错误");
}
}
scan.close();
}
public static void checkTel() {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("请您输入您的注册tel号码:");
String phone = scan.next();
//027-3572457 0273572457
if (phone != null && phone.matches("0\\d{2,6}-?\\d{5,20}")){
System.out.println("输入正确");
break;
}else {
System.out.println("输入错误");
}
}
scan.close();
}
public static void checkPhone() {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("请您输入您的注册手机号码:");
String phone = scan.next();
if (phone != null && phone.matches("1[3-9]\\d{9}")){
System.out.println("输入正确");
break;
}else {
System.out.println("输入错误");
}
}
scan.close();
}
public static void checkEmail() {
Scanner scan = new Scanner(System.in);
while (true) {
//[email protected]
//[email protected]
//[email protected]
System.out.println("请您输入您的注册Email:");
String phone = scan.next();
if (phone != null && phone.matches("\\w{1,30}@[a-zA-Z1-9]{2,20}" +
"(\\.[a-zA-Z1-9]{2,20}){1,2}")){
System.out.println("输入正确");
break;
}else {
System.out.println("输入错误");
}
}
scan.close();
}
}
package com.api.regex;
//正则表达式在字符串方法中的使用
import java.util.Arrays;
public class RegexDemo04 {
public static void main(String[] args) {
String names = "小明asdasd3132131小张asdddd1115小林";
String[] s1 = names.split("\\w+");
for (int i = 0; i < s1.length; i++) {
System.out.println(s1[i]);
}
System.out.println(Arrays.toString(s1));
String s2 = names.replaceAll("\\w+", " ");
System.out.println(s2);
}
}
package com.api.regex;
//正则表达式爬取信息
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexDemo05 {
public static void main(String[] args) {
String s = "来黑马程序学习Java,电话020-43422424,或者联系邮箱" +
"[email protected],电话18762832633,0203232323 " +
"邮箱[email protected], 400-100-3233, 4001003232";
String regex = "(0\\d{2,6}-?\\d{5,20})|(1[3-9]\\d{9})" +
"|(\\w{1,30}@[a-zA-Z1-9]{2,20}(\\.[a-zA-Z1-9]{2,20}){1,2})" +
"|(400-?\\d{3,9}-?\\d{3,9})";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(s);
while (matcher.find()){
System.out.println(matcher.group());
}
}
}
标签:String,正则表达式,matches,System,包装,println,public,out
From: https://www.cnblogs.com/799rijiyuelei/p/16908952.html