• 深入分析String类型(一)


        字符串是我们日常开发中使用频繁的一种数据类型。在java中字符串类主要有String、StringBuilder、StringBuffer这三个类,其中String表示的是不可变字符串,StringBuilder和Stringbuffe表示的是可变字符串。

       在java中创建一个字符串比较的简单,可以直接通过常量来定义字符串:String str = "hello world",也可以通过创建对象的方式来创建字符串:String str = new String("hello world")。在java自己提供的api里面封装一些简单的操作字符串的方法,例如:

     public static void main(String[] args) {
            String str = "";
            //public boolean isEmpty() 判断字符串是否为空
            System.out.println(str.isEmpty());  //true
            str="hello world";
            System.out.println(str.isEmpty());  //false
            
            //public String substring(int beginIndex, int endIndex) //截取在字符串 (左闭右开)
            str = "suyang-java"; //截取yang
            str = str.substring(2, 6); 
            System.out.println(str);
            
            //public int indexOf(String str) //查找子串,从前向后找
            str = "suyang-java";
            int start = str.indexOf("a");
            System.out.println(start);  //3
            
            //public int lastIndexOf(String str) //查找子串,从后向前找
            str = "suyang-java";
            int last = str.lastIndexOf("a");
            System.out.println(last); //10
            
            //public boolean contains(CharSequence s) //判断字符串是否包含指定的字符
            str = "suyang-java";
            System.out.println(str.contains("java"));//true
            
            //public boolean startsWith(String prefix) //判断字符串是否已指定的字符开头
            str = "suyang-java";
            System.out.println(str.startsWith("suyang"));//true
            
            
            //public boolean endsWith(String suffix) //判断字符串是否已指定的字符结尾
            str = "suyang-java";
            System.out.println(str.endsWith("java"));//true
            
            //public String toUpperCase() 把字符串中所有的字符转化为大写
            str = "suyang-java";
            System.out.println(str.toUpperCase()); //SUYANG-JAVA
            
            //public String toLowerCase() 把字符串中所有的字符转化为小写
            str = "SUYANG-JAVA";
            System.out.println(str.toLowerCase()); //suyang-java
            
        }

    String类中封装的方法都是比较的简单易用,在平时的开发中也使用的比较的频繁。

        String类中提供了几个方法来返回字符串的编码后的byte数组。

    public byte[] getBytes()
    public byte[] getBytes(String charsetName)
    public byte[] getBytes(Charset charset)

        第一个方法没有参数,使用的是系统的默认编码;第二个和第三个都是可以手动的指定字符串按照何种编码方式编码。String类中提供了对应的构造方法将编码后的字节数组转化为字符串。例如:

      public static void main(String[] args) throws UnsupportedEncodingException {
            String str = "好好学习,天天向上";
            byte[] b1 = str.getBytes();
            byte[] b2 = str.getBytes("GBK");
            String str1 = new String(b1);
            String str2 = new String(b2,"GBK");
            System.out.println(str1);
            System.out.println(str2);
         }

    这里在多说一点,在java中使用Charset类来表示各种的编码,其中Charset.defaultCharset().name()返回的就是系统默认的编码。

        在java中通过String类创建的字符串都是不可变的。在String api中提供的那些看似修改字符串内容的方法其实都是复制原来的字符串数组然后新建一个字符串,例如substring方法其内部的实现方式是:

    private final char value[];
    
    public String substring(int beginIndex, int endIndex) {
            if (beginIndex < 0) {
                throw new StringIndexOutOfBoundsException(beginIndex);
            }
            if (endIndex > value.length) {
                throw new StringIndexOutOfBoundsException(endIndex);
            }
            int subLen = endIndex - beginIndex;
            if (subLen < 0) {
                throw new StringIndexOutOfBoundsException(subLen);
            }
            return ((beginIndex == 0) && (endIndex == value.length)) ? this
                    : new String(value, beginIndex, subLen);
        }

    和包装类类似,String类定义为不可变,程序可以变得更加的简单,安全但是如果频繁的修改字符串而每次修改都去新建一个字符串那么效率就会下降,这时我们优先考虑使用StringBuilder和StringBuffer类。

        

  • 相关阅读:
    虚拟机安装CentOS不能联网的解决
    64位openSUSE12.3最完整的安装QQ的方法
    打水井
    一个阶乘中末尾零的个数
    DiscuzX开发手册【精品】
    一个获取PHP消耗时间的小函数
    php获取本月的第一天与最后一天
    在博客园创建了一个自己的博客~
    ie6 fixed 很纠结~这个js就解决了
    现在各个网站都在使用瀑布流布局吧~
  • 原文地址:https://www.cnblogs.com/suyang-java/p/10661137.html
Copyright © 2020-2023  润新知