String 中相关的API
String str = "123ABC";
String unstr = "\u1D56";
System.out.println("字符串长度="+str.length());
System.out.println("取值付出第 1个字符: "+str.charAt(0));
System.out.println("取出A的Unicode代码: "+ str.codePointAt(3));
System.out.println("取出A的前3的Unicode代码: "+ str.codePointBefore(3));
System.out.println("计算出Unicode代码点长度: "+ unstr.codePointCount(0, 1));
System.out.println("字符串比较: "+ str.compareTo("123abc")); //正为为大于,负为小于,0为等于
System.out.println("忽略大小写字符串比较: "+ str.compareToIgnoreCase("123abc"));
System.out.println("字符串拼接: "+ str.concat("cc"));
System.out.println("鉴定2个对象是否相等的: "+ str.hashCode());
System.out.println("字符首次出现位置: "+ str.indexOf("AB"));
System.out.println("索引到N开始,字符首次出现位置: "+ str.indexOf("A", 4));
System.out.println("字符最后一次出现位置: "+ str.lastIndexOf("A"));
System.out.println("索引到N开始,字符最后出现位置: "+ str.lastIndexOf("A", 4));
System.out.println("从第N个开始偏移M个: "+ str.offsetByCodePoints(2,6));
System.out.println("替换所有字符: "+ str.replaceAll("A", "a"));
System.out.println("替换所有字符: "+ str.replace('A', 'a'));
System.out.println("替换第一次出现的字符: "+ str.replaceFirst("A", "a"));
System.out.println("替换第一次出现的字符: "+ str.replaceFirst("A", "a"));
System.out.println("截取第N个字符后: "+ str.substring(3));
System.out.printf("截取字符串大于等于1至小于3下标的字符串 : ", str.subSequence(0, 3)); //substring返回的是String,subSequence返回的是类
System.out.println("截取第N至M个前的字符: "+ str.substring(1, 3));
System.out.println("方法用于把字符串转换为小写。: "+ str.toLowerCase());
System.out.println("方法用于把字符串转换为大写。: "+ str.toUpperCase());
char[] data = new char[]{'a','b','c'};
String a = String.valueOf(data);
//String a = String.valueOf(data,1,3); //N下标开始拿M个元素出来
String b = String.copyValueOf(data);
System.out.println("把值转换: "+ a +" "+ b);
str=String.format("Hi,%s:%s.%s", "王南","王力","王张");
System.out.println(str);
System.out.println("字符串是否包含BC::"+ str.contains("BC"));
String str2 = "23";
System.out.printf("将“str中从位置1开始的字符串”和“region1中位置1开始的字符串”进行比较,比较长度是3: "+str.regionMatches(1, str2,0, 2));
System.out.println("字符串最前一个字母是否是1:"+str.startsWith("1"));
System.out.println("字符串最后一个字母是否是1:"+str.endsWith("C"));
System.out.printf("字符串长度 : ", str.length());
System.out.printf("字符串是否为空 : ", str.isEmpty());
// 1. CharSequence的子类String
String substr = (String)str.subSequence(0, 5);
System.out.println("str.subSequence(0,5) :" + substr.toString());
// 2. CharSequence的子类StringBuilder
StringBuilder strbuilder = new StringBuilder("abcdefghijklmnopqrstuvwxyz");
String substrbuilder = (String)strbuilder.subSequence(0,5);
System.out.println("strbuilder.subSequence(0,5):" + substrbuilder.toString());
// 3. CharSequence的子类StringBuffer
StringBuffer strbuffer = new StringBuffer("abcdefghijklmnopqrstuvwxyz");
String substrbuffer = (String)strbuffer.subSequence(0,5);
System.out.println("strbuffer.subSequence(0,5):"+substrbuffer.toString());
总结不好多多担待,文章只单纯个人总结,如不好勿喷,技术有限,有错漏麻烦指正提出。本人QQ:373965070