返回值类型为 void 的方法
getBytes (int srcBegin, int srcEnd, byte[] dst, int dstBegin);
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
// 使用指定的字符集将此 String 编码为 byte 序列,
//并将结果存储到一个新的 byte 数组中。
返回值类型为 char 类型的方法
charAt(int index);
public static void main(String[] args) {
// a.charAt();
String a = "我叫奔波霸他叫霸波奔";
//返回 字符串a中下标为0的字符的统一编码号
char value = a.charAt(1);
System.out.println(value);
// unicodeCode = 叫
}
返回值类型为 char[] 类型的方法
getBytes(String charsetName);
public static void main(String[] args) {
// a.getBytes()();
String a = "我叫奔波霸他叫霸波奔";
//使用平台的默认字符集将此 String 编码为 byte 序列
//把该字符串转化为char[]
byte value[] = a.getBytes();
for (byte b: value) {
System.out.print(b+" ,");
}
// = -26 ,-120 ,-111 ,-27 ,-113 ,-85 ,-27 ,-91 ,-108 ,-26 ,-77 ,-94 ,-23 ,-100 ,-72 ,-28 ,-69 ,-106 ,-27 ,-113 ,-85 ,-23 ,-100 ,-72 ,-26 ,-77 ,-94 ,-27 ,-91 ,-108 ,
}
toCharArray();
将此字符串转换为一个新的字符数组。
public static void main(String[] args) {
// a.toCharArray()();
String a = "我叫奔波霸他叫霸波奔";
//将此字符串转换为一个新的字符数组。
char value[] = a.toCharArray();
System.out.println(value);
// value = 我叫奔波霸他叫霸波奔
}
返回值类型为 int 类型的方法
codePointAt(int index);
返回 字符串中下标为 ? 的字符的统一编码号
public static void main(String[] args) {
// a.codePointAt();
String a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//返回 字符串a中下标为0的字符的统一编码号
int unicodeCode = a.codePointAt(0);
System.out.println(unicodeCode);
// unicodeCode = 65
}
codePointBefore(int index);
返回 字符串a中下标为1前面一个字符的统一编码号
public static void main(String[] args) {
// a.codePointBefore();
String a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//返回 字符串a中下标为1前面一个字符的统一编码号
//入参为1,则找下标为0的字符的统一编码(入参为0时会报错)
int unicodeCode = a.codePointBefore(1);
System.out.println(unicodeCode);
// unicodeCode = 65
}
compareTo(String anotherString);
按字典顺序比较两个字符串
public static void main(String[] args) {
// a.compareTo();
String a = "我叫奔波霸他叫霸波奔Abc";
//按字典顺序比较两个字符串
//如果返回值为0代表两个字符串的值完全相同
int unicodeCode = a.compareTo("我叫奔波霸他叫霸波奔Abc");
System.out.println(unicodeCode);
// unicodeCode = 0
}
compareToIgnoreCase(String str);
按字典顺序比较两个字符串,忽略大小写差异
public static void main(String[] args) {
// a.compareToIgnoreCase();
String a = "我叫奔波霸他叫霸波奔Abc";
//按字典顺序比较两个字符串
//与compareTo()方法类似 但这个忽略大小写差异
//如果返回值为0代表两个字符串的值完全相同或忽略大小写差异后完全相同
int unicodeCode = a.compareToIgnoreCase("我叫奔波霸他叫霸波奔ABc");
System.out.println(unicodeCode);
// unicodeCode = 0
}
hashCode();
返回 该字符串的hash码
public static void main(String[] args) {
// a.hashCode()
String a = "我叫奔波霸他叫霸波奔";
String b = "我叫霸波奔他叫奔波霸";
//返回 该字符串的hash码
//相同字符串返回的hashCode一定相同
//不同字符串返回的hashCode可能相同
int hashCodeA = a.hashCode();
int hashCodeB = b.hashCode();
System.out.println(hashCodeA);
System.out.println(hashCodeB);
// hashCodeA= 1584039749
// hashCodeB= -317671099
}
indexOf(String ch);
返回 此字符串中第一次出现指定字符的索引
public static void main(String[] args) {
// a.indexOf()
String a = "我叫奔波霸他叫霸波奔";
//返回 此字符串中第一次出现指定字符的索引。
int index = a.indexOf("霸");
System.out.println(index);
// index = 4
}
indexOf(String ch, int fromIndex);
返回 此字符串中第一次出现指定字符的索引,从指定下标开始
public static void main(String[] args) {
// a.indexOf()
String a = "我叫奔波霸他叫霸波奔";
//返回 此字符串中第一次出现指定字符的索引,从指定下标开始。
int index = a.indexOf("霸",5);
System.out.println(index);
// index = 7
}
lastIndexOf(String ch);
返回 此字符串中最后一次出现指定字符的索引
public static void main(String[] args) {
// a.lastIndexOf()
String a = "我叫奔波霸他叫霸波奔";
//返回 此字符串中最后一次出现指定字符的索引
int index = a.lastIndexOf("霸");
System.out.println(index);
// index = 7
}
lastIndexOf(String ch, int fromIndex);
返回 此字符串中最后一次出现指定字符的索引,从指定的索引开始向后搜索
public static void main(String[] args) {
// a.lastIndexOf()
String a = "我叫奔波霸他叫霸波奔";
//返回 此字符串中最后一次出现指定字符的索引,从指定的索引开始向后搜索。
int index = a.indexOf("霸",6);
System.out.println(index);
// index = 7
}
length()
返回 此字符串的长度。
public static void main(String[] args) {
// a.indexOf()
String a = "我叫奔波霸他叫霸波奔";
//返回 此字符串的长度。
int length= a.length();
System.out.println(length);
// index = 10
}
offsetByCodePoints()
比较冷门,几乎没见哪里用过这个方法,据说在未知字符串中会有用
public static void main(String[] args) {
// a.offsetByCodePoints()
String a = "我叫奔波霸他叫霸波奔";
//第一个参数为要偏移的索引,第二个参数为偏移量。
//比较冷门,几乎没见哪里用过这个方法,据说在未知字符串中会有用
int index = a.offsetByCodePoints(4, 6);
System.out.println(index);
// index = 10
}
codePointCount()
返回此 String 的指定文本范围内的 Unicode 代码点数。
public static void main(String[] args) {
// a.codePointCount()
String a = "我叫奔波霸他叫霸波奔";
//返回此 String 的指定文本范围内的 Unicode 代码点数。
//也是几乎不用
int index = a.codePointCount(1, 6);
System.out.println(index);
// index = 5
}