字符串String知识总结(下) 冲冲冲!!!

  1. 字符串的比较

区分大小写的比较 equals()

public boolean equals(Object anObject)

不区分大小写的比较 equalsIanoreCase()

Public boolean equalsIanoreCase(String anotherString)

比较两个字符串的大小关系 compareTo()

public int compareTo(String anotherString)

该方法返回一个整型,该数据会根据大小关系返回三类内容:
相等:返回0
小于:返回内容小于0
大于:返回内容大于0

  1. 字符串的查找

判断一个子字符串是否存在 contains()

public boolean contains(CharSequence s)

从头开始查找指定字符串的位置,查到返回位置的开始索引,查不到返回-1 indexOf()

public int indexOf(String str)

如果要找的子字符串重复出现,只返回第一次出现的位置
也可以通过再传一个参数的方法,来从指定位置开始查找该子字符串第一次出现的位置

public int indexOf(String str, int fromIndex)

还有从后到前查找的方法 lastIndexOf()

public int lastIndexOf(String str)

同样的,内容重复的时候只返回第一次出现的位置,也可以传一个参数来从指定位置开始查找

public int lastIndexOf(String str, int fromIndex)
String str = "helloworld" ; 
System.out.println(str.indexOf("l")); // 2 
System.out.println(str.indexOf("l",5)); // 8 
System.out.println(str.lastIndexOf("l")); // 8 

判断字符串的开头或结尾

String str = "**@@helloworld!!" ; 
System.out.println(str.startsWith("**")); // true 
System.out.println(str.startsWith("@@",2)); // ture 
System.out.println(str.endsWith("!!")); // true 
  1. 字符串的替换

替换所有指定内容

public Stirng replaceAll(String regex, String replacement)

替换首个内容

public Stirng replaceFirst(String regex, String replacement)
String str = "helloworld" ;
System.out.println(str.replaceAll("l", "_"));
System.out.println(str.replaceFirst("l", "_"));

运行结果为:

he__owor_d
he_loworld

注意: 由于字符串是不可变对象, 替换不修改当前字符串, 而是产生一个新的字符串

  1. 字符串的拆分

将字符串全部拆分

public String[] split(String regex)

将字符串部分拆分,该数组长度就是limit极限

public String[] split(String regex, int limit)

代码示例: 实现字符串的拆分处理

String str = "hello world hello dagongren" ;
        String[] result = str.split(" ") ; // 按照空格拆分
        for(String s: result) {
            System.out.println(s);
        }

代码示例: 字符串的部分拆分

 String str = "hello world hello dagongren" ;
        String[] result = str.split(" ",2) ;
        for(String s: result) {
            System.out.println(s);
        }

有些特殊字符作为分割符可能无法正确切分, 需要加上转义

代码示例: 拆分IP地址

String str = "192.168.1.1" ; 
String[] result = str.split("\\.") ;
//用增强for()循环来遍历整个字符串 
for(String s: result) {   
 System.out.println(s); 
} 

运行结果为

192
168
1
1

注意事项:

  • 字符" | " 、 " * " 、" + " 都得加上转义字符,前面加上" \ "
  • 而如果是"",那么就得写成" \\ "
  • 如果一个字符串中有多个分隔符,可以用 " | " 作为连字符
  1. 字符串的截取

从指定索引截取到结尾

public Stirng substring(int beginIndex)

截取部分内容

public Stirng substring(int beginIndex, int endIndex)
String str = "helloworld" ; 
System.out.println(str.substring(5)); 
//区间都是左闭右开 [0,5)
System.out.println(str.substring(0, 5)); 

运行结果为:

world
hello
  1. 字符串的其他操作

去掉字符串左右两边的空格,保留中间的空格

public String trim()

大小写转换

//转大写
public String toUpperCase()
//转小写
public String toLowerCase()

获取字符串长度

public int length()

判断字符串是否为空(长度为0)

public boolean isEmpty()
  1. StringBuffer和StringBuilder
  • StringBuffer是线程安全的,是单线程的。StringBuilder是在jdk1.5版本的时候提出的,但是它不是线程安全的,支持多线程

  • StringBuffer和StringBuilder用法基本相同

  • String和StringBuffer类不能直接转换。

    如果要想互相转换,可以采用如下原则:
    String变为StringBuffer : 利用StringBuffer的构造方法或append()方法
    StringBuffer变为String : 调用toString()方法。

StringBuffer和StringBuilder与String的区别是:

  • String是一个不可变的字符序列,StringBuffer和StringBuilder是可变的字符数组
  • StringBuilder比StringBuffer有速度优势,所以多数情况下建议使用StringBuilder。但是在要求程序线程安全的情况下必须使用StringBuffer
  • 频繁修改字符串的情况考虑使用StingBuffer。

StringBuffer和StringBuilder常用的一些方法:

  1. append()方法

在字符串之后新增字符串

	StringBuffer str = new StringBuffer("abaaba");
        str.append("lalala");
        System.out.println(str);

运行结果为:abaabalalala

  1. reverse()方法

翻转字符串

	StringBuffer sb = new StringBuffer("helloworld"); 
		sb.reverse();
		System.out.println(sb); 

运行结果为:dlrowolleh

  1. insert()方法

插入字符串

	StringBuffer str = new StringBuffer("haha");
        str.insert(2,"www");
        System.out.println(str);

运行结果为:hawwwha

  1. delete()方法

删除一个区间内的字符串

	StringBuffer str = new StringBuffer("abcdefg");
		sb.delete(2,5);
        System.out.println(sb);

运行结果为:abfg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值