您的位置:首页 > 编程语言 > Java开发

Java学习笔记:String

2015-04-30 16:06 120 查看
1.初始化

// public String()
        String string =
new String();

     

        // public String(byte[] bytes);

        // params:  bytes :
被用于创建Stirng的byte数组,存放的是字符的ascii码
       
byte[] bytes = {1,
'a', 'b',
'c', 'd'};
        String string1 =
new String(bytes);
        System.out.println(string1);

     

        // public String(char[] value);

        // params:  value :
被用于创建String 的char数组

        //
创建以后改变字符数组并不会改变String
       
char[] chars = {'我',
'爱',
'你',
'中',
'国'};
        String string2 =
new String(chars);
        System.out.println(string2);

     

        // public String(String original);

        // params:  original :
被用于创建String的原始String
        String string3 =
new String(string2);
        System.out.println(string3);

     

        // public String(char[] value, int offset, int count);

        // params:  value :
字符数组

        //          offset :
偏移量

        //          count :
个数;

        //
如果offset 和 count
超出了字符数组的长度就会抛出一个IndexOutOfBoundsException

        //
创建成功以后修改字符数组并不会改变String
       
char[] chars = {'a',
'b', 'c',
'd', 'e', 'f',
'g', 'h',
'i'};
        String string =
new String(chars,
5, 13);
        System.out.println(string);

     

        // public String(int[] codePoints, int offset, int count);

        // params:  codePoints :
数组中存放的字符的ascii码

        //          offset :
偏移量

        //          count :
个数

        //
如果offset 和 count
超出了字符数组的长度就会抛出一个IndexOutOfBoundsException
       
int[] array = {97,
98, 99,
100, 101, 102,
103, 655394,
9};
        String string =
new String(array,
2, 5);
        System.out.println(string);

        

        // public String(byte[] bytes, int offset, int length);

        // public String(byte[] bytes, int offset, int length, String charsetName);

        // params:  bytes : bytes数组,存放的是字符的ascii码

        //          offset :
偏移量

        //          length :
字符串长度

        //          charsetName :
字符集名称

        //
如果offset 和 count
超出了字符数组的长度就会抛出一个IndexOutOfBoundsException
       
throws UnsupportedEncodingException
       
byte[] bytes = {97,
98, 99,
100, 101, 102,
103, 104,
105, 106, 107,
108};
       
try {
            String string =
new String(bytes,
2, 5, "UTF-8");
            System.out.println(string);
        }
catch (Exception e) {
            e.printStackTrace();
        }
finally {

            
        }

        

        //  public String(byte[] bytes, int offset, int length, Charset charset);

        //  params: bytes :
被用于创建Stirng的byte数组,存放的是字符的ascii码

        //          offset :
偏移量

        //          length :
长度

        //          charset :
字符集

        //          charset可以通过Charset获取,要包含头文件:import java.nio.charset.Charset;
       
byte[] bytes = {97,
98, 99,
100, 101, 102,
103, 104,
105, 106, 107,
108};
        String string =
new String(bytes,
2, 5, Charset.defaultCharset());
        System.out.println(string);

   

        // public String(byte[] bytes, Charset charset);

        // public String(byte[] bytes, String charsetName) throws UnsupportedEncodingException;

        // params:  bytes :
被用于创建Stirng的byte数组,存放的是字符的ascii码

        //          charsetName : charsetName :
字符集名称
       
byte[] bytes = {97,
98, 99,
100, 101, 102,
103, 104,
105, 106, 107,
108};
       
try {
            String string =
new String(bytes,
"UTF-8");
            System.out.println(string);
        }
catch (Exception e) {
            e.printStackTrace();
        }
finally {

            
        }

        

        // public String(StringBuilder builder);

        // public String(StringBuffer buffer);

        // params:  buffer :
一个StringBuffer类对象
        StringBuffer stringBuffer =
new StringBuffer("abc");
        String string =
new String(stringBuffer);
        System.out.println(string);

2.常用方法

/**

         *  Method Detail

         */
        String string =
"1234567890哈abc哈a";

        

        

        //  public int length();

        // 
获取字符串长度
       
int l = string.length();
        System.out.println("string length = " + l);

        //  string length = 10

        

        //  public boolean isEmpty();

        // 
判断字符串是否为空
       
boolean b = string.isEmpty();
        System.out.println("string is empty : " +  b);
        System.out.println("is empty : " +
"".isEmpty());

        //  string is empty : false

        //  is empty : true

        

        //  public char charAt(int index);

        // 
获取字符串index位的字符

        // 
是从第0位开始

        // 
如果index超出了字符串长度就抛出IndexOutOfBoundsException
       
char c = string.charAt(5);
        System.out.println("string charAt(5) : " + c);

        //  string charAt(5) : 6

        

        //  public int codePointAt(int index);

        // 
获取字符串index位的字符的ascii码

        // 
是从第0位开始

        // 
如果index超出了字符串长度就抛出IndexOutOfBoundsException
       
int code = string.codePointAt(13);
        System.out.println("string code : " + code);

        //  string code : 21704

        

        

        //  public int codePointBefore(int index);

        // 
获取字符串index - 1位的字符的ascii码
        code = string.codePointBefore(15);
        System.out.println("string code :" + code);

        //  string code :21704

        

        

        //  public int codePointCount(int beginIndex, int endIndex);

        // 
在此字符串指定的文本范围,方法返回数字的Unicode代码点。文本范围始于指定的beginIndex,并延伸到索引endIndex - 1的char。因此,文本范围的长度(以字符为单位)是endIndex-beginIndex.

        //  params: beginIndex :
这是该指数的第一个字符的文本范围

        //          endIndex :
这是该指数后的最后一个字符的文本范围

        //  IndexOutOfBoundsException --
如果beginIndex是负或endIndex大于这个字符串的长度或beginIndex是大于endIndex
        code = string.codePointCount(0, string.length());
        System.out.println("string code :" + code);

        //  string code :15

        

        

        //  public int offsetByCodePoints(int index, int codePointOffset);

        //  offsetByCodePoints方法返回此String中从给定的index处偏移codePointOffset个代码点的索引。文本范围内由index和codePointOffset给定的未配对代理项各计为一个代码点。

        //  params: index :
是要偏移的索引

        //          codePointOffset :
为代码点中的偏移量
        code = string.offsetByCodePoints(1,
5);
        System.out.println("offsetByCodePoints code :" + code);

        //  offsetByCodePoints code :6

        

        //  public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);

        // 
从字符串中截取从srcBegin到srcEnd的所有字符存入到dst字符数组中,并从字符数组的dstBegin开始存

        //  params: srcBegin :
要复制的第一个字符

        //          srcEnd :
要复制的最后一个字符

        //          dst :
要存储复制的所有字符的字符数组

        //          dstBegin :
字符数组存储字符时的偏移量
       
char[] dst = new
char[20];
        string.getChars(2,
10, dst, 10);
       
for (int i =
0; i < dst.length; i++) {
            System.out.print(dst[i] +
" ");
        }
        System.out.println();

        //            3 4 5 6 7 8 9 0

        

        

        //  public byte[] getBytes();

        //  public byte[] getBytes(Charset charset);

        //  public byte[] getBytes(String charsetName) throws UnsupportedEncodingException;

        // 
编码字符串转换成使用指定的字符集的字节序列,并将结果存储到一个新的字节数组
       
try {
           
byte[] bytes = string.getBytes("UTF-8");
           
for (int i =
0; i < bytes.length; i++) {
                System.out.print(bytes[i] +
" ");
            }
        }
catch (Exception e) {
            e.printStackTrace();
        }
finally {
            System.out.println();
        }

        //  49 50 51 52 53 54 55 56 57 48 -27 -109 -120 97 98 99 -27 -109 -120 97

        

        

        //  public boolean equals(Object anObject);

        // 
用于比较字符串与指定的对象,当anObject不为null并且是一个String对象,以及这个对象表示的字符序列与string一致时,返回true,否则返回false
       
boolean eB = string.equals("1234567890哈abc哈");
        System.out.println("equals eb : " + eB);
        System.out.println("equals2 : " + string.equals("1234567890哈abc哈a"));

        //  equals eb : false

        //  equals2 : true

        

        

        

        //  public boolean contentEquals(StringBuffer sb);

        // 
用于比较字符串与指定的StringBuffer对象,当这个对象表示的字符序列与string一致时,返回true,否则返回false
        StringBuffer stringBuffer =
new StringBuffer(string);
        eB = string.contentEquals(stringBuffer);
        System.out.println("contentEquals eB : " + eB);
        stringBuffer.insert(2,
"1");
        eB = string.contentEquals(stringBuffer);
        System.out.println("stringbuffer = " + stringBuffer +
"\n" + "eB : " + eB);

        //  contentEquals eB : true

        //  stringbuffer = 12134567890哈abc哈a

        //  eB : false
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java String