一、String
创建:自变量,new,String类型的不可变性
常用API 有关字符串的长度、字符检索、格式匹配等操作
package t12_2;
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
//charAt()
String s1="Java shixun";
System.out.println(s1);
System.out.println(s1.charAt(0));//J
System.out.println(s1.charAt(5));//s
//indexOf()
String st1="a";
System.out.println(s1.indexOf(st1));//1
String st2="K";
System.out.println(s1.indexOf(st2));//-1
//lastIndexOf()
System.out.println(s1.lastIndexOf(st1));//3
//startsWith()
String s2="tb123123123";
boolean result1 = s2.startsWith("tb");
System.out.println(result1);//true
//substring()
String s3="0123456789";
System.out.println(s3);
String st3=s3.substring(1, 4);
System.out.println(st3);//123
String st4=s3.substring(6);
System.out.println(st4);//6789
//split()
String s5="工程设计院;创新谷;园博园;大学城";
String[] stations = s5.split(";");
System.out.println(Arrays.toString(stations));//[工程设计院, 创新谷, 园博园, 大学城]
//trim()
String s6=" admi ";
System.out.println(s6.length());//10
s6=s6.trim();
System.out.println(s6.length());//4
}
}
二、正则表达式
package t12_3;
import java.util.Scanner;
/**
* 正则表达式
* @author 惊蛰
*
*/
public class TestRegex {
public static void main(String[] args) {
//1)手机号11位,开头1
//2)验证码6位
Scanner sc=new Scanner(System.in);
String phone = "";
String code = "";
System.out.println("请输入手机号");
while(true) {
phone=sc.next();
String regex1 = "^1[0-9]{10}$";//正则表达式,1开头,后面是10位0-9数字
if(!phone.matches(regex1)) {
System.out.println("手机号格式错误");
continue;
}
break;
}
System.out.println("请输入验证码");
while(true) {
code=sc.next();
String regex2 = "^[0-9]{6}$";
if(!code.matches(regex2)) {
System.out.println("验证码格式错误");
continue;
}
break;
}
}
}
三、字符串缓冲区(StringBuilder)
可变字符序列
创建:new
常用API:字符序列可变
package t12_5;
public class Test {
/**
* 字符串缓冲区:可以做扩容
* @param args
*/
public static void main(String[] args) {
StringBuilder builder = new StringBuilder();
StringBuilder builder1=builder;
//append
builder.append("hello");
System.out.println(builder==builder1);//true
builder.append(" everyone");
System.out.println(builder==builder1);//true
System.out.println(builder.toString());//hello everyone
System.out.println(builder1.toString());//hello everyone
//delete
builder.delete(6, 11);//删除序号6-10
System.out.println(builder.toString());//hello one
//insert
builder.insert(6, "every");//从第6个位置后mj0,开始插入
System.out.println(builder.toString());//hello everyone
//substring,同String中
//reverse
builder.reverse();//反转
System.out.println(builder.toString());//enoyreve olleh
//length,同String中
//toString返回此序列中数据的字符串表示形式
}
}
密码加密,输入密码,从第三个字符开始插入pwd,追加123,倒序
package t12_5_1;
import java.util.Scanner;
public class Test {
/**
* 密码加密,输入密码,从第三个字符开始插入pwd,追加123,倒序
* @param args
*/
public static void main(String[] args) {
//StringBuilder不能通过next()输入
String password = null;
Scanner sc = new Scanner(System.in);
System.out.println("请输入密码");
password = sc.next();
StringBuilder builder = new StringBuilder();
builder.append(password);
builder.insert(2, "pwd");//
builder.append("123");
builder.reverse();
System.out.println(builder.toString());
}
}