将字符串倒序的三种写法
1.先定义字符串 hello-123-java中国
public static void main(String[] args) {
//将字符串倒序
String str = "hello-123-java中国";
System.out.println(str+" 第一次输出");
}
第一种写法:利用反转函数reserve
//revers 将字符串进行倒序
System.out.println(new StringBuilder(str).reverse()+" 第一种方法运行结果");
第二种写法:利用substring 进行截取
StringBuilder su = new StringBuilder();
String temp = " ";
for (int i=0; i<str.length() ;i++){
temp = str.substring(i ,i+1)+ temp ;
}
System.out.println( temp+" 输出temp的结果");
第三种写法:利用charAt ,返回一个字符串的指定位置的字符
StringBuilder su = new StringBuilder();
for (int i=0; i<str.length() ;i++){
su.insert(0,str.charAt(i));
}
System.out.println(su+" 输出su的结果");
三种方法的结果如下:
判断名字的中英文类型
创建Scanner 类对象
定义名字的类型方式
用matchers检测字符串是否匹配给定的正则表达式
([\u4e00-\u9fa5]{2,8} 代表2到8个汉字字符)
//判断字符串是不是中文字符?
Scanner sc = new Scanner(System.in);
System.out.println( "请输入姓名:");
//trim()和strip()都可以使用
String name = sc.nextLine().trim();
if (name .matches("[\u4e00-\u9fa5 ]+")){
System.out.println("欢迎:"+ name);
}else {
System.out.println("Welcome:"+ name);
}
//这样写也是可以的
Scanner sc =new Scanner(System.in);
System.out.println("请输入字符串: ");
String ss =sc.nextLine().trim();
if (ss.matches("[\u4e00-\u9fa5 ]+")){
System.out.println("欢迎你: "+ss);
} else if (ss.matches("[a-zA-Z ]+")) {
System.out.println("Hello :" + ss);
} else {
System.out.println("其他的输出方式,请在思考其他方法的使用");
}
两种不同的输出结果:
判断手机号格式:
利用ASCII码进行判断:
//判断一个手机是不是正确的手机号
System.out.println("请输入你的手机号: ");
String phone = sc.nextLine().trim();
boolean f = true;
if (phone.length() == 11 && phone.startsWith("1")) {
for (int i = 1; i < phone.length(); i++) {
char c = phone.charAt(i);
if (c >= '0' && c <= '9') {
} else {
f = false;
break;
}
}
} else {
f = false;
}
if (f) {
System.out.println("手机号格式正确: " + phone);
} else {
System.out.println("手机号格式错误");
}
}
}
利用正向表达式进行书写:
//判断一个手机是不是正确的手机号
Scanner sc =new Scanner(System.in);
System.out.println("请输入你的手机号: ");
String phone = sc.nextLine().trim();
if (phone.matches("1[358]\\d{9}")) {
System.out.println("手机号格式正确: " + phone);
} else {
System.out.println("手机号格式错误");
}
}
}
正则表达式
正则表达式是一种字符串的表示模式,是一种特殊的字符串
.*中 .代表一个任意符号(什么都行),*代表前边的符号出现0次或者多次,和{0,}类似
.*也可以表达为.{0,}
在我现在所学的知识中,要在[ ]中加空格,在空格外加会报错
public static void main(String[] args) {
//判断字符串有没有大小写字母
//判断字符串是不是大写字母组成
System.out.println("Hello".matches("[A-Z]+"));
//判断字符串有没有大写字母
System.out.println("Hello".matches(".*[A-Z]+.*"));
System.out.println("hello".matches(".*[A-Z]+.*"));
//判断字符串中有没有汉字
System.out.println("hello 世界".matches(".*[\u4e00-\u9fa5].*"));
//判断字符串中有没有数字 \\d = [0-9]
System.out.println("hello526".matches(".*[0-9].*"));
System.out.println("hello526".matches(".*\\d.*"));
//判断字符串中有没有手机号
System.out.println("我的手机号是:13110111010 .".matches(".*1[358]\\d{9}.*"));
}
这两行的输出结果就很有意思:
String ss = "123abcdefg3555AABBCCDD255java44php";
System.out.println(ss.replaceAll("[0-9]*","?"));
输出结果:??a?b?c?d?e?f?g??A?A?B?B?C?C?D?D??j?a?v?a??p?h?p?
如果再正则表达式输出前的括号内加上一个空格会进行报错,输出不来想要的结果
Scanner sc = new Scanner(System.in);
System.out.println("请输入姓名字符串: ");
String aa = sc.nextLine().trim().strip();
//再正则表达式的最后的引号前加空格会报错,注释的地方进行输出的时候,输入中中文会报错
// if (aa.matches("[\u4e00-\u9fa5 ]+ ")){
if (aa.matches("[\u4e00-\u9fa5 ]+")){
System.out.println("你好 :" +aa);
} else if (aa.matches("[a-zA-Z ]+")) {
System.out.println("hello : " +aa);
}else {
System.out.println("请输入正确的字符串 ");
}
在项目注册的时候,名字要么是中文要么是英文
Scanner sc = new Scanner(System.in);
System.out.println("请输入姓名 : ");
String name = sc.nextLine().strip();
Pattern p = Pattern.compile("[a-zA-Z\s]+|[\u4e00-\u9fa5\s]{2,}");
Matcher m = p.matcher(name);
if (m.matches()){
System.out.println("姓名格式正确: "+ name );
}else {
System.out.println("格式错误: " + name );
}