1,String类的构造方法
(1)public String():空构造.
String s1 = new String();
System.out.println("s1:" + s1);
System.out.println("s1.length():" + s1.length());
结果:
s1:
s1.length:0
(2)public String(byte[] bytes):把字节数组转换成字符串
byte[] bys = { 97, 98, 99, 100, 101 };
String s2 = new String(bys);
System.out.println("s2:" + s2);
System.out.println("s2.length():" + s2.length());
结果:
s2:abcde
s2.length:5
(3)public String (byte[] bytes,int offset,int length):把字节数组的一部分转换成字符串。
byte[] bys = { 97, 98, 99, 100, 101 };
String s3 = new String(bys,1,3);
System.out.println("s3:" + s3);
System.out.println("s3.length:" + s3.length);
结果:
s3:bcd
s3.length:3
(4)public String(char[] value):把字符数组转换成字符串
char[] chs = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };
String s4 = new String(chs);
System.out.println("s4:" + s4);
System.out.println("s4.length():" + s4.length());
结果:
s4:abcdefgh
s4.length:8
(5)public String(char[] value,int offset,int count):把字符数组的一部分转换成字符串。
char[] chs = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'f' };
String s5 = new String(chs, 2, 4);
System.out.println("s5:" + s5);
System.out.println("s5.length():" + s5.length());
结果:
s5:cdef
s5.length:4
(6)public String(String original):把字符常量转换成字符串
String s6 = new String("abcde");
System.out.println("s6:" + s6);
System.out.println("s6.length():" + s6.length());
结果:
s6:abcde
s6.length:5
下面的这一个虽然不是构造方法,但是结果也是一个字符串对象
String s7 = “abcde”;
String s7 = "abcde";
System.out.println("s7:" + s7);
System.out.println("s7.length():" + s7.length());
结果也是一样的:
s7:abcde
s7.length:5
2,字符串的特点
(1):字符串一旦被赋值,就不能改变。
注意:这里指的是字符串的内容不能改变,而不是引用不能改变。
(2):字面值作为字符串对象和通过构造方法创建对象的不同
3,String类的功能
(1)判断功能:
a, boolean equals(Object obj):比较字符串的内容是否相等,区分大小写。
String s1 = "helloworld";
String s2 = "helloworld";
String s3 = "HelloWorld";
System.out.println("equals:" + s1.equals(s2));
System.out.println("equals:" + s1.equals(s3));
结果是:
equals:true
equals:false
b, boolean equalsIgnoreCase(String str):比较字符串的内容是否相等,忽略大小写。
String s1 = "helloworld";
String s2 = "helloworld";
String s3 = "HelloWorld";
System.out.println("equals:" + s1.equalsIgnoreCase(s2));
System.out.println("equals:" + s1.equalsIgnoreCase(s3));
结果是:
String s1 = "helloworld";
String s2 = "helloworld";
String s3 = "HelloWorld";
System.out.println("contains:" + s1.contains("hello"));
System.out.println("contains:" + s1.contains("hw"));
结果是:
equals:true
equals:true
c, boolean contains(String str):判断大字符串中是否含有小字符串。
String s1 = "helloworld";
String s2 = "helloworld";
String s3 = "HelloWorld";
System.out.println("contains:" + s1.contains("hello"));
System.out.println("contains:" + s1.contains("hw"));
结果是:
contains:true
contains:false
d, boolean startsWith(String str):判断字符串是否以某个指定的字符串开头。
String s1 = "helloworld";
String s2 = "helloworld";
String s3 = "HelloWorld";
System.out.println("startsWith:" + s1.startsWith("h"));
System.out.println("startsWith:" + s1.startsWith("hello"));
System.out.println("startsWith:" + s1.startsWith("world"));
结果是:
startsWith:true
startsWith:true
startsWith:false
e, boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾。
String s1 = "helloworld";
String s2 = "helloworld";
String s3 = "HelloWorld";
System.out.println("endsWith:" + s1.endsWith("world"));
System.out.println("endsWith:" + s1.endsWith("hello"));
System.out.println("endsWith:" + s1.endsWith("d"));
结果是:
endsWith:true
endsWith:false
endsWith:true
f , boolean isEmpty():判断字符串是否为空。
String s1 = "helloworld";
String s2 = "helloworld";
String s3 = "HelloWorld";
System.out.println("isEmpty:" + s1.isEmpty());
String s4 = "";
String s5 = null;
System.out.println("isEmpty:" + s4.isEmpty());
结果是
isEmpty:false
isEmpty:true
但是如果是s5.isEmpty()输出结果时,会出现空指针异常(NullPointerException),因为s5不存在对象。
(2)获取功能:
a,int length():获取字符串的长度
String s = "helloworld";
System.out.println("s.length:" + s.length());
结果是:
s.length:10
b,char charAt(int index):获取指定索引位置的字母
String s = "helloworld";
System.out.println("charAt:" + s.charAt(7));
结果是:
charAt:r
c,int indexOf(int ch):返回指定字符在此字符串中第一次出现的索引
System.out.println("indexOf:" + s.indexOf('l'));
结果是:
indexOf:2
d,int indexOf(String str):返回指定字符串在此字符串中第一次出现的索引
System.out.println("indexOf:" + s.indexOf("owo"));
结果是:
indexOf:4
e,int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现的索引
System.out.println("indexOf:" + s.indexOf('l', 4));
System.out.println("indexOf:" + s.indexOf('k', 4));
System.out.println("indexOf:" + s.indexOf('l', 40));
结果是:
indexOf:8
indexOf:-1
indexOf:-1
g,int indexOf(String str,int fromIndex)返回指定字符串在此字符串中从指定位置后第一次出现的索引
System.out.println("indexOf:" + s.indexOf("llo",0));
System.out.println("indexOf:" + s.indexOf("ld",5));
结果是:
indexOf:2
indexOf:8
h,String substring(int start):从指定位置开始截取字符串,默认到结尾
System.out.println("substring:" + s.substring(5));
System.out.println("substring:" + s.substring(0));
结果是:
substring:world
substring:helloworld
i,String substring(int start,int end):从指定位置开始到指定位置结束截取字符串
System.out.println("substring:" + s.substring(3, 8));
System.out.println("substring:" + s.substring(0, s.length()));
结果是:
substring:lowor
substring:helloworld
(3):转换功能
a,byte[] getBytes():把字符串转换为字节数组。
String s = "JavaSE";
byte[] bys = s.getBytes();
for (int x = 0; x < bys.length; x++) {
System.out.println(bys[x]);
}
结果是:
74
97
118
97
83
69
b,char[] toCharArray():把字符串转换为字符数组。
String s = "JavaSE";
char[] chs = s.toCharArray();
for (int x = 0; x < chs.length; x++) {
System.out.println(chs[x]);
}
结果是:
J
a
v
a
S
E
c,static String valueOf(char[] chs):把字符数组转换为字符串。
String s = "JavaSE";
String ss = String.valueOf(chs);
System.out.println(ss);
结果是:
JavaSE
d,static String valueOf(int i):把int类型的数据转换为字符串。
int i = 100;
String sss = String.valueOf(i);
System.out.println(sss);
结果是:
100
e,String toLowerCase():把字符串转换为小写。
System.out.println("toLowerCase:" + s.toLowerCase());
System.out.println("s:" + s);
结果是:
toLowerCase:javase
s:JavaSE
f,String toUpperCase():把字符串转换为大写。
System.out.println("toUpperCase:" + s.toUpperCase());
结果是:
toUpperCase:JAVASE
g,String concat(String str):把字符串拼接。
String s1 = "hello";
String s2 = "world";
String s3 = s1 + s2;
String s4 = s1.concat(s2);
System.out.println("s3:"+s3);
System.out.println("s4:"+s4);
结果是:
s3:helloworld
s4:helloworld
(4):其他功能
a:替换功能
String replace(char old,char new):
String replace(String old,String new)
String s1 = "helloworld";
String s2 = s1.replace('l', 'k');
String s3 = s1.replace("owo", "ak47");
System.out.println("s1:" + s1);
System.out.println("s2:" + s2);
System.out.println("s3:" + s3);
结果是:
s1:helloworld
s2:hekkoworkd
s3:hellak47rld
b:去空格功能
String trim()
String s4 = " hello world ";
String s5 = s4.trim();
System.out.println("s4:" + s4 + "---");
System.out.println("s5:" + s5 + "---");
结果是:
s4: hello world ---
s5:hello world---
c:按字典比较功能
int compareTo(String str)
int compareToIgnoreCase(String str)
String s6 = "hello";
String s7 = "hello";
String s8 = "abc";
String s9 = "xyz";
System.out.println(s6.compareTo(s7));// 0
System.out.println(s6.compareTo(s8));// 7
System.out.println(s6.compareTo(s9));// -16
结果是:
0
7
-16