zoukankan      html  css  js  c++  java
  • java基础---->编码方式_char型变量_字符串处理 2

    第一部分 字符的编码方式

    • 概述:每一个字符都可以对应于一个二进制码(整型数值),这个过程称作编码过程
      •  对字符进行编码,编码成二进制数值可以表示的形式,有利于计算机操作
      •  目前为止,已经存在多种比较成熟的编码方式,如ASCIIISO-8859-1Unicodegbkgbk2312等等编码方式
      • 不同编码方式下包含的字符集是不同的,同一个字符在不同编码方式下的编码结果是不同的,不同编码方式下每个字符的编码结果所占用的存储空间是不同的。

    编码方式

    ASCII

    ISO-8859-1

    Unicode

    Utf-8

    gbk

    单个字符编码结果所占用的存储空间大小

    7 bit

    8 bit 

    16 bit

    变长(1 byte或者 3 byte

     

    编码结果取值范围

    0-127

    0-255

    0-63535

     

     

    涵盖的字符集

    英文字符以及其他常用字符

    ASCII码所有字符+德语、西班牙语西欧语言中的字符

    ISO-8859-1码中的所有字符+其他语言中的字符

    Unicode码所涵盖的字符集一样

    中文字符

    编码方式(即字符与整形数值之间的对应关系)

    ASCII码表

    ASCII码表中相同的字符编码结果是高位为0,地位仍和ASCII码相同;

    ASCII码表之外的其他字符见iso-8859-1

    都是使用两个字节表示一个字符的Unicode编码结果:高位为0,低位仍和ISO-8859-1表中编码结果相同;ISO-8859-1码表之外的字符编码结果见Unicode

    ISO-8859-1部分的字符都使用1byte来表示,还和ISO-8859-1中的编码结果相同;

    ISO-8859-1码之外的字符使用3byte来表示,最高byte使用111*****;后面的两个byte都以10开头,其他6bit为有效位

     

     

    第二部分 Java8种基本数据类型中的char

    • 概述:charJava8种基本数据类型中的一种,对应的编码方式是Unicode
      •  每个char型变量占用2byte的内存空间,正好可以表示Unicode码下的所有字符
      •  char型变量有三种赋值方式:
        •   方法一,char ch=’a’;//使用单引号括住的Unicode码表中的单个字符
        •   方法二,char ch=97;//单个字符对应于Unicode码表中的位置
        •      方法三,char ch=’u2605’;//转义字符
      •  charJava的基本API中有一个封装类,java.lang.Character,这个类将char变量封装成一个类对象,并且提供了若干常用操作方法

    第三部分 Java中的字符串处理

    • 概述:

      • Java中的字符串其实就是单个字符串起来的,字符串也是遵循Unicode编码方式
      • Java的常用API中也有若干用于字符串操作的类:
        • 如8种基本数据类型对应的封装类(Byte/Short/Integer/Long/Float/Double/Boolean/Character)中,都有将基本数据类型转变成String对象的成员函数。
        • 如java.lang.String类,见eclipse中javaTest工程下package char_and_string中的JavaAPI_String.java中就讲述了String类中的构造函数、成员函数的用法。
        • 如java.lang.StringBuffer类,见eclipse中javaTest工程下package char_and_string中的JavaAPI_StringBuffer.java 中就讲述了StringBuffer类中的构造函数、成员函数的用法。
        • 如java.lang.StringBuilder类,见eclipse中javaTest工程下package char_and_string中的JavaAPI_StringBuilder.java 中就讲述了StringBuilder类中的构造函数、成员函数的用法。
        • 编写程序展示String类和StringBuilder/StringBuffer类的差别
          • 在需要不停地改变String对象的值的时候,最好用StringBuilder/StringBuffer。因为String类是不停地创建新的字符串,占用新的内存空间,而StringBuffer/StringBuilder则是在原有内存地址处修改字符串,在StringBuilder/StringBuffer内部array没有溢出之前,都不会改变内存,溢出时则会扩展internal array的容量
    • 字符处理相关API——java.lang.String类

     

    /**
     * @author lxrm
     * @date 20170208
     * @description:本文件讲述javaAPI之String类的用法,用于字符串生成、处理
     * java.lang.String
     *         概述:String类对象所存储的字符串中的所有字符也是Unicode编码的
     *             String类中有许多成员方法:包括字符串比较、子串查找、获取子串、复制字符串、转换大小写等多种函数
     *         String类中的构造函数
     *             1)public String()
     *             2)public String(String original)
     *                 参数:是一个String对象
     *                 结果:创建了一个和参数有着相同值的新的String对象
     *             3)public String(char value[]) 
     *                 参数:char型数组
     *             4)public String(char value[], int offset, int count)
     *                 参数:char型数组,初始位置,子串长度
     *                 结果:新建的String对象的值是参数value:char[] 的子集,子串是从value[offset]开始,长度为count的子串
     *             5)public String(int[] codePoints, int offset, int count)
     *                 参数:codePoints:int[] int型数组,其中每个元素的值是字符在Unicode表中的位置(也即单个字符对应的Unicode编码结果)
     *                     offset:int  初始字符下标
     *                     count:int 子串长度
     *                 结果:新建的String对象的值是参数所代表的字符串的子串,子串=codePoints[offset]开始,长度为count的字符串
     *             6)public String(byte bytes[], int offset, int length, String charsetName)
     *                 功能:将byte数组转变成Unicode码下的字符,并且将其串成子串赋值给新建的String对象
     *                  @param  bytes
     *                          The bytes to be decoded into characters
     *                  @param  offset
     *                          The index of the first byte to decode
     *                  @param  length    
     *                          The number of bytes to decode
     *                  @param  charsetName
     *                          The name of a supported {@linkplain java.nio.charset.Charsetcharset}
     *               public String(byte bytes[], String charsetName)
     *                 备注:和上述函数唯一的不同之处是,这个函数不用指定待转换byte数组的初始位置和长度,因为这个函数将对整个byte数组进行转换,
     *                     将byte数组值转变成对应编码方式charsetName下的字符,然后串成字符串赋值给新建的String对象
     *               public String(byte bytes[], int offset, int length, Charset charset)
     *                 功能:和6)中的构造函数功能一致,只不过这个构造函数的第四个参数是Charset型的,和6)中的不一样
     *               public String(byte bytes[], Charset charset)
     *                 备注:直接将   整个  byte数组按照charset对应的解码方式将数值解析成字符,然后串成字符串
     *               public String(byte bytes[], int offset, int length)
     *                 备注:使用这个平台的默认编码方式进行解码(即将byte数值解析成对应编码表中的相应字符)
     *               public String(byte bytes[])
     *                 备注:使用当前平抬中的默认编码方式进行解码,对整个byte数组进行byte值到character(当前平台默认编码方式下的字符)的转换
     *             7)public String(StringBuffer buffer)
     *                 功能:创建和stringBuffer中字符序列相同的String对象
     *             8)public String(StringBuilder builder)
     *             9)一个被弃用的构造函数,弃用原因:该构造函数并不能保证ASCII码到Character(即Unicode码)字符的转换是正确的
     *             @Deprecated
     *            public String(byte ascii[], int hibyte, int offset, int count)
     *                功能:可以将一个ASCII码字符数组转变成Unicode码字符串,并且取转换结果中的字符串的子串作为新建String对象的值
     *                注意:这个函数已经被弃用了
     *
     * 
     *         String类中的成员函数
     *             1)public int length()串中字符数
     *             2)public boolean isEmpty()串中字符数为0时返回值为true
     *             3)public char charAt(int index)串中特定位置的单个字符,index取值范围:0~length-1
     *               public int codePointAt(int index)
     *                 第二个函数和第一个函数的不同之处在于:第二个函数返回值不是单个字符,而是单个字符在Unicode码表中的位置
     *               public int codePointBefore(int index)
     *                 这个函数和第二个函数的不同之处在于,这个函数返回index:int前面一个字符对应的Unicode码
     *             4)public int codePointCount(int beginIndex, int endIndex) 
     *                 功能:返回beginIndex和endIndex之间有多少个Unicode字符
     *             5)public int offsetByCodePoints(int index, int codePointOffset)
     *                 返回值:index偏移codePointOffset之后的index
     *             6)void getChars(char dst[], int dstBegin)
     *                 功能:Copy characters from this string into dst starting at dstBegin.
     *                  This method doesn't perform any range checking.
     *                  即将string值复制到char型数组中,存放在该数组的dst[dstBegin],dst[dstBegin+1]...位置上
     *            public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)
     *                功能:将String字符串的一部分字符复制到一个char型数组中
     *                    具体来说是将String[srcBegin]~String[srcEnd-1]这些字符复制到
     *                    dst[dstBegin],dst[dstBegin+1]...位置上
     *          7)public byte[] getBytes(String charsetName)
     *                功能:将String对象按照参数charsetName:String所指定的编码方式进行编码(即,将字符转变成对应编码方式下的整型数值),
     *                    并且将字符对应的编码结果存放在一个byte数组中返回
     *              public byte[] getBytes(Charset charset)
     *                备注:和上述函数的不同之处在于参数类型不同
     *              public byte[] getBytes()
     *                备注:和上述两个函数的不同之处在于,这个函数不去显式指定编码方式,而是默认使用该平台的默认编码方式
     *            8)public boolean equals(Object anObject)
     *                返回值:只有参数也是String对象并且和调用者拥有相同的值时返回值为true
     *              public boolean contentEquals(StringBuffer sb)
     *                功能:将String对象和StringBuffer对象比较,若两者内容相同则返回值为true
     *              public boolean contentEquals(CharSequence cs)
     *                功能:将String对象和CharSequence对象作比较,若两者内容相同则返回值为true
     *              public boolean equalsIgnoreCase(String anotherString)
     *                功能:比较两个String对象的内容是否相同(不考虑大小写)
     *              public boolean regionMatches(int toffset, String other, int ooffset,int len) 
     *                备注:这个函数用于比较两个String对象的子串是否相同
     *              public boolean regionMatches(boolean ignoreCase, int toffset,
     *                          String other, int ooffset, int len)
     *              备注:这个函数也是用于比较两个String对象的子串是否相同,但这个函数功能更多一些,因为这个函数可以控制比较时是否考虑字母的大小写
     *            9)public int compareTo(String anotherString)
     *                返回值:若两个string对象的值相同,则返回值是0
     *                     若string比参数中的字符小(按照在Unicode表中的位置比较两个字符的大小),则返回值是一个负整数
     *                     若string比参数中的字符大,则返回值是一个正整数
     *                    (返回值=this.charAt(k)-anotherString.charAt(k)
     *                            或者this.length()-anothorString.length())
     *             public int compareToIgnoreCase(String str)
     *                备注:和上述函数的不同之处在于这个函数在比较两个字符串是忽略大小写
     *            10)public boolean startsWith(String prefix, int toffset)
     *                功能:Tests if the substring of this string beginning at the
     *                     specified index starts with the specified prefix.
     *              public boolean startsWith(String prefix)
     *                功能:看字符串是否是以特定的前缀开头的
     *              public boolean endsWith(String suffix)
     *            11)public int hashCode()
     *                功能:返回字符串所对应的hash码=s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
     *            12)public int indexOf(int ch)
     *                功能:返回String对象中字符(ch:int)第一次出现的位置下标
     *                    如果串中没有指定的字符,返回值是-1
     *                参数:ch:int 单个字符对应的Unicode码(即字符在Unicode码表中的位置)
     *               public int indexOf(int ch, int fromIndex)
     *                备注:和上述函数的不同之处在于,这个函数是从String[fromIndex]开始往后寻找指定字符ch:int
     *               public int lastIndexOf(int ch)
     *                返回值:指定字符在字符串中最后一次出现的位置下标,如果串中没有指定字符则返回值为-1
     *               public int lastIndexOf(int ch, int fromIndex)
     *                备注:和上述第三个函数的不同之处在于,这个函数是从String[fromIndex]开始查找
     *            13)public int indexOf(String str)
     *                功能:Returns the index within this string of the first occurrence of the specified substring.
     *               public int indexOf(String str, int fromIndex)
     *                备注:和上述函数的不同之处在于,这个函数是从String[fromIndex]开始查找
     *               public int lastIndexOf(String str)
     *               public int lastIndexOf(String str, int fromIndex)
     *            14)static int indexOf(char[] source, int sourceOffset, int sourceCount,
     *                     char[] target, int targetOffset, int targetCount,
     *                     int fromIndex)
     *              功能:在char型数组source:char[]中查找target:char[]
     *             static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
     *                          char[] target, int targetOffset, int targetCount,
     *                         int fromIndex)
     *          15)public String substring(int beginIndex)
     *                  功能:取子串,返回值为从beginIndex开始到String尾部的子字符串
     *             public String substring(int beginIndex, int endIndex) 
     *                     返回值:String[beginIndex]~String[endIndex-1]
     *             public CharSequence subSequence(int beginIndex, int endIndex)
     *                     备注:这个函数和上述第二个函数功能相同,唯一的不同之处在于返回值类型不同,分别为String对象和CharSequence对象
     *             public boolean contains(CharSequence s)
     *                     功能:判断是否含有子串s
     *          16)public String concat(String str)
     *                  功能:将参数str:String 连接到原有字符串的末尾组合成一个新的字符串并返回
     *          17)public boolean matches(String regex)
     *                  功能:判断该String的值是否符合正则表达式regex:String
     *          18)public String replace(char oldChar, char newChar) 
     *                  功能:用newChar:char替换掉String中的oldChar:char
     *             public String replaceFirst(String regex, String replacement)
     *                     功能:找到该String对象中第一个满足正则表达式regex:String的子串
     *                         并且使用replacement:String替换上述子串
     *             public String replaceAll(String regex, String replacement)
     *                     备注:这个函数和上述第二个函数的不同之处在于,上述第二个函数只是替换掉第一个满足条件的子串,
     *                         而这个函数则是替换掉所有符合正则表达式的子串
     *             public String replace(CharSequence target, CharSequence replacement) 
     *                     功能:使用replacement:CharSequence替换掉该String对象中的所有target:CharSequence
     *          19)分解字符串,分割成若干子串
     *                public String[] split(String regex, int limit)
     *                     功能:根据正则表达式regex:String所定义的规则,分割字符串,将分割结果存放到String[]中并返回
     *                     参数:regex:String 正则表达式,定义了字符串分割方法
     *                         limit:int  限制返回值String[]的元素个数,如果给定的limit为一个正整数,那么返回值String[]的大小不会超过limit
     *                                     如果给定的limit参数是一个小于或等于0的整数,那么返回值String[]的大小没有限制
     *                public String[] split(String regex)
     *                     备注:这个函数相当于上述函数的第二个参数limit:int=0的情况,即返回值String[]的大小没有限制
     *          20)public String toLowerCase(Locale locale)
     *             public String toLowerCase()
     *             public String toUpperCase(Locale locale)
     *             public String toUpperCase()
     *              
     *          21)public String trim()
     *                  功能:删掉String对象中首部以及尾部的空格
     *                  omit省略,删除
     *          22)public String toString()
     *                  返回值:该String对象本身
     *          23)public char[] toCharArray() 
     *                  功能:String对象转变成char型数组
     *          24)public static String format(String format, Object ... args)
     *                  功能:使得参数args:Object按照format:String所指定的格式被格式化成格式化的字符串,
     *                      并且使格式化之后的新字符串使用本地默认的语言环境
     *                      如将Date对象格式化为format所指定的格式的字符串
     *                      再如将其他任意类型数值转变成十进制、十六进制字符串,转换成字符,布尔型字符串等等。
     *                  @param format:String  通过此参数说明期待的格式,format:String中常常含有一些特殊的字符来表示期待的格式,
     *                                          如"%b"与%B用来表示期望相应的参数被格式化为boolean型的数值,示例true
     *                                        再如%h与%H用来表示期望相应的参数被格式化为散列码,示例A05A5198
     *                                       如...(更多特殊字符参见博客:“java基础--->应用实例--->日期及时间字符串格式化”一文)
     *                  @param args:Object  可以有0个或者多个该类型参数
     *                                      该类型参数是参数format:String中所引用的变量
     *                  @return 按照要求格式化所得的新的字符串
     *             public static String format(Locale l, String format, Object ... args)
     *                     备注:这个函数和上述函数唯一的不同之处在于,这个函数可以通过参数Locale来指定格式化过程中所使用的语言环境
     *                         而上述函数只能默认地使用本地语言环境
     *          25)public static String valueOf(Object obj) 
     *                  功能:如果参数为空,那么返回值=“null”
     *                      如果参数不为空,那么返回值=obj.toString();
     *             public static String valueOf(char data[])
     *                     返回值:值=char数组的所有字符串成的字符串
     *             public static String copyValueOf(char data[])
     *                     返回值:值=char数组的所有字符串成的字符串
     *             public static String valueOf(char data[], int offset, int count) 
     *             public static String copyValueOf(char data[], int offset, int count)
     *                     返回值:值=char数组的    部分字符   串成的字符串
     *             public static String valueOf(boolean b)
     *                     返回值:如果参数为true,则返回值为值为“true”的String对象
     *                             如果参数为false,则返回值为值为“false”的String对象
     *             public static String valueOf(char c)
     *                     返回值:长度为1的String对象
     *             public static String valueOf(int i)
     *             public static String valueOf(long l)
     *             public static String valueOf(float f)
     *             public static String valueOf(double d)
     *          26)public native String intern();
     *          
     *
     * 使用实例:
     *         实例一,创建String对象
     *             方法一,直接等于双引号包裹的字符串:"字符串"
     *             方法二,使用构造函数public String(char value[])
     *             方法三,使用构造函数public String(char value[], int offset, int count)
     *             方法四,使用构造函数public String(int[] codePoints, int offset, int count)
     *             方法五,将byte数组转变成char字符,然后创建String对象
     *                   使用构造函数public String(byte bytes[], int offset, int length, String charsetName)
     *                 或者public String(byte bytes[], String charsetName)
     *                 或者public String(byte bytes[], int offset, int length, Charset charset)
     *                 或者public String(byte bytes[], Charset charset)
     *                 或者public String(byte bytes[], int offset, int length)
     *                 或者public String(byte bytes[])
     *         实例二,String类的成员方法---subString()的用法
     *         实例十九,字符串分割
     *             方法一,调用String类的public String[] split(String regex, int limit)
     *             方法二,调用String类的public String[] split(String regex)
     *         实例二十一,删除字符串首部以及尾部的空白格
     *         实例二十四,format函数的应用
     *             第一部分:用于格式化日期和时间(Date对象)
     *                 方法一,调用String类中的public static String format(String format, Object ... args)
     *                 方法二,调用String类中的public static String format(Locale l,String format, Object ... args)
     *             第二部分:用于格式化一般形式的参数(其他类型对象)
     *                 方法一,调用String类中的public static String format(String format, Object ... args)
     *                 方法二,调用String类中的public static String format(Locale l,String format, Object ... args)
     *         */
    package char_and_string;
    
    import java.io.UnsupportedEncodingException;
    import java.nio.charset.Charset;
    import java.util.Date;
    import java.util.Locale;
    
    public class JavaAPI_String {
        public static void main(String[] args) throws UnsupportedEncodingException{
            //实例一,创建String对象
            //方法一,
            String str11 = "abc";
            //方法二,
            char data[] = {'a', 'b', 'c'};
            String str12 = new String(data);
            System.out.println("输出字符串str12:"+str12);//结果:输出字符串str12:abc
            //方法三,
            String str13=new String(data,1,2);
            System.out.println("输出字符串str13:"+str13);//结果:输出字符串str13:bc
            //方法四,
            int charPoints[]={97,98,99};
            String str14=new String(charPoints,1,2);
            System.out.println("输出字符串str14:"+str14);//结果:输出字符串str14:bc
            //方法五
            byte bytes[]={0,97,97,98,0,99,0,97};
            String str15=new String(bytes,0,6,"UTF-8");
            String str16=new String(bytes,0,6,"Unicode");
            String str17=new String(bytes,"UTF-8");
            String str18=new String(bytes,0,6);
            System.out.println("输出字符串str15:"+str15);//结果:输出字符串str15:空格aab空格c
            System.out.println("输出字符串str16:"+str16);//结果:a慢c
            System.out.println("输出字符串str17:"+str17);//结果:空格aab空格c空格a
            System.out.println("输出字符串str18:"+str18);//结果:和str15结果一样,说明该平台下默认编码方式是UTF-8
            
            //实例二,成员方法subString()的用法
            String c = "abc".substring(2,3);
            String str3="abc";
            String d = str3.substring(1, 2);
            
            //实例五,使用标准输出流输出字符串
            String str4="test";
            String str5="hello";
            System.out.println(str4);
            System.out.println("str4="+str4);
            System.out.println(str5+str4);//结果:hellotest
            
            //实例十九,字符串分割
            System.out.println("
    使用String类中的成员方法按照一定规则对字符串进行分割:");
            //             方法一,调用String类的public String[] split(String regex, int limit)
            System.out.println("1)调用myStr.split(regex,limit)");
            String str191="lxrm:chen:wang:ma:li";
            String strs[]=str191.split(":",3);
            for(int i=0;i<strs.length;i++){
                System.out.println("strs["+i+"]="+strs[i]);
            }
            //             方法二,调用String类的public String[] split(String regex)
            System.out.println("2)调用myStr.split(regex)");
            String str192="lxrm:chen:wang:ma:li";
            String strs2[]=str191.split(":");
            for(int i=0;i<strs2.length;i++){
                System.out.println("strs2["+i+"]="+strs2[i]);
            }
            
            //实例二十一,删除字符串首部以及尾部的空白格
            System.out.println("
    删除字符串首部以及尾部的空白格:");
            String str211=" hello world! ";
            String str212=str211.trim();
            System.out.println("删除前:"+str211+"   str.length()="+str211.length());
            System.out.println("删除后:"+str212+"   str2.length()="+str212.length());
            
            //实例二十四,format()函数的应用实例
                //第一部分:用于格式化日期和时间(Date对象)
            System.out.println("String类中的format()函数的使用实例:");
            System.out.println("1)使用String类中的format()函数格式化日期与时间字符串");
                    //实例一
            Date date=new Date();//带格式化日期
            String format1="%te";//期待的格式:如本例中是显示该日期对应于一个月中的哪一天
            System.out.println("今天是本月的第:"+String.format(format1, date)+"天");//结果:今天是本月的第8天
                    //实例二
            String year=String.format("%tY", date);
            String month=String.format("%tm", date);
            String day=String.format("%td", date);
            String hour=String.format("%tH", date);
            String minute=String.format("%tM", date);
            System.out.println(year+"-"+month+"-"+day+" "+hour+":"+minute);//结果:2017-02-08 20:13
                    //实例三,直接使用组合形式
            System.out.println(String.format("%tF", date));//结果:2017-02-08
                //第二部分:用于格式化一般形式的参数(其他类型对象)
            System.out.println("2)使用String类中的format()函数格式化一般类型参数");
            String str241=String.format("%d", 400/2);
            String str242=String.format("%b",3>5);
            String str243=String.format("%x",200);
            System.out.println("十进制显示:"+str241);//结果:200
            System.out.println("布尔型显示:"+str242);//结果:false
            System.out.println("十六进制显示:"+str243);//结果:c8
        }
    }
    •  处理字符串的JavaAPI——StringBuffer

        • /**
           * @author chen
           * @date 20170303
           * @description 本程序讲解java.lang.StringBuffer类的用法
           * java.lang.StringBuffer
           *     概述:
           *         1)StringBuffer类的特点
           *             a.它是线程安全的,相应的地方有synchronized修饰符
           *             b.它的值是可变的字符序列,每个StringBuffer对象都有一个容量,在其值超过该容量的时候,StringBuffer对象会自动扩充其容量(创建一个新的internal buffer array)
               StringBuffer对象用于存储 A mutable【可变的】 sequence of characters. * c.java API中还有一个和这个API有着类似功能的class——StringBuilder,不过StringBuilder是线程不安全的,即没有synchronized * 类成员变量 * 构造函数 * 1)public StringBuffer(): * 创建一个StringBuffer对象 * 该对象的初始值为null * 该对象的初始容量为16个character * 2)public StringBuffer(int capacity) * 创建一个StringBuffer对象 * 该对象的初始值为null * 可以通过参数capacity:int来指定其初始容量,该对象的初始容量为capacity个character * 3)public StringBuffer(String str) * 创建一个StringBuffer对象 * 该对象的初始值为参数str:String * 该对象的初始容量为16个character+str.length() * 4) public StringBuffer(CharSequence seq) * 创建一个StringBuffer对象 * 该对象的初始值为参数seq:CharSequence * 该对象的初始容量为16个character+seq.length() * 成员函数 * 静态成员函数 * 实例成员函数 * 1) public synchronized int length() * 2)public synchronized int capacity() * 3)public synchronized void ensureCapacity(int minimumCapacity) * 4)public synchronized void trimToSize() * 5)public synchronized void setLength(int newLength) * 6)public synchronized char charAt(int index) * 7)public synchronized int codePointAt(int index) * 8)public synchronized int codePointBefore(int index) * 9)public synchronized int codePointCount(int beginIndex, int endIndex) * 10)public synchronized int offsetByCodePoints(int index, int codePointOffset) * 11)public synchronized void getChars(int srcBegin, int srcEnd, char[] dst, * int dstBegin) * 12)public synchronized void setCharAt(int index, char ch) * 13)public synchronized StringBuffer append(Object obj) * append:追加 * 14)public synchronized StringBuffer append(String str) * 15)public synchronized StringBuffer append(StringBuffer sb) * 如果参数sb:StringBuffer的值为空,那么将会在调用者的末尾添加“null”这四个字符 * 16)public StringBuffer append(CharSequence s) * 如果参数s:CharSequence的值为空,那么将会在调用者的末尾添加“null”这四个字符 * 17)public synchronized StringBuffer append(CharSequence s, int start, int end) * 18)public synchronized StringBuffer append(char[] str) * 19) public synchronized StringBuffer append(char[] str, int offset, int len) * 20)public synchronized StringBuffer append(boolean b) * 21)public synchronized StringBuffer append(char c) * 22)public synchronized StringBuffer append(int i) * public synchronized StringBuffer append(long lng) * public synchronized StringBuffer append(float f) * public synchronized StringBuffer append(double d) * 23)public synchronized StringBuffer appendCodePoint(int codePoint) * 24)public synchronized StringBuffer delete(int start, int end) * public synchronized StringBuffer deleteCharAt(int index) * 25)public synchronized StringBuffer replace(int start, int end, String str) * 26)public synchronized String substring(int start) * public synchronized CharSequence subSequence(int start, int end) * public synchronized String substring(int start, int end) * 27)public synchronized StringBuffer insert(int index, char[] str, int offset,int len) public synchronized StringBuffer insert(int offset, Object obj) public synchronized StringBuffer insert(int offset, String str) public synchronized StringBuffer insert(int offset, char[] str) public StringBuffer insert(int dstOffset, CharSequence s) public synchronized StringBuffer insert(int dstOffset, CharSequence s,int start, int end) public StringBuffer insert(int offset, boolean b) public synchronized StringBuffer insert(int offset, char c) public StringBuffer insert(int offset, int i) public StringBuffer insert(int offset, long l) public StringBuffer insert(int offset, float f) public StringBuffer insert(int offset, double d) 28)public int indexOf(String str) public synchronized int indexOf(String str, int fromIndex) public int lastIndexOf(String str) public synchronized int lastIndexOf(String str, int fromIndex) 29)public synchronized StringBuffer reverse() 30)public synchronized String toString() *
          */ package char_and_string; public class JavaAPI_StringBuffer { public static void main(String[] args){ StringBuffer st=new StringBuffer(); }//end main }//end class
    • 处理字符串的javaAPI——StringBuilder

        • /**
           * @author chen
           * @date 20170303
           * @description 本程序讲解java.lang.StringBuilder类的用法
           * java.lang.StringBuilder
           *     概述:
           *         1)StringBuilder类的特点
           *             a.它是线程不安全的,完全没有synchronized修饰符
           *             b.它的值是可变的字符序列,每个StringBuffer对象都有一个容量,在其值超过该容量的时候,StringBuffer对象会自动扩充其容量(创建一个新的internal buffer array)
           *                 StringBuilder对象用于存储 A mutable【可变的】 sequence of characters. 
           *             c.java API中还有一个和这个API有着类似功能的class——StringBuffer,不过StringBuilder是线程不安全的,而StringBuffer是线程安全的
           *     类成员变量
           *         
           *     构造函数
           *         和java.lang.StringBuffer类中的构造函数一致
           *  成员函数
           *      和java.lang.StringBuffer类中的成员函数一致,只不过本类中的成员函数都没有synchronized修饰符
           */
          package char_and_string;
          
          public class JavaAPI_StringBuilder {
              public static void main(String[] args){
                  StringBuilder str=new StringBuilder();
              }
          
          }
    • 编写程序展示String和StringBuilder/StringBuffer的区别  

        • 不停地修改String的值时,最好用StringBuilder/StringBuffer,因为它们比较快

          /**
           * @author chen
           * @date 20170305
           * @description 展示String和StringBuilder类的差别
           * 
           * 实例一、在需要不停地修改String对象的值的情况下,StringBuilder比String快多少*/
          package char_and_string;
          
          public class Application3_Compare_String_and_StringBuilder {
              public static void main(String[] args){
                  System.out.println("实例一、在需要不停地修改String对象的值的情况下,StringBuilder比String快多少");
                  String str="";
                  long startTime=System.currentTimeMillis();//获取系统当前时间
                  for(int i=0;i<1000;i++){
                      str=str+i;
                  }
                  long endTime=System.currentTimeMillis();
                  System.out.println("改变1000次String 对象的值总共用时:"+(endTime-startTime));
                  StringBuilder strBuilder=new StringBuilder();
                  startTime=System.currentTimeMillis();
                  for(int i=0;i<1000;i++){
                      strBuilder=strBuilder.append(i);
                  }
                  String strBuilder2String=strBuilder.toString();//使用StringBuilder中的toString()将StringBuilder对象转成String对象
                  endTime=System.currentTimeMillis();
                  System.out.println("改变1000次StringBuilder 对象的值总共用时:"+(endTime-startTime));
                  System.out.println("
          检查两种方法下得到的字符串是否相同:");
                  System.out.println("方法一,对String对象直接修改,
          结果str="+str);
                  System.out.println("方法二,先将初始字符串赋值给StringBuilder对象,然后使用StringBuilder中的方法进行修改,最后调用strBuider.toString()将其转换成String对象"+"
          结果:"+strBuilder.toString());
                  System.out.println("两种方法得到的字符串是否相同:"+str.equals(strBuilder2String));
              }
          
          }

          结果:

          实例一、在需要不停地修改String对象的值的情况下,StringBuilder比String快多少
          改变1000次String 对象的值总共用时:25
          改变1000次StringBuilder 对象的值总共用时:1
          
          检查两种方法下得到的字符串是否相同:
          方法一,对String对象直接修改,
          结果str=0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
          方法二,先将初始字符串赋值给StringBuilder对象,然后使用StringBuilder中的方法进行修改,最后调用strBuider.toString()将其转换成String对象
          结果:0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
          两种方法得到的字符串是否相同:true

                

       

    学习的过程中总会得到一些心得体会,认真地将它们记录下来并分享给每一个愿意花费时间去阅读它们的人,然后意外地收获某个读者的评论,从而激发出新的感想,是一件十分令人欢快的事。如果你也在研习这方面的知识,欢迎加入到我们的队伍中来,和我们一起进步吧(^_^)
  • 相关阅读:
    python中取列表的后半部分元素
    python中列表分片
    python中统计列表中元素出现的次数
    python中range()函数用法
    pyhton中实现列表元素顺序颠倒
    python中列表元素求交集和并集
    python中编写抽奖小游戏
    python中删除列表元素
    python中列表的去重复和取重复
    欲为Java技术大牛所需的25个学习要点
  • 原文地址:https://www.cnblogs.com/lxrm/p/6427690.html
Copyright © 2011-2022 走看看