String常用字符串
字符串的创建方式
(1)直接赋值
String str = "jk"
(2)通过构造器创建
String str = new String("kk");
构造器可以传入字符数组或字节数组
1:charAt 截取一个字符串,返回一个字符
String str = "lihui001"
str.charAt(i) //i为要截取的字符在字符串中的下标
2:getchars //截取一段连续的字符,存放在新字符数组里,无返回值
String str = "lihui001"
char[] ch = new char[10];
str.getchars(a,b,c,d) //a:要截取的开始下标 b:要截取的结束下标 c:存放截取的所有字符的字符数组 d:存放在字符数组中的位置
3:getBytes 将字符串转换为字节数组,返回字节数组
String str = "lihui001"
byte[] bytes = str.getBytes()
4:tocharArray 将字符串转换成字符数组
String str = "lihui001"
char[] chars = str.tocharArray();
5:equals() 字符串比较大小,区分大小写,返回布尔值
String str = "lihui001"
String str2 = "lihui001"
str.equals(str2)
6:equalsIgnoreCase() 字符串比较大小,不区分大小写,返回布尔值
String str1 = "li"
String str2 = "LI"
str1.equalsIgnoreCase(str2)
7:startsWith() endsWith() //判断字符串是不是以特定的字符开头或结束
String a = "Hello Word";
a.startsWith("ee");
a.endsWith("d");
8: toUpperCase() toLowerCase() //将字符串转换为大写或小写
String str = "lihui";
String s = str.toUpperCase();
String ss = str.toLowerCase();
9:concat() //字符串连接
String str = "lihui";
String s = "ll";
String aa = str.concat(s);
10:trim() //去掉起始和末尾的空格
String a = " Hello Word ";
System.out.println(a.trim());
11:substring() //截取字符串
String a = "Hello Word";
System.out.println(a.substring(0, 5));
System.out.println(a.substring(6));
12:replase() //可以替换字符串
String a = "Hello Word"; String b = "你好";
System.out.println(b.replace("你", "大家"));