zoukankan      html  css  js  c++  java
  • 23java的String类常用方法

    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()+"	"+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);
    

    运行结果:

    自学java,请多多指教!
  • 相关阅读:
    只要三步,使用html5+js实现像素风头像生成器
    按Ctrl+Enter发送的实现
    “放到桌面”的Servlet实现
    从tom大叔那想着拿书的,呵呵。
    也写dateUtil.js
    智习室
    零基础爬虫课,不会编程也能做爬虫
    1小时教你学会如何采集微博数据:0基础小白也能轻松学会!
    TransactionScope 分布式事务配置
    centos7创建共享文件夹
  • 原文地址:https://www.cnblogs.com/fanfada/p/13835913.html
Copyright © 2011-2022 走看看