String类方法
1.字符与字符串
1.1 将字符数字中所有内容变为字符串:
public String(char value[]) //——构造方法
1.2 将部分字符数组的内容变为字符串:
public String(char value[],int offset,int count) //——构造方法
//在value[ ]数组中,取索引为 offset 位置开始的 count 个元素
1.3 取得字符串中索引位置的字符:
public char charAt(int index)
1.4 将字符串变为字符数组返回:
public char[] toCharArray( )
1.5 范例:
String str = "Hello world!";
char[] arr = {'A','B','C','D','E','f','g','h','i','j'};
//将字符数字中所有内容变为字符串
System.out.println(new String(arr));
//将部分字符数字中所有内容变为字符串
System.out.println(new String(arr,3,4));
//将字符串变为字符数组返回System.out.println(str.toCharArray());
//取得字符串中索引位置的字符
System.out.println(str.charAt(4));
System.out.println(str.charAt(15)); //会报错:StringIndexOutOfBoundsException
2 字节与字符串
2.1 将字节数组变为字符串:
public String(byte bytes[]) //——构造方法
2.2 将部分字节数组中的内容变为字符串:
public String(byte bytes[],int offset,int count) //——构造方法
//在value[]数组中,取索引为 offset 位置开始的 count 个元素
2.3 将字符串以字节数组的形式返回:
public byte[ ] getBytes()
2.4 范例:
String str = "Hello world!";
byte[] arr = str.getBytes();
System.out.println(new String(arr));
3.字符串比较:
3.1 区分大小写比较(常用比较方法):
public boolean equals(Object anObject)
3.2 不区分大小写比较:
public boolean equalsIgnoreCase(String anotherString)
3.3 比较两个字符串大小关系(常用比较方法):
public int compareTo(String anotherString)
//如:str1.compareTo(str2)
//相当于:str1 和 str2 中第一个不同的字符的ASCII码相减
//若 str1>str2 返回值大于0;
//若 str1=str2 返回值为0;
//若 str1<str2 返回值小于0;
3.4 不区分大小写比较两个字符串大小关系:
public int compareToIgnoreCase(String anotherString)
3.5 范例:
String str1 = "Hello";String str2 = "hello";System.out.println(str1.equals(str2)); //falseSystem.out.println(str1.equalsIgnoreCase(str2)); //trueSystem.out.println(str1.compareTo(str2)); //-32
4 字符串查找:
4.1 判断一个子字符串是否存在:
public boolean contains(String str)
4.2 查找指定字符串的位置:(找到返回位置的开始索引,找不到返回-1,如果内容重复,只返回第一个查找到的位置)
4.2.1 从头开始查找:
public int indexOF(String str)
4.2.1 从指定位置开始查找:
public int indexOf(String str,int fromIndex)
4.2.2 从后向前开始查找:
public int lastIndexOf(String str,int fromIndex)
4.2.2 从指定位置开始由后向前查找:
public int lastIndexOf(String str,int fromIndex)
4.3 判断是否是以指定字符串开头/结尾:
4.3.1 从头开始判断是否是以指定字符串开头
public boolean startsWith(String prefix)
4.3.2 从指定位置开始判断是否是以指定字符串开头
public boolean startsWith(String prefix,int toffset)
4.3.3 判断是否是以指定字符串结尾
public boolean endsWith(String suffix)
4.4 范例:
String str = "abcdefgh";System.out.println(str.contains("cde")); //true
System.out.println(str.indexOf("b")); //1
System.out.println(str.indexOf("c",4)); //-1
System.out.println(str.lastIndexOf("e")); //4
System.out.println(str.lastIndexOf("e",5)); //4
System.out.println(str.lastIndexOf("e",3)); //-1
System.out.println(str.startsWith("abc")); //true
System.out.println(str.startsWith("abd")); //false
System.out.println(str.startsWith("cde",2)); //true
System.out.println(str.endsWith("fgh")); //true
System.out.println(str.endsWith("h")); //true
System.out.println(str.endsWith("fh")); //false
5.字符串替换:
5.1 用字符串替换字符串中所有字符串:
public String replaceAll(String regex,String replacement)
//用 replacement 替换掉原字符串中的 regex 并输出
5.2 用字符串替换字符串中首个指定字符串:
public String replaceFirst(String regex,String replacement)
5.3 用字符替换字符串中所有字符:
public String replaceAll(chr oldChar,char newChar)
5.4 范例:
String str = "abababcbc";System.out.println(str.replaceAll("b","*")); //a*c*d*g*h
System.out.println(str.replaceFirst("b","*")); //a*cbdbgbh
System.out.println(str.replace('a','#')); //#b#b#bcbc
6.字符串拆分:
6.1 将字符串全部拆分:
public String[] split(String regex)
//以 regex 为界限,将字符串全部拆分
6.2 将字符串部分拆分:
public String[] split(String regex,int limit)
//以 regex 为界限,将字符串分为 limit 份
6.3 范例:
String str = "abababcbc";
String[] arr = str.split("a"); //b b bcbc
String[] arr2 = str.split("b",2); //a ababcbc
7.字符串截取:
7.1 从指定位置截取到结尾:
public String substring(int beginIndex)
7.2 截取部分内容:
public String substring(int beginIndex,int endIndex)
//从 beginIndex 开始,截取 endIndex-beginIndex 个字符,索引为:endIndex不会被获取
7.3 范例:
String str = "abcdefgh";
System.out.println(str.substring(3)); //defgh
System.out.println(str.substring(1,5)); //bcde
8.其他操作:
8.1 去掉字符串中左右空格,保留中间空格:
public String trim();
8.2 大小写转换:
8.2.1 字符串转大写:
public String toUpperCase();
8.2.1 字符串转小写:
public String toLowerCase();
8.3 字符串入池操作:
public native String intern()
8.4 字符串连接(等同于+,不入池):
public String concat(String str)
8.5 取得字符串长度:
public int length();
8.6 判断是否为空字符串,但不是null,而是长度为0
public boolean isEmpty()
8.7 范例:
String str = " a BcD ef Gh ";
String str2 = "";
System.out.println(str); // a BcD ef Gh
System.out.println(str.trim()); //a BcD ef GhSystem.out.println(str.toUpperCase()); // A BCD EF GH System.out.println(str.toLowerCase()); // a bcd ef gh System.out.println(str.concat("xxx")); // a BcD ef Gh
xxxSystem.out.println(str.length()); //13
System.out.println(str.isEmpty()); //false
System.out.println(str2.isEmpty()); //true