一、字符与字符串
public static void main(String[] args) {
char[] data = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g'};
//1. 将字符数组中的所有内容变为字符串
String str = new String(data);
System.out.println(str);
//2.将部分字符数组中的内容变为字符串
String str2 = new String(data,2,3);
System.out.println(str2);
//3.取得指定索引位置的字符
String str3 = "hello";
System.out.println(str3.charAt(4));
//4.将字符串变为字符数组返回
String str4 = "world";
char[] data2 = str4.toCharArray();
for(int i = 0;i<data2.length;i++){
System.out.print(data2[i]+"、");
}
}
}
运行结果:
abcdefg
cde
o
w、o、r、l、d、
二、字节与字符串
public static void main(String[] args) {
//1.将字符转换成字节数组
String str = "helloworld";
byte[] data = str.getBytes();
for(int i = 0;i<data.length;i++){
System.out.print(data[i]+"、");
}
//2.将字节数组变为字符串
System.out.println(new String(data));
//3.将部分字节数组中的内容转换成字符串
System.out.println(new String(data,3,4));
}
}
运行结果:
104、101、108、108、111、119、111、114、108、100、
helloworld
lowo
三、字符串比较
public static void main(String[] args) {
String str1 = "hello";
String str2 = "Hello";
String str3 = "hello";
String str4 = "aello";
//1. 区分大小写的内容比较
System.out.println(str1.equals(str2));
System.out.println(str1.equals(str3));
//2. 不区分大小写的内容比较
System.out.println(str1.equalsIgnoreCase(str2));
//3.比较两个字符串的大小关系
System.out.println(str3.compareTo(str4));
// 1. 相等:返回0.
// 2. 小于:返回内容小于0.
// 3. 大于:返回内容大于0
}
}
运行结果:
false
true
true
7
四、字符串查找
public static void main(String[] args) {
String str1 = "abcdefg";
//1.判断一个子字符串是否存在
System.out.println(str1.contains("efg"));
//2.从头开始查找指定字符串的位置,查到了返回位置的开始索引,如果查找不到返回-1
System.out.println(str1.indexOf("efg"));//找到了
System.out.println(str1.indexOf("obc"));//找不到
//3.从指定位置开始查找子字符串的位置
System.out.println(str1.indexOf("cde", 2));
//4.由后向前查找子字符串的位置
System.out.println(str1.lastIndexOf("efg"));
//5.从指定位置由后向前查找
System.out.println(str1.lastIndexOf("efg",5));
//6.判断是否以指定字符串开头
System.out.println(str1.startsWith("a"));
//7.从指定位置判断是否以指定字符串开头
System.out.println(str1.startsWith("a",0));
//8.判断是否以指定字符串结尾
System.out.println(str1.endsWith("efg"));
}
}
运行结果:
true
4
-1
2
4
4
true
true
true
五、字符串拆分
public static void main(String[] args) {
String str1 = "hello world";
//1.替换所有的指定内容
System.out.println(str1.replaceAll("l","_"));
//2.替换首个内容
System.out.println(str1.replaceFirst("l","_"));
}
}
运行结果:
he__o wor_d
he_lo world
六、字符串拆分
public static void main(String[] args) {
String str1 = "hello world hello china!";
//1.将字符串全部拆分
String[] result = str1.split(" ");
for(String s:result){
System.out.println(s);
}
//2.将字符串部分拆分,该数组长度就是limit极限。
String[] result2 = str1.split(" ",3);
for(String s:result2){
System.out.println(s);
}
}
}
运行结果:
hello
world
hello
china!
hello
world
hello china!
七、字符串截取
public static void main(String[] args) {
String str1 = "helloworld";
//1.从指定索引截取到结尾
System.out.println(str1.substring(5));
//2.截取部分内容
System.out.println(str1.substring(1,5));//左闭右开
}
}
运行结果:
world
ello
八、字符串的其他操作方法
public static void main(String[] args) {
String str1 = " Hello World ";
//1.去掉字符串中的左右空格,保留中间的空格
System.out.println(str1.trim());
//2.字符串转大写
System.out.println(str1.toUpperCase());
//3.字符串转小写
System.out.println(str1.toLowerCase());
//4.字符串入池操作
System.out.println(str1.intern());
//5.字符串连接,等同于“+”,不入池
System.out.println(str1.concat("hello china!"));
//6.取得字符串长度
System.out.println(str1.length());
//7.判读是否为空字符串,但不是NULL,而是长度为0
System.out.println(str1.isEmpty());
}
}
运行结果:
Hello World
HELLO WORLD
hello world
Hello World
Hello World hello china!
13
false