String类详解(二)

32 篇文章 0 订阅

1.8字符串比较
boolean equals():区分大小写的比较
boolean equalsIgnoreCase():不区分大小写的比较
int compareTo(String anotherString): 当碰到第一个不相等的字符时,终止比较,返回两个字符的ASCII码差值。其中:当
>0时,表示当前字符串 > 目标字符串
=0时,表示当前字符串 = 目标字符串
<0时,表示当前字符串 < 目标字符串

System.out.println("A".compareTo("a")); // -32
System.out.println("a".compareTo("A")); // 32
System.out.println("A".compareTo("A")); // 0
System.out.println("AB".compareTo("AC")); // -1
System.out.println("刘".compareTo("杨"));//-5456

1.9******字符串查找***************
从完整的字符串中判断指定内容是否存在
I.public boolean contains(String str):判断一个子字符串是否存在
II.public int indexOf(String str):从头开始查找字符串的位置,查到了返回位置的开始索引,如果查不到返回-1
III.public int lastIndexOf(String str):从后往前查找指定字符串,若找到返回开始索引,否则返回-1

String str="hello,world";
System.out.println(str.contains("h"));//true
System.out.println(str.indexOf("e"));//1
System.out.println(str.lastIndexOf("l"));//9

IV.public boolean startsWith(String prefix):判断是否以指定字符串开头
public boolean endsWith(String sufffix):判断是否以指定字符串结尾

String str="@@@##hello!!";
System.out.println(str.startsWith("@"));//true
System.out.println(str.endsWith("!"));//true

1.10 字符串替换
I.public String replaceAll(String regex,String replacment):将regex内容的字符串全部替换为replacment
II.public String replaceFirst(String regex,String replacment):替换首个内容

String str="A-B";
System.out.println(str.replaceAll("A","*"));//*-B
System.out.println(str.replaceFirst("A","8"));//8-B

1.11 字符串拆分
讲一个完整的字符串按照指定的格式拆分成若干个子字符串
public String[] split(String regex):将字符串全部拆分
public String[] split(String regex,int limit):将字符串按照指定格式拆分成limit个子字符串

String str ="127.0.0.1";//拆分IP地址
//若拆分的结果为空,则指定的字符串需要转义
String[] result= str.split("\\.");
for(String str1:result){
    System.out.println(str1);//127 0 0 1
}
String[] result= str.split("\\.",2);//拆成两个子字符串
for(String str1:result){
    System.out.println(str1);
}

1.12字符串截取
public String[] substring(int beginIndex):从指定索引位置开始截取到字符串结尾
public String[] substring(int beginIndex, int endIndex):截取部分内容

String str ="hello world";
System.out.println(str.substring(5));//输出时包括空格
System.out.println(str.substring(0,5)+"c");//输出时不包括空格

应用:首字母大写
用到了字符串的截取和转换成大写字母两种方法

public class StringClassTest {
    public static void main(String[] args) {
        String str="hello";
        System.out.print(upperCaseFirstChar(str));
    }
    public static String upperCaseFirstChar(String str){
        return str.substring(0,1).toUpperCase()+str.substring(1);
    }
}

1.13其它操作方法
public String toUpperCase():转换成全大写字母
public String toLowerCase():装换成全小写字母
public String trim():去掉字符串中的左右空格,保留中间空格
public native String intern():字符串入池操作
public String concat(String str):字符串连接
public int length():返回字符串长度,长度包括空格
public boolean isEmpty():判断是否为空字符串,但不是null,而是长度为0

if(str==null || str.isEmpty()){
    System.out.println("空字符串");
}

以后在判断空字符串时,一定要先判断字符串是否为空,再用isEmpty方法

1.14两个sb类(StringBuffer,StringBuilder)
由于字符串常量不可变更,为了方便进行字符内容的修改,引入两个SB类,与String类完全不是一个东西
在字符串中使用"+"进行字符串内容的拼接操作,这种拼接操作会产生大量垃圾空间(因为字符串常量不可变更),引入两个SB类后,字符串拼接操作改为调用append(各种数据类型)方法:public synchronized StringBuffer append(各种数据类型 b)

1.14.1 String与SB的相互转换:
1.String类变为StringBuffer类
I.调用StringBuffer的构造方法
II.调用append()

2.StringBuffer类变为String类
I.调用toString()

String str="test";
StringBuffer sb=new StringBuffer(str);
sb.append("hello").append("world");
System.out.println(sb.toString());//testhelloworld

StringBuffer和StringBuilder类中的append()方法
public synchronized StringBuffer append(Object obj)
public StringBuilder append(Object obj)

1.14.2面试题:请解释String类和StringBuffer、StringBuilder的区别
I.字符串常量不可变更,而stringBuffer与StringBuilder内容可以修改(append).
String变量在使用“+”进行字符串拼接时,javac编译器会将String变量变为StringBuilder而后进行append处理。
II.StringBuffer采用同步处理,线程安全,性能较低;
StringBuilder采用异步处理,线程不安全,性能较高
1.14.3 StringBuffer中独有的方法:
字符串反转:public synchronized StringBuffer reverse();
字符串删除:public synchronized StringBuffer delete(int start,int end);
字符串插入:public synchronized StringBuffer insert(int offset,Object obj);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值