23java的String类常用方法

序号23及其以前的博客都是由博客园搬运过来的。

https://www.cnblogs.com/fanfada/

String 类

String类是包含于java.lang包里面的。

下面根据String的源码来学习String类(由于需要反复测试,为避免重复,所以只放程序的主体部分)。

  • String类的对象的内存关系:

String str1 = new String("abc");
        String str2 = new String("abc");
        System.out.println(str1.equals(str2));
        System.out.println(str1 == str2);
        String str3 = "def";
        String str4 = "def";
        System.out.println(str3 == str4);
        System.out.println(str3.equals(str4));

运行结果:

为何会出现这种情况?

下面看这四个字符串的内存图,str1和str2都是使用new创建的新的String类的对象它们指向的不是同一个地址,对象str1/str2储存的是它们保存字符串的地址,显然str1和str2的内容相同,但是内存地址不同。对于str3和str4,是字符串常量,它们的内容是保存在常量池中的,JDK的管理机制就是如果常量池中保存有这个内容,再次创建的对象保存的内容是常量池中已经存在的,那么就不再开辟新的内存保存相同的内容,而是让这个新的对象直接指向原来的内容。也就是说:str3和str4的内容的地址是同一个,得证。

 

  • String类的charAt()方法:

String str4 = "def";
	System.out.println(str4.charAt(2));//查找index=2所在位置的字符
        System.out.println(str4.indexOf('f'));//查找字符'f'所在位置的索引

运行结果:

显然,charAt()方法是寻找字符串中某个索引位置的字符。如果超出字符串的索引,会报错:

看这个错误信息:

第一行提示:StringIndexOutOfboundsException,字符串超出索引异常;

第二行提示:at java.lang.String.charAt(String.java:658),提示源码中charAt()方法所在的位置,下面是源码:

public char charAt(int index) {
        if ((index < 0) || (index >= value.length)) {
            throw new StringIndexOutOfBoundsException(index);
        }
        return value[index];
    }

第三行提示:at testString.myString.main(myString.java:14),提示这个错误在本程序中的位置

  • String类中的substring()方法

String str5 = "qwert";
	String substr5=str5.substring(2);
        System.out.println(substr5);

运行结果:

功能显然:返回字符串中指定位置到末尾的一个新字符串,源码如下:

/**
     * Returns a string that is a substring of this string. The
     * substring begins with the character at the specified index and
     * extends to the end of this string. <p>
**/
public String substring(int beginIndex) {
        if (beginIndex < 0) {
            throw new StringIndexOutOfBoundsException(beginIndex);
        }
        int subLen = value.length - beginIndex;
        if (subLen < 0) {
            throw new StringIndexOutOfBoundsException(subLen);
        }
        return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
    }

注意:这里的截取的一个字符串是一个新的字符串对象。

  • String类中replace()方法

String str6 = "asd";
        String str7 = str6.replace('s', 'x');
        System.out.println(str7);

运行结果:

  • String类中split()方法

String str8 = "ffd,gfj,hhh";
        String[] str8Array = str8.split(",");
        for (int i = 0; i < str8Array.length; i++) {
            System.out.println(str8Array[i]);
        }

运行结果:

  • String类的其他方法

String str7 = "  aa  bb  ";
        String str77 = str7.trim();
        System.out.println(str77.length()+"\t"+str77);

        System.out.println("Abc".equalsIgnoreCase("abc"));
        System.out.println("Abcbd".indexOf('b'));
        System.out.println("Abcbd".lastIndexOf('b'));
        System.out.println("Abcbd".startsWith("Ab"));
        System.out.println("Abcbd".endsWith("bd"));
        System.out.println("Abcbd".toLowerCase());
        System.out.println("Abcbd".toUpperCase());

        System.out.println("##################");
        String gh = new String("a");
        for (int i = 0; i < 10; i++) {
            gh = gh + i;
        }
        System.out.println(gh);

运行结果:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值