下面用一段例子来展示String的常见方法:
package string;
public class StringMethodDemo {
public static void main(String[] args) {
//字符串最大的特点是一旦初始化不可以改变
String a = " abc abc";//s1是一个类型变量,在内存中有一个对象,“abc”是一个对象
String b = new String(" abc abc");//s2在内存中有两个对象,一个是String,一个是“abc”
compare(a,b);//判断两个字符串是否相同
//字符串的常见操作
//1 获取
//1.1 获取字符串长度
int length = a.length();
System.out.println(length);
//1.2 根据位置获取位置上的字符,注意角标越界异常StringIndexOutOfBoundsException
char c = a.charAt(1);
System.out.println(c);
//1.3根据字符返回该字符在字符串中第一次出现的位置,当没有找到时返回-1
int here = a.indexOf(97); //该方法可以接受ASCII
System.out.println(here);
int here2 = a.indexOf("a");//也可以接受String
System.out.println(here2);
int here3 = a.indexOf("a", 3);//指定从角标为3的字符开始往后查找第一个“a”
System.out.println(here3);
int here4 = a.indexOf("a");//也可以从后查找第一个“a”
System.out.println(here4);
int here5 = a.lastIndexOf("a", 3);//指定从角标为3的字符开始往前查找第一个“a”
System.out.println(here5);
//2 判断
//2.1字符串是否包含指定内容
boolean isContains = a.contains("bca");
System.out.println(isContains);
//2.2判断字符是否为空
boolean isEmpty = a.isEmpty();//JDK1.6才有的方法
System.out.println(isEmpty);
//2.3判断字符串是否以指定内容开头
boolean isStart = a.startsWith("ab");
System.out.println(isStart);
//2.4判断字符串是否以指定内容结尾
boolean isEnd = a.endsWith("bc");
System.out.println(isEnd);
//2.5判断字符串内容是否相同
boolean isEquals = a.equals(b);
boolean isEquals2 = a.equalsIgnoreCase(b);//忽略大小写判断字符串内容是否相同
System.out.println(isEquals);
System.out.println(isEquals2);
//3 转换
//3.1将字符数组转换成字符串
//(3.3 String的构造方法也可以将字符数组byte[]转成字符串,
// new String(byte[]),
// new String(byte[],startAt,number);
// 3.5 将基本类型转换成字符串String.valueOf();)
char[] ch = {'a','b','c','a','b','c'};
String chStr = new String(ch);
System.out.println(chStr);
String chStr2 = new String(ch,4,1);
System.out.println(chStr2);
String chStr3 = String.copyValueOf(ch);
System.out.println(chStr3);
String chStr4 = String.valueOf(ch);
System.out.println(chStr4);
//3.2将字符串变成字符数组
char[] ch2 = a.toCharArray();
for(int i = 0;i<ch2.length;i++){
if(i==0){
System.out.print("["+ch2[i]+",");
}else if(i==ch2.length-1){
System.out.println(ch2[i]+"]");
}else{
System.out.print(ch2[i]+",");
}
}
//3.4将字符串转成字节数组
byte[] bt = a.getBytes();
System.out.println(new String(bt));
//4 替换
String newStr = a.replace("bc", "D");//将字符串中的“bc”,替换成“D”
System.out.println(newStr);
//5 分割
String[] cutArr = a.split("b");//将字符串按着“b”进行分割,然后放到String[]数组中
System.out.println(cutArr.toString());
//6 剪切
String cutStr = a.substring(2);//从角标为2的字符开始剪切(包含该字符)
System.out.println(cutStr);
String cutStr2 = a.substring(2,5);//从角标为2的字符开始剪切(包含该字符),剪切到角标为5的字符(不包含该字符)
System.out.println(cutStr2);
//7 比较 去空格 大小写
int compare = a.compareTo(b);//返回的是a与b字符串中字符的ASCII码的差值(a-b)
System.out.println(compare);
String trimStr = a.trim();//只去除字符串两端的空格,中间的空格不会被去除
System.out.println(trimStr);
String lower = a.toLowerCase();//将字符串转换成小写
System.out.println(lower);
String upper = a.toUpperCase();//将字符串转换成大写
System.out.println(upper);
}
//比较两个字符串是否相同
private static void compare(String a,String b){
System.out.println(a==b);//不仅判断内容,还有内存中的地址
System.out.println(a.equals(b));//只判断内容是否相同
}
}