作者:爱塔居的博客_CSDN博客-JavaSE领域博主
专栏:JavaSE
🌼作者简介:大三学生,希望跟大家一起进步!
文章目录
目录
文章目录
前言
一、构造字符串
二、Sring对象的比较
2.1 比较是否引用同一对象
2.2 比较String内容是否相同
2.3 比较String大小返回差值
三、 字符串查找
3.1 char charAt(int index)的使用
3.2 int indexOf(int ch)的使用
编辑 3.3 int indexOf(int ch,int froIndex)的使用
编辑
3.4 int indexOf(String str)的使用
3.5 int indexOf(String str,int fromIndex)的使用
编辑
3.6 int lastIndexOf(int ch)的使用
四、转化
4.1 数值和字符串转化
编辑 4.2 大小写转换
4.3 字符串转数组
编辑 4.4 格式化
五、字符串替换
5.1 替换所有的指定内容
5.2 替换首次出现的指定内容
六、字符串拆分
6.1 将字符串全部拆分
6.2 将字符串以指定的格式,拆分成limit组
七、截取字符串
7.1 从指定索引截取到结尾
7.2 截取部分内容
八、trim()的用法
总结
前言
我们在C语言中,已经涉及到字符串了,但是在C语言中要表示字符串只能使用字符数组或者字符指针,可以使用标准库提供的字符串系列的函数完成大部分操作。但是这种将数据和操作数据方法分离开的方式不符合面向对象的思想。所以,java语言专门提供了String类。
一、构造字符串
public class Test {
public static void main(String[] args) {
//第一种字符串构造方法:使用常量串构造
String s1="Hello World!";
System.out.println(s1);
//第二种:直接newString对象
String s2=new String("Hello World!");
System.out.println(s2);
//第三种:使用字符串进行构造
char[] array={'H','e','l','l','o',' ','W','o','r','l','d','!'};
String s3=new String(array);
System.out.println(s3);
}
}
输出结果:
🥪注意:
1.String是引用类型,内部并不存储字符串本身。
在12行取断点,Debug:
🍇我们会发现:
1.Java中,字符串没有存/0
2.字符串中其实存的是value数组的地址。
2.在Java中用" "引起来的也是String类型对象
public class Test {
public static void main(String[] args) {
System.out.println("hello".length());//输出字符串hello的长度
}}
输出:
二、Sring对象的比较
2.1 比较是否引用同一对象
🌹对于内置类型来说,==比较的是变量中的值;对于引用类型==比较的是引用中的地址
public class Test {
public static void main(String[] args) {
String s1="Hello";
String s2="Hello";
String s3="Hello World!";
System.out.println(s1==s2);//引用的同一个对象
System.out.println(s1==s3);
System.out.println(s2==s3);
}
}
输出结果:
public class Test {
public static void main(String[] args) {
String s1=new String("Hello");
String s2=new String("Hello");
String s3=new String("Hello World!");
System.out.println(s1==s2);
System.out.println(s1==s3);
System.out.println(s3==s2);
}
}
输出结果:
2.2 比较String内容是否相同
public class Test {
public static void main(String[] args) {
String s1=new String("Hello");
String s2=new String("Hello");
String s3=new String("hello");
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
}
}
输出结果:
2.3 比较String大小返回差值
public class Test {
public static void main(String[] args) {
String s1=new String("Hello");
String s2=new String("Hello");
String s3=new String("Hello World!");
System.out.println(s1.compareTo(s2));
System.out.println(s1.compareTo(s3));
System.out.println(s3.compareTo(s2));
}
}
输出结果:
除了compareTo,还有int compareToIgnoreCase(String str) 。方式相同,但是忽略大小写比较。
public class Test {
public static void main(String[] args) {
String s1=new String("Hello");
String s2=new String("Hello");
String s3=new String("hello");
System.out.println(s1.compareTo(s2));
System.out.println(s1.compareTo(s3));
}
}
输出:
public class Test {
public static void main(String[] args) {
String s1=new String("Hello");
String s2=new String("Hello");
String s3=new String("hello");
System.out.println(s1.compareToIgnoreCase(s2));
System.out.println(s1.compareToIgnoreCase(s3));
}
}
输出结果:
三、 字符串查找
方法 | 功能 |
char charAt(int index) | 返回index位置上字符,如果index为负数或者越界,抛出IndexOutOfBoundsException异常 |
int indexOf(int ch) | 返回ch第一次出现的位置,没有返回-1 |
int indexOf(int ch, int fromIndex) | 从fromIndex位置开始找ch第一次出现的位置,没有返回-1 |
int indexOf(String str) | 返回str第一次出现的位置,没有返回-1 |
int indexOf(String str, int fromIndex) | 从fromIndex位置开始找str第一次出现的位置,没有返回-1 |
int lastIndexOf(int ch) | 从后往前找,返回ch第一次出现的位置,没有返回-1 |
int lastIndexOf(int ch, int fromIndex) | 从fromIndex位置开始找,从后往前找ch第一次出现的位置,没有返回-1 |
int lastIndexOf(String str) | 从后往前找,返回str第一次出现的位置,没有返回-1 |
int lastIndexOf(String str, int fromIndex) | 从fromIndex位置开始找,从后往前找str第一次出现的位置,没有返回-1 |
3.1 char charAt(int index)的使用
作用:返回以 index位置的数字 为坐标下的字符。
public class Test {
public static void main(String[] args) {
String s ="Hello";
System.out.println(s.charAt(1));
}}
输出:
public class Test {
public static void main(String[] args) {
String s ="Hello";
System.out.println(s.charAt(-1));
}}
编译错误:
3.2 int indexOf(int ch)的使用
作用:返回 ch位置下的字符 在字符串中第一次出现的位置坐标
public class Test {
public static void main(String[] args) {
String s ="Hello";
System.out.println(s.indexOf('H'));
System.out.println(s.indexOf('l'));//s中第一次出现l的位置坐标
}}
输出:
3.3 int indexOf(int ch,int froIndex)的使用
作用:返回 以在froIndex位置的数字为坐标开始,查找在ch位置的字符第一次出现的坐标。没有找到的话,就返回-1
public class Test {
public static void main(String[] args) {
String s ="Hello";
System.out.println(s.indexOf('H',0));
System.out.println(s.indexOf('o',0));
System.out.println(s.indexOf('o',1));
System.out.println(s.indexOf('l',0));//找到从0开始,第一个出现的位置坐标
System.out.println(s.indexOf('e',2));//没有找到,返回-1
}}
输出:
3.4 int indexOf(String str)的使用
作用:返回str位置上字符串所在位置坐标。如果没有找到,则返回-1
public class Test {
public static void main(String[] args) {
String s ="Hello";
System.out.println(s.indexOf("el"));
System.out.println(s.indexOf("elo"));
}}
输出:
3.5 int indexOf(String str,int fromIndex)的使用
作用:输出在str位置的字符串,从 以在fromIndex位置数值为坐标 开始查找,返回位置。如果没有找到,输出-1
public class Test {
public static void main(String[] args) {
String s ="Hello";
System.out.println(s.indexOf("ll",0));
System.out.println(s.indexOf("ll",2));
System.out.println(s.indexOf("ll",3));
System.out.println(s.indexOf("llo",0));
}}
结果:
3.6 int lastIndexOf(int ch)的使用
作用:从后往前找,返回ch第一次出现的位置,没有返回-1
public class Test {
public static void main(String[] args) {
String s ="aabbccaa";
System.out.println(s.lastIndexOf("a"));
System.out.println(s.lastIndexOf("e"));
}}
输出结果:
后面的,大家就自己试着去写吧。
四、转化
4.1 数值和字符串转化
public class Test {
public static void main(String[] args) {
String s1 = String.valueOf(12);//数字转换为字符串
String s2 = String.valueOf(0.34);
String s3 = String.valueOf(true);//boolean转字符串
System.out.println(s1+s2);
System.out.println(s3);
int data1 = Integer.parseInt("12");//字符串转数字
double data2 = Double.parseDouble("0.34");
System.out.println(data1+data2);
}
}
输出:
4.2 大小写转换
只转化字母:
public class Test {
public static void main(String[] args) {
String s1="hello world!";
String s2="HELLO WORLD!";
System.out.println(s1.toUpperCase());
System.out.println(s2.toLowerCase());
}
}
输出:
4.3 字符串转数组
public class Test {
public static void main(String[] args) {
String s = "hello";
char[] ch=s.toCharArray(); //字符串转数组
for (int i=0;i<s.length();i++){
System.out.print(ch[i]);
}
System.out.println();
String s1=new String(ch);//数组转字符串
System.out.println(s1);
}
}
输出:
4.4 格式化
public class Test {
public static void main(String[] args) {
String s=String.format("%d年%d月%d日",2022,11,23);
System.out.println(s);
}
}
输出结果:
五、字符串替换
使用一个指定的新的字符串替换掉已有的字符串数据:
5.1 替换所有的指定内容
public class Test {
public static void main(String[] args) {
String s1="hello!";
System.out.println(s1.replaceAll("!","?"));;
System.out.println(s1);
}
}
5.2 替换首次出现的指定内容
public class Test {
public static void main(String[] args) {
String s1="helloworld";
System.out.println(s1.replaceFirst("o","-"));;
System.out.println(s1);
}
}
输出:

public class Test {
public static void main(String[] args) {
String s1="helloworld";
String s2=s1.replaceFirst("o","-");
System.out.println(s2);
}
}
输出结果:
🍨我们会注意到:由于字符串是不可变对象,替换不修改当前的字符串,而是产生一个新的字符串
六、字符串拆分
6.1 将字符串全部拆分
public class Test {
public static void main(String[] args) {
String s1="hello my friends";
String[] s2=s1.split(" ");
for (String s:s2){
System.out.println(s);
}
}
}
输出:
6.2 将字符串以指定的格式,拆分成limit组
public class Test {
public static void main(String[] args) {
String s1="hello my friends";
String[] s2=s1.split(" ",2);
for (String s:s2){
System.out.println(s);
}
}
}
输出:
注意!!!
1.字符"|","*","+","."都要加上转义字符,前面加上"\\"
2.如果是"\",那么就写成"\\\\"
3.如果一个字符串中有多个分隔符,那么用"|"作为连字符
public class Test {
public static void main(String[] args) {
String s1="192.168.1.1";
String[] s2=s1.split("\\.");
for (String s:s2){
System.out.println(s);
}
}
}
多次拆分:
public class Test {
public static void main(String[] args) {
String str = "name=zhangsan&age=18" ;
String[] result = str.split("&") ;
for (int i = 0; i < result.length; i++) {
String[] temp = result[i].split("=") ;
System.out.println(temp[0]+" = "+temp[1]);
}
}
}
输出:

七、截取字符串
7.1 从指定索引截取到结尾
public class Test {
public static void main(String[] args) {
String str = "hello world";
System.out.println(str.substring(2));
}
}
输出:
7.2 截取部分内容
public class Test {
public static void main(String[] args) {
String str = "hello world";
System.out.println(str.substring(2,7));
}
}
输出:
🍅注意:
1.索引从0开始
2.前闭后开,substring(2,7)表示包含2下标的字符,不包括7下标的字符
八、trim()的用法
功能:会去掉字符串开头和结尾的空白字符(空格, 换行, 制表符等),保留中间空格
public class Test {
public static void main(String[] args) {
String str = " 红楼 梦 ";
System.out.println("《"+str.trim()+"》");
}
}
输出: