package chapter3;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Locale;
/**
* 本类是用来测试String中的各种方法
* @author Administrator
*
*/
public class StringTest {
public static String str = new String("abcdefghijklmnopqrestuvwxyz");
public static void main(String[] args) {
//charAt(int index)获取第index位置的字符index从0开始算起
char ch = str.charAt(10);
System.out.println(ch);
//codePointAt(int index)
//返回指定索引处的字符(Unicode 代码点)。索引引用 char 值(Unicode 代码单元),其范围从 0到 length() - 1。
for(int i = 0; i < str.length(); i++) {
System.out.println(str.charAt(i) + ":" + str.codePointAt(i));
}
System.out.println("==========================");
//codePointBefore(int index)
//返回指定索引前一处的字符(Unicode 代码点)。索引引用 char 值(Unicode 代码单元),其范围从 0到 length() - 1。
for(int i = 1; i < str.length(); i++) {
System.out.println(str.charAt(i) + ":" + str.codePointBefore(i));
}
//codePointCount(int beginIndex, int endIndex)
//准确计算unicode字符的数量,而不是char的数量。(含头不含尾)
System.out.println(str.codePointCount(2, 10));
//compareTo(String string)
//如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的差值。如果相等返回0
String s1="abc";
String s2 = "bcd";
System.out.println(s1.compareTo(s2));
//compareToIgnoreCase(String str)(忽略大小写)
//如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的差值。如果相等返回0
String s3="ABC";
System.out.println(s3.compareTo(s2));
System.out.println(s3.compareToIgnoreCase(s2));
//concat(String str)两个字符串拼接不会改变被拼接的字符串而是创建新的字符串
System.out.println(s1.concat(s2));
System.out.println(s1);
//contains(CharSequence s)
//用作当且仅当此字符串包含指定的 char 值序列时,返回 true。参数是CharSequence,不能传入char类型
System.out.println(str.contains(s1));
//contentEquals(CharSequence cs)
//比较字符串到指定的CharSequence。其结果是true当且仅当此String指定序列相同的char值序列
System.out.println(str.contentEquals(s1));
// contentEquals(StringBuffer sb)
//比较字符串到指定的CharSequence。其结果是true当且仅当此String指定序列相同的char值序列
System.out.println(str.contentEquals(new StringBuffer(s1)));
//copyValueOf(char[] data)
//将字符数组转string
char[] letters = {'1','a','c'};
System.out.println(String.copyValueOf(letters));
//copyValueOf(char[] data, int offset, int count)
//将字符数组中的一部分转string
char[] letters2 = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
System.out.println(String.copyValueOf(letters2,10,10));
//endsWith(String suffix)
//测试字符串是否以suffix结尾
System.out.println(str.endsWith("xyz"));
//equals(Object anObject)
//比较两个字符串是否相同
System.out.println("abc".equals("abc"));
// equalsIgnoreCase(String anotherString)
//比较两个字符串是否相同忽略大小写
System.out.println("abc".equalsIgnoreCase("ABC"));
//format(Locale l, String format, Object... args)
System.out.println(String.format(Locale.CHINA, "%tF", new Date()));
//getBytes()
//将String转字节数数组
byte[] bytes = str.getBytes();
for(int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
//getBytes(Charset charset)
//按照指定的编码格式将String转字节数数组
try {
byte[] bytes2 = str.getBytes("UTF-8");
for(int i = 0; i < bytes2.length; i++) {
System.out.println(bytes2[i]);
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)
byte[] bytes3 = new byte[10];
str.getBytes(5, 15, bytes3, 0);
for(int i = 0; i < bytes3.length; i++) {
System.out.println(bytes3[i]);
}
//getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
//将字符串中的一部分存入到目标字符数组中
char[]chs = new char[10];
str.getChars(5, 15, chs, 0);
for(int i = 0; i < chs.length; i++) {
System.out.println(chs[i]);
}
//indexOf(int ch)数字ch第一次出现的位置
System.out.println(str.indexOf(1));
//indexOf(int ch, int fromIndex)从第fromIndex位置开始逆向查找数字ch第一次出现的位置
System.out.println(str.indexOf(1,10));
//indexOf(String str)字符串str出现的位置
System.out.println(str.indexOf("def"));
//indexOf(String str, int fromIndex)从第fromIndex位置开始逆向查找字符串str出现的位置
System.out.println(str.indexOf("def",10));
//inter 常量池被放入到堆空间中,这导致intern()函数的功能不同
String inter =new String("z") + new String("h");
String uninter = "zh";
System.out.println(inter == uninter);
String inter2 =new String("z") + new String("l");
inter2.intern();
String intered = "zl";
System.out.println(inter2 == intered); //这个输出与jdk版本有关
//isEmpty()判断字符串是否为空串"",字符串为null时不能使用此方法
System.out.println("".isEmpty());
//lastIndexOf(int ch)数字最后一次出现的位置
System.out.println(str.lastIndexOf(1));
//lastIndexOf(int ch, int fromIndex) 从fromIndex位置开始逆向查找,数字ch最后一次出现的位置
System.out.println(str.lastIndexOf(1,10));
//lastIndexOf(String str) 字符串str最后一次出现的位置
System.out.println(str.lastIndexOf("u"));
//lastIndexOf(String str, int fromIndex) 从fromIndex位置开始逆向查找,字符串str最后一次出现的位置
System.out.println(str.lastIndexOf("u",26));
//length() 字符串长度
System.out.println(str.length());
//matches(String regex)正则表达式匹配(以后单独学习)
System.out.println(str.matches("[a-zA-Z_]{1,}"));
//offsetByCodePoints(int index, int codePointOffset)
//返回该字符串中的索引,该索引由codePointOffset代码点对给定索引进行偏移。(....)
System.out.println(str.offsetByCodePoints(0, 3));
//regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
//比较两个字符串中的某一部分是否相同(可以选择是否区分大小写)
String str2 = "fdfsdxyz";
System.out.println(str.regionMatches(true, 24, str2, 5, 3));
//regionMatches(int toffset, String other, int ooffset, int len)
//比较两个字符串中的某一部分是否相同(不可以区分大小写)
System.out.println(str.regionMatches(24, str2, 5, 3));
//replace(char oldChar, char newChar)替换原字符串不变生成新的被替换的字符串
String str3 = str2.replace("f", "这");
System.out.println(str3);
System.out.println(str2);
//replace(CharSequence target, CharSequence replacement)
String str4 = str2.replace("fdf", "abc");
System.out.println(str4);
System.out.println(str2);
//replaceAll(String regex, String replacement) 替换多有满足正则表达式的字符串
//成功则返回替换的字符串,失败则返回原始字符串。
String str5 = new String("www.baidu.com");
System.out.println(str5.replaceAll("(.*)baidu(.*)", "tx" ));
//replaceFirst(String regex, String replacement)
System.out.println(str5.replaceFirst("(.*)baidu(.*)", "tx" ));
//split(String regex)
//按照正则表达式将字符串截取为字符串组
String[] split = str5.split("\\.");
for(int i = 0; i<split.length; i++) {
System.out.println(split[i]);
}
//split(String regex, int limit)
//按照正则表达式将字符串截取为字符串组,limit表示截取片段树,当limit<=0时,只要满足正则表达式就进行截取
String[] split2 = str5.split("\\.",2);
for(int i = 0; i<split2.length; i++) {
System.out.println(split2[i]);
}
//startsWith(String prefix)判断字符串的前缀
System.out.println(str5.startsWith("www"));
//startsWith(String prefix, int toffset)从第toffset位置开始是否是prefix
System.out.println(str5.startsWith("baidu", 4));
//subSequence(int beginIndex, int endIndex)
System.out.println("subSequence: " + str5.subSequence(4, 9));
//substring(int beginIndex) 从beginIndex开始截取
System.out.println(str5.substring(2));
//substring(int beginIndex, int endIndex)从beginIndex开始截取endIndex截止
System.out.println(str5.substring(2,6));
//toCharArray()字符串转字符数组
char[] charArray = str.toCharArray();
for(int i = 0; i< charArray.length; i++) {
System.out.print(charArray[i] + " ");
}
//toLowerCase()字符串大写转小写
String ustr = "ABC";
System.out.println(ustr.toLowerCase());
//toLowerCase(Locale locale)字符串大写转小写,这个可能会存在跨地域性的bug,不推荐使用
String ustr2 = "V";
System.out.println(ustr2.toLowerCase(Locale.ROOT));
//toUpperCase()字符串小写转大写
String lStr = "a";
System.out.println(lStr.toUpperCase());
//toUpperCase(Locale locale)字符串小写转大写 ,这个可能会存在跨地域性的bug,不推荐使用
System.out.println(lStr.toUpperCase(Locale.ROOT));
//trim()去掉字符串前后的空白符(包括:空格, 回车, tab等)
String tString = " abc a\n";
System.out.println("_" + tString + "_");
String trim = tString.trim();
System.out.println("_" + trim + "_");
//valueOf(boolean b)将boolean转字符串
System.out.println(String.valueOf(true));
//valueOf(char c)将char转为字符串
System.out.println(String.valueOf('a'));
//valueOf(char[] data)
System.out.println(String.valueOf(new char[]{'a','b','b'}));
//valueOf(char[] data, int offset, int count)
System.out.println(String.valueOf(new char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'},1,3));
//valueOf(double d)
System.out.println(String.valueOf(1.23d));
//valueOf(float f)
System.out.println(String.valueOf(1.23f));
//valueOf(int i)
System.out.println(String.valueOf(1));
//valueOf(long l)
System.out.println(String.valueOf(1L));
//valueOf(Object obj)
}
}