大家好,欢迎来到IT知识分享网。
目录
一、String存在意义
二、字符串为何不可变
三、String类常用方法
1、字符串构造
2、String对象的比较
3、字符串查找
4、转化
(1)数值和字符转化
(2)大小写转换
(3)字符串转数组
(4)格式化
5、字符串替换
6、字符串拆分
7、字符串截取
8、字符串修改
9、其他操作方法
四、StringBuilder和StringBuffer的使用和方法介绍
五、StringBuilder和StringBuffer的不同
第一个final是不能被继承我们理解,那么第二个为啥不是字符串被修改的主要原因呢,上代码:
public static void main(String[] args) {
final char[] str={‘1′,’2′,’3’};
str[0]=’4′;
System.out.println(str);
}
打印出来的结果是:
为何会这样呢?主要得益于final修饰的一般是地址不能变,而修改数组里面的东西,地址是不会变化的,所以就算被fianl修饰,char[]里面的内容还是可以被修改。 而private私有化封装才是字符串不能被修改的本质原因。
三、String类常用方法
1、字符串构造
对于字符串的构造,常用的就三种构造方法——常量串构造,使用newString对象构造,使用字符数组构造,用代码来写如下:
public static void main(String[] args) {
//使用常量串构造
String s1=”wqe”;
//使用newString对象
String s2=new String(“wqer”);
//使用字符数组进行构造
char[] str={‘w’,’q’,’e’};
String s3=new String(s2);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
打印结果如下:
String类还提供了很多中字符串构造方法,而常用的就这三种
对于==符号比较,比较的就是地址,equals方法比较的是里面的值是否相等,而且还是boolean类型,而compareTo方法是按顺序比较里面的字符串,相等则继续比较下一个,不相等则返回差值,值得注意的是,compareTo方法是按照ASC码里面的值进行比较,而对于相同的长度的字符串,返回-1或者1(根据前后俩个字符串哪个更长,前者减去后者)。
public static void main(String[] args) {
String q1=”abc”;
String q2=”abcA”;
String q3=”abZ”;
String q4=”abc”;
String q5=q1;
//==比较地址
System.out.println(q1 == q5);
System.out.println(q1 == q3);
System.out.println(q1 == q2);
System.out.println(“===========”);
//equals方法比较值
System.out.println(q1.equals(q5));
System.out.println(q1.equals(q3));
System.out.println(q1.equals(q2));
//compareTo桉顺序比较,不同返回差值,如果字符串长度不一样,而返回-1或者1
System.out.println(“===========”);
System.out.println(q1.compareTo(q4));
System.out.println(q2.compareTo(q1));
System.out.println(q1.compareTo(q3));
}
而打印出来的结果是:
总的来说,String类的比较方式还是挺常用的,可以根据自己需要去做出不同的比较方式。
方法 功能
char charAt(int index) 返回index位置上字符,如果index为负数或者越界,抛出IndexOutOfBoundsException
int indexOf(int ch) 返回ch第一次出现的位置,没有返回-1
int index(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
public static void main(String[] args) {
String s=”abcabcabcqwertyuiopz”;
System.out.println(s.charAt(3));//a
System.out.println(s.indexOf(‘a’));//0
System.out.println(s.indexOf(‘a’, 3));//3
System.out.println(s.indexOf(“cab”));//2
System.out.println(s.indexOf(“cab”, 5));//5
System.out.println(s.lastIndexOf(3));//-1(没有找到)
System.out.println(s.lastIndexOf(“a”, 10));//6
System.out.println(s.lastIndexOf(“cabc”));//5
System.out.println(s.lastIndexOf(“cabc”,7));//5
}
主要查找的方法就是这些,索引,向前往后查找和从后往前查找,查找字符串或者字符,第二个参数就是从第几个开始查找。
4、转化
(1)数值和字符转化
数值和字符转化可以分为数值转化为字符串,也可以是字符串转化为数值,以代码为例:
public static void main(String[] args) {
//数字转字符串(通过String.valueOf)
String s1=String.valueOf(1234);
String s2=String.valueOf(12.34);
String s3=String.valueOf(true);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(“============”);
int data1=Integer.parseInt(“1234”);
int data2=Integer.parseInt(“12534”);
System.out.println(data1);
System.out.println(data2);
}
打印结果如下:
.value()是转换为字符串的用法,而包装类.转换类型()是字符串转换为数值的方法。
public static void main(String[] args) {
String q1=”qwe”;
String q2=”QWE”;
System.out.println(q1.toUpperCase());
System.out.println(q1.toLowerCase());
}
打印结果为:
public static void main(String[] args) {
String w1=”qwerty”;
//字符串转数组
char[] ch= w1.toCharArray();
for (int i = 0; i < ch.length; i++) {
System.out.println(ch[i]);
}
System.out.println();
//数组转字符串
String s2=new String(ch);
System.out.println(s2);
}
打印结果如下:
public static void main(String[] args) {
String e1=String.format(“%d-%d-%d”,2019,9,14);
System.out.println(e1);
}
格式化主要是按照里面的格式打印,对于设置日期这些有很大帮助。
需要new一个对象,代码如下:
public static void main(String[] args) {
//StringBuffer sbd=”qwe”;//会报错
StringBuilder sbd=new StringBuilder(“wqwq”);
StringBuffer sbf=new StringBuffer(“asd”);
System.out.println(sbd);
System.out.println(sbf);
}
打印出来结果是:
那么它们有什么不同呢?方法上有很大不同,我列出一个表格:
方法 说明
StringBuff append(String str) 在尾部追加,相当于String的+=,可以追加:boolean、char、char[]、double、float、int、long、Object、String、StringBuff的变量
char charAt(int index) 获取index位置的字符
int length() 获取字符串的长度
int capacity() 获取底层保存字符串空间总的大小
void ensureCapacity(int mininmumCapacity) 扩容
void setCharAt(int index,char ch) 将index位置的字符设置为ch
int indexOf(String str) 返回str第一次出现的位置
int indexOf(String str,int fromIndex) 从fromIndex位置开始查找str第一次出现的位置
int lastIndexOf(String str) 返回最后一次出现str的位置
int lastIndexOf(String str,int fromIndex) 从fromIndex位置开始找str最后一次出现的位置
StringBuffer insert(int offset,String str)
在offset位置插入:八种基本类型&String类型&
Object类型数据
StringBufferdeleteCharAt(int index) 删除index位置字符
StringBuffer delete(int start, int end) 删除[start,end)区间内的字符
StringBuffer replace(int start, int end, String str) 将[start,end)位置的字符替换为str
tring substring(int start) 从start开始一直到末尾的字符以String的方式返回
String substring(int start, int end)将[start,end) 将[start,end)范围内的字符以String的方式返回
StringBuffer reverse() 反转字符串
ring toString() 将所有字符按照String的方式返回
五、StringBuilder和StringBuffer的不同
StringBuilder更适合于单线程,StringBuffer更适合多线程,为什么呢,我们打开这俩个的里面内容来看:
这个是StringBuffer的,它的里面有锁,更适合多线程,而StringBuilder里面是没有锁的,更适合单线程,但速度更快。
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/gug12345sheh/article/details/
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/141013.html