package rejava2.工具.字符串相关;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestString {//String有private final char[] value; final:地址不变,本身长度不变;private:外部不能访问
public static void main(String[] args) {
String s1="abcdef"; //"abc"是对象,看作常量,在字符串常量池里
String s2=new String("abc"); //new的在堆中
System.out.println("================================字符串连接拆分和查找变更================================");
byte[] ba=new byte[]{48,65,97};
String s3=new String(ba); //ASCII码
char[] ca=new char[]{'a','b','c','d','e','f'};
String s4=new String(ca,1,4); //拼接,1索引开始,后4个
char cha = "asd".charAt(0); //"asd"索引为0转化成char
int i = "asd".codePointAt(2); //"asd"索引位2转换成ASCII码
char[] ch = "asas".toCharArray(); //String转化成char数组
String concat = "a".concat("b"); //拼接,此性能较+高
String con="a"+"b"+"c"+"d"; //过程产生7个对象,"a"一个,"b"一个,创建一个新的位"ab"......
String[] spli="as df gh jk".split("\t",2); //只能拆成两段
String substr1=s1.substring(1,2); //截取字符串从索引1
String substr2=s1.substring(1);
boolean tf1=s1.contains("asas");
boolean tf2=s1.endsWith("assa");
boolean tf3=s1.startsWith("as");
int a1=s1.indexOf("e",3); //从3号索引开始找
int a2=s1.lastIndexOf("e",3); //从后开始找
s1=s1.replace("a","*");
s1=s1.replaceAll("a","*"); //一样
s1=s1.replaceFirst("a","*"); //第一次出现
s1=s1.toLowerCase();
s1=s1.toUpperCase();
s1=s1.trim(); //前后去空格
System.out.println("================================常用继承和实现方法=====================================");
boolean equals = s1.equals(s2); //引用类型才能用,被String重写,原先比较地址,后比较值
int hash = s1.hashCode(); //被重写,通常equals重写时这个也重写
String e1="abc";
String e2="af";
int e = e1.compareTo(e2); //e1[i]-e2[i],一旦有结果就停止并返回结果(0不算),直到比完最小长度的次数,如果大的长度还有则返回两字符串个数差
System.out.println("================================Builder,Buffer=====================================");
StringBuilder sb = new StringBuilder(17); //默认长度空间为16,可自定义长度,满了自动扩容
StringBuilder abc = new StringBuilder("abc"); //默认为"abc"的长度+16
abc.append("d"); //拼接效率超高,并发
abc.insert(1,"aa"); //引用类型,都不用重新接收
abc.replace(0,8,"1212"); //0~8的都换
abc.reverse(); //翻转
abc.setCharAt(1,'b'); //1号变b
abc.capacity(); abc.length(); abc.trimToSize(); //底层数组容量 //长度 //去除多余容量
String hou=abc.substring(1,2); abc.delete(1,2); //重新接收 //直接删
abc.deleteCharAt(2); //指定删除2索引的
StringBuffer stringBuffer = new StringBuffer("abc");
//早期版本,线程安全,执行效率较低,Builder相反
System.out.println("================================正则================================================");
String str="abcdef";
boolean matches1 = str.matches("a[ab]"); //第一个字母是a,第二个是a,b中一个
boolean matches2 = str.matches("a[^abc]"); //第二个是除abc的其他
boolean matches3 = str.matches("a[a-zA-Z]"); //第二个是范围之内的
boolean matches4 = str.matches("a[a-z&&[^A-Z]]"); //a~z并且不能A~Z
boolean matches5 = str.matches("a?"); //零次或一次
boolean matches6 = str.matches("a*"); //零次或多次
boolean matches7 = str.matches("a+"); //一次或多次,至少一次
boolean matches8 = str.matches("\\d{6}"); //六次数字
boolean matches9 = str.matches("\\w{6,}"); //至少六次以上字母
boolean matches10 = str.matches("\\s{6,7}"); //六次或七次空白
String[] split = "1+2-3/4✖5".split("[\\+-/✖]"); //1,2,3,4,5
System.out.println("================================匹配================================================");
String pa="123456abcdef654321abcdef7891011121314";
Pattern compile = Pattern.compile("\\d{6}"); //模式对象
Matcher matcher = compile.matcher(pa); //匹配字符
while(matcher.find()){ //如果找到
System.out.println(matcher.group());
}
java工具类之字符串相关
最新推荐文章于 2024-03-20 03:28:14 发布