String类
string类概述: 字符串本质就是字符数组
字符串特点:
- java程序中的所有字符串字面值(如“abc”)都作为此类的实例实现
- 字符串是常量,他们的值在创建之后不能更改,但是可以被共享
- 字符串缓冲区支持可变的字符串(stringbuffer和stringbuilder)
常用方法(一)
public String()
public String(String original)
public String(char[] value) 将字符数组转换成字符串
public String(char[] value, int index, int count) 将字符数组的一部分转换成字符串
public String(byte[] bytes)
public String(byte[] bytes, int offset, int length)
String(byte[] bytes, String charsetName) 后面讲解IO编码的时候讲解
java.lang.StringIndexOutOfBoundsException
异常名称: 字符串索引越界
产生原因: 访问了字符串所对应的字符数组的索引范围
解决办法: 自己检查索引是否超出了字符串的范围
public class StringDemo02 {
public static void main(String[] args) throws UnsupportedEncodingException {
String s1 = "";
String s2 = new String();
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
// public String(String original)
String s3 = new String("abc");
System.out.println(s3);
// public String(char[] value)
char[] chs = {'a', 'b', 'c', 'd'};
String s4 = new String(chs);
System.out.println(s4);
// public String(char[] value, int offset, int count)
String s5 = new String(chs, 0, 3);
System.out.println(s5);
// public String(byte[] bytes)
System.out.println(new String(new byte[] {97,98,99}));
// public String(byte[] bytes, int offset, int length)
System.out.println(new String(new byte[]{97,98,99,100}, 0, 2));
// String(byte[] bytes, String charsetName)
String s6 = new String("中国你好".getBytes(), "gbk");
System.out.println(s6);
}
}
常用方法(二)
char charAt(int index) 返回某个索引出的字符
int indexOf(int ch)
int indexOf(String str) 返回某个字符串在源字符串中第一次出现的索引
int indexOf(int ch,int fromIndex) 从fromIndex索引开始从左往右找,第一次出现ch字符的索引
int indexOf(String str,int fromIndex)
int lastIndexOf(int ch) 从右边往左边找,第一次出现ch的索引
int lastIndexOf(int ch,int fromIndex)
int lastIndexOf(String str,int fromIndex)
String substring(int start)
String substring(int start,int end)
int length()
public class StringDemo03 {
public static void main(String[] args) {
String s1 = "abchelloDEbhellofhello";
// char charAt(int index)
char ch = s1.charAt(0);
System.out.println(ch);
System.out.println("-------------------");
// int indexOf(int ch)
int index = s1.indexOf('E');
System.out.println("index:" + index);
System.out.println("-------------------");
// int indexOf(String str)
System.out.println("indexOf:" + s1.indexOf("bc"));
System.out.println("--------------------");
// int indexOf(int ch,int fromIndex)
// 从fromIndex索引开始,查找第一次出现ch字符的索引
System.out.println("indexOf:" + s1.indexOf('b', 6));
System.out.println("--------------------");
// int indexOf(String str,int fromIndex)
System.out.println("indexOf:" + s1.indexOf("hello", 6));
System.out.println("--------------------");
// int lastIndexOf(int ch)
System.out.println("lastIndexOf:" + s1.lastIndexOf("hello", 12));
String s2 = "HelloWorldEverybody";
// String substring(int start)
String substring = s2.substring(5);
System.out.println(substring);
System.out.println(s2.substring(5, 10));
CharSequence sequence = s2.subSequence(5, 10);
System.out.println(sequence);
}
}
常用方法(三)
boolean isEmpty():判断字符串是否为空。
boolean equals(Object obj):将此字符串的内容与指定的对象比较,区分大小写。
boolean equalsIgnoreCase(String str):将此 String 与另一个 String 比较,忽略大小写。
boolean contains(String str):判断字符串中是否包含方法传入的字符串。
boolean startsWith(String str):判断字符串是否以某个指定的字符串开头。
boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾。
public class StringDemo05 {
public static void main(String[] args) {
String s = "HelloWorld"; // "" + H + "" + e + ""
String s2 = "";
String s3 = "HELLOWORLD";
System.out.println("isEmpty: " + s2.isEmpty());
System.out.println("equals: " + s.equals(s3));
System.out.println("equalsIgnoreCase: " + s.equalsIgnoreCase(s3));
System.out.println("contains: " + s.contains("Hello"));
System.out.println("contains: " + s.contains(""));
System.out.println("startsWith: " + s.startsWith(""));
System.out.println("startsWith: " + s.startsWith("Hello"));
System.out.println("endsWith: " + s.endsWith("World"));
System.out.println("endsWith: " + s.endsWith(""));
}
}
(四)
byte[] getBytes() :将字符串转化为字节数组。
char[] toCharArray(): 将字符串转化为字符数组。
static String valueOf(char[] chs): 返回 char 数组参数的字符串表示形式。
static String valueOf(int i) :返回 int 参数的字符串表示形式。
String toLowerCase() :将此 String 中的所有字符都转换为小写。
String toUpperCase() :将此 String 中的所有字符都转换为大写。
String concat(String str): 将指定字符串连接到此字符串的结尾。
public class StringDemo07 {
public static void main(String[] args) {
String s1 = "abcEFefg";
byte[] bys = s1.getBytes();
for (int i = 0; i < bys.length; i++) {
byte b = bys[i];
System.out.println((char)b);
}
System.out.println("-----------------------");
char[] chs = s1.toCharArray();
System.out.println(Arrays.toString(chs));
for (int i = 0; i < chs.length; i++) {
char c = chs[i];
System.out.println((short)c);
}
System.out.println("------------------------");
// static String valueOf(char[] chs): 返回 char 数组参数的字符串表示形式。
String result = String.valueOf(false);
System.out.println(result);
System.out.println(String.valueOf(new Student()));
System.out.println("-----------------------");
/*
* String toLowerCase() :将此 String 中的所有字符都转换为小写。
String toUpperCase() :将此 String 中的所有字符都转换为大写。
*/
System.out.println(s1.toLowerCase());
System.out.println(s1.toUpperCase());
System.out.println("-------------------------");
// String concat(String str): 将指定字符串连接到此字符串的结尾。
String s2 = "Hello";
String s3 = "World";
String concat = s2.concat(s3);
System.out.println(concat);
}
}
(六)
String replace(char old,char new) :替换功能。
String replace(String old,String new) :替换功能。
String trim():去除字符串两空格。
int compareTo(String str) :按字典顺序比较两个字符串。
int compareToIgnoreCase(String str):按字典顺序比较两个字符串,忽略大小写。
public String[] split(String regex):分隔字符串成字符数组。
compareTo源码解析
String s3 = "abcd";
String s4 = "abdd";
int compareTo = s3.compareTo(s4);
public int compareTo(String anotherString) {
int len1 = value.length; "abcd" 4
int len2 = anotherString.value.length; "abdd" 4
int lim = Math.min(len1, len2);
char v1[] = value; "abcd"
char v2[] = anotherString.value; "abcd"
int k = 0;
while (k < lim) {
char c1 = v1[k]; a b c
char c2 = v2[k]; a b d
if (c1 != c2) {
return c1 - c2; 99 - 100 -1
}
k++;
}
return len1 - len2;
}
public class StringDemo09 {
public static void main(String[] args) {
// String replace(char old,char new) :替换功能。
String s1 = "HelloWorldJava";
String replace1 = s1.replace('H', 'h');
System.out.println(replace1);
String replace2 = s1.replace("Java", "Android");
System.out.println(replace2);
String replace3 = s1.replaceAll("", "隔壁老王");
System.out.println(replace3);
String replace4 = s1.replaceFirst("", "隔壁老王");
System.out.println(replace4);
System.out.println("----------------------");
String s2 = " Good Good Study ";
System.out.println(s2.trim() + "-----------");
System.out.println("-----------------------");
// int compareTo(String str) :按字典顺序比较两个字符串。
String s3 = "abcd";
String s4 = "abdd";
int compareTo = s3.compareTo(s4);
int compareTo2 = s4.compareTo(s3);
System.out.println(compareTo);
System.out.println(compareTo2);
System.out.println("---------------------");
String s5 = "G-o-o-d-Boy";
String[] strs = s5.split("-");
System.out.println(Arrays.toString(strs));
}
}