package com.string;
public class string1 {
public static void main(String args[]){
//将char[]数组转换成String
char[] ch={'h','我','是','中','国','人'};
String str=new String(ch);
System.out.println(str); //结果为str="我是中国人"
//将字符串转换为char数组,方法一使用getChars()
char[] ch2 =new char[8];
str.getChars(0, 3, ch2, 2);
System.out.println(ch2);
//将字符串转换为char数组,方法二使用toCharArray()方法
ch2=str.toCharArray();
System.out.println(ch2);
//获取字符串指定位置的字符charAt()方法
System.out.println(str.charAt(1));
//getBytes()方法将字符串变成一个Byte数组
byte[] bt = str.getBytes();
System.out.println(new String(bt));
//indexOf()方法判断字符串是否存在
if(str.indexOf("中国人")!=-1){
System.out.println("\"中国人\"字符串存在!");
}else{
System.out.println("\"中国人\"字符串不存在!");
}
//trim()方法去掉左右空格
str=" "+str+" ";
System.out.println(str);
str=str.trim();
System.out.println(str);
//substring() 字符串截取
System.out.println(str.substring(1,4));
//split()方法按指定的字符串拆分字符串成数组
String sp[]=str.split("是");
System.out.println(sp[0]);
//toLowerCase()方法将字符串转换为小写
System.out.println(str.toLowerCase());
//toUpperCase()方法将字符串转换为大写
System.out.println(str.toUpperCase());
//startsWith()方法判断字符串是否以指定的内容开头
System.out.println(str.startsWith("h")?"以h开头":"非以h开头");
//endsWith()方法判断字符串是否以指定的内容结尾
System.out.println(str.endsWith("h")?"以h结尾":"非以h结尾");
//equals()方法区分大小写比较两个字符串
System.out.println(str.equals(new String("H我是中国人"))?"字符串相同":"字符串不相同");
//equalsIgnoreCase()方法不区分大小写比较两个字符串
System.out.println(str.equalsIgnoreCase(new String("H我是中国人"))?"字符串相同":"字符串不相同");
//replaceAll()方法将字符串中指定内容替换成其它内容
System.out.print(str.replaceAll("h","H"));
}
}