String的用法、StringBuffer和StringBuilder之间的区别

String 类包括的方法可用于检查序列的单个字符、比较字符串、搜索字符串、提取子字符串、创建字符串副本并将所有字符全部转换为大写或小写。大小写映射基于 Character 类指定的 Unicode 标准版。

1、检查序列的单个字符:

 String c = "abc".substring(2, 3);
 System.out.println("c" + c);
 String d = "abc".substring(1);
 System.out.println("d" + d);

输出结果:c
        bc

两个参数的substring方法的意思是开始位置到结束位置。
一个参数的substring方法的意思是从哪个位置开始一直到结束。

2、比较字符:

 @Override public boolean equals(Object other) {
        if (other == this) {
          return true;
        }
        if (other instanceof String) {
            String s = (String)other;
            int count = this.count;
            if (s.count != count) {
                return false;
            }
            if (hashCode() != s.hashCode()) {
                return false;
            }
            for (int i = 0; i < count; ++i) {
                if (charAt(i) != s.charAt(i)) {
                    return false;
                }
            }
            return true;
        } else {
            return false;
        }
    }

这是源码,可以看出有五个比较步骤:
第一个就是如果能直接等(比如int类型的)就返回true。
第二个是判断是否属于String;
第三步判断字符数是否相等;
第四部判断哈希码是否相等;
最后判断每一个字符是否相等;

3、搜索字符串

public int indexOf(String string) {
        int start = 0;
        int subCount = string.count;
        int _count = count;
        if (subCount > 0) {
            if (subCount > _count) {
                return -1;
            }
            char firstChar = string.charAt(0);
            while (true) {
                int i = indexOf(firstChar, start);
                if (i == -1 || subCount + i > _count) {
                    return -1; // handles subCount > count || start >= count
                }
                int o1 = i, o2 = 0;
                while (++o2 < subCount && charAt(++o1) == string.charAt(o2)) {
                    // Intentionally empty
                }
                if (o2 == subCount) {
                    return i;
                }
                start = i + 1;
            }
        }
        return start < _count ? start : _count;
    }

public int indexOf(String subString, int start) {
        if (start < 0) {
            start = 0;
        }
        int subCount = subString.count;
        int _count = count;
        if (subCount > 0) {
            if (subCount + start > _count) {
                return -1;
            }
            char firstChar = subString.charAt(0);
            while (true) {
                int i = indexOf(firstChar, start);
                if (i == -1 || subCount + i > _count) {
                    return -1; // handles subCount > count || start >= count
                }
                int o1 = i, o2 = 0;
                while (++o2 < subCount && charAt(++o1) == subString.charAt(o2)) {
                    // Intentionally empty
                }
                if (o2 == subCount) {
                    return i;
                }
                start = i + 1;
            }
        }
        return start < _count ? start : _count;
    }

4、创建字符串

String str = “abc”;
等效于:
char data[] = {‘a’, ‘b’, ‘c’};
String str = new String(data);
所以两种创建方式都可以。

5、endsWith(String suffix)和startsWith(String prefix)

通过后缀和前缀的对比返回布尔类型值。
多数是用于判断文件格式。

6、split(String regex)

通过这个方法拆分字符串,返回数组。

7、valueOf(float f)。

强转用这个方法更加好。静态方法可以直接调用。那个类型类调用就转化为什么类型。

8、转化编码格式。

try {
           byte[] b =  "cde".getBytes("UTF-8");
           String str =  new String(b);
        }catch(Exception e){
            e.toString();
        }

9、StringBuffer和StringBuilder之间的区别?

这两个类都能进行字符拼接,我们主要在什么时候用哪个类。
1、首先通过API可以看到StringBuffer,里面方法带synchronized关键字,说明StringBuffer是同步线程操作,换句话说这是线程安全的。
2、通过API看StringBuilder,方法里面不带synchronized关键字,说明StringBuilder并不存在线程问题,
换句话说这是线程不安全的。
3、最后总结,单线程操作,我们可以使用StringBuilder,多线程操作,我们使用StringBuffer。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老李与GPT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值