java之String

一、构造器

package com.string;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

public class StringTest {

    public static void main(String[] args) {
        
        /**
         * String(byte[] bytes) 
         * 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
         */
        byte[] b1 = {'a','b','c','d'};
        System.out.println(new String(b1));//abcd
        
        /**
         * String(byte[] bytes, Charset charset)
         * 通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。 
         */
        System.out.println(new String(b1, Charset.forName("UTF-8")));//Charset代表的是编码方式
        
        /**
         * String(byte[] bytes, int offset, int length) 
         * 通过使用平台的默认字符集解码指定的 byte子数组,构造一个新的 String。
         */
        System.out.println(new String(b1, 1, 2));//bc 从第2个字符开始依次取两个字符拼接成字符串
        
        /**
         * String(byte[] bytes, int offset, int length, Charset charset)
         * 通过使用指定的 charset 解码指定的 byte 子数组,构造一个新的 String。
         */
        System.out.println(new String(b1, 1, 3, Charset.forName("UTF-8")));//bcd
        
        /**
         * String(byte[] bytes, int offset, int length, String charsetName)
         * 通过使用指定的字符集解码指定的 byte 子数组,构造一个新的 String。
         */
        try {
            System.out.println(new String(b1, 0, 2, "UTF-8"));//ab
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        
        /**
         * String(byte[] bytes, String charsetName)
         * 通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。
         */
        try {
            System.out.println(new String(b1, "UTF-8"));//abcd
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        
        /**
         * String(char[] value)
         * 分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。 
         */
        System.out.println(new String(b1));//abcd
        
        /**
         * String(char[] value, int offset, int count)
         * 分配一个新的 String,它包含取自字符数组参数一个子数组的字符。
         */
        System.out.println(new String(b1, 2, 2));//cd
        
        /**
         * String(int[] codePoints, int offset, int count)
         * 分配一个新的 String,它包含 Unicode 代码点数组参数一个子数组的字符。
         */
        int[] b2 = {65,66,67,68,97,97};
        System.out.println(new String(b2, 1, 3));//BCD 将Unicode码转为对应字符
        
        /**
         * String(String original)
         * 初始化一个新创建的 String 对象,使其表示一个与参数相同的字符序列;换句话说,新创建的字符串是该参数字符串的副本。
         */
        System.out.println(new String("abcd"));//abcd 与new String()在内存分配上存在差异
        
        /**
         * String(StringBuffer buffer)
         * 分配一个新的字符串,它包含字符串缓冲区参数中当前包含的字符序列
         */
        StringBuffer b3 = new StringBuffer("abcd");
        System.out.println(new String(b3));//abcd
        
        /**
         * String(StringBuilder builder) 
         * 分配一个新的字符串,它包含字符串生成器参数中当前包含的字符序列。
         */
        StringBuilder b4 = new StringBuilder("abcd");
        System.out.println(new String(b4));//abcd
    }

}

二、方法

package com.string;

import java.nio.charset.Charset;
import java.util.Locale;

public class StringMethodTest {

    public static void main(String[] args) {
        String s = "ebcd";
        
        /**
         * charAt(int index)
         * 返回指定索引处的 char 值。
         */
        System.out.println(s.charAt(0));//e
        
        /**
         * codePointAt(int index)
         * 返回指定索引处的字符(Unicode 代码点)。
         */
        System.out.println(s.codePointAt(1));//98
        
        /**
         * codePointBefore(int index) 
         * 返回指定索引之前的字符(Unicode 代码点)。
         */
        System.out.println(s.codePointBefore(1));//101
        
        /**
         * codePointCount(int beginIndex, int endIndex)
         * 返回此 String 的指定文本范围中的 Unicode 代码点数。
         * 
         * 码点,我译为“码位值”。每个码位值实际上代表一个真正unicode字符。即unicode字符集上的码位值。
         *为什么要这些码位相关的方法?源自1个java的char字符并不完全等于一个unicode的字符。
         *char采用UCS-2编码是一种淘汰的UTF-16编码,最多65536种形态,也远少于当今unicode拥有11万字符的需求。
         *java只好对后来新增的unicode字符用2个char拼出1个unicode字符。导致String中char的数量不等于unicode字符的数量
         */
        System.out.println(s.codePointCount(0, 2));//2
        
        /**
         * compareTo(String anotherString)
         * 按字典顺序比较两个字符串。
         */
        String s1 = "abcd";
        System.out.println(s.compareTo(s1));//4
        
        /**
         * compareToIgnoreCase(String str)
         * 按字典顺序比较两个字符串,不考虑大小写。
         */
        System.out.println(s.compareToIgnoreCase(s1));//4
        
        /**
         * concat(String str) 
         * 将指定字符串连接到此字符串的结尾。
         */
        System.out.println(s.concat(s1));//ebcdabcd
        
        /**
         * contains(CharSequence s) 
         *  当且仅当此字符串包含指定的 char 值序列时,返回 true。
         */
        System.out.println(s.contains("b"));//true
        
        /**
         * contentEquals(CharSequence cs)
         * 将此字符串与指定的 CharSequence 比较。
         * 
         * CharSequence类型
         *这是一个接口,代表的是一个有序字符集合,这个接口包含的方法有:charAt(int index),
         *toString(),length(),subSequence(int start,int end).
         */
        System.out.println(s.contentEquals("ebcd"));
        
        /**
         * contentEquals(StringBuffer sb) 
         *将此字符串与指定的 StringBuffer 比较。
         */
        System.out.println(s.contentEquals(new StringBuffer("ebcd")));//true
        
        /**
         * copyValueOf(char[] data) 
         *返回指定数组中表示该字符序列的 String。
         */
        char[] c = {'a','b','c','d'};
        System.out.println(String.copyValueOf(c));//abcd
        
        /**
         * copyValueOf(char[] data, int offset, int count) 
         *返回指定数组中表示该字符序列的 String。
         */
        System.out.println(String.copyValueOf(c, 1, 2));//bc
        
        /**
         * endsWith(String suffix) 
         *测试此字符串是否以指定的后缀结束。
         */
        System.out.println(s.endsWith("d"));//true
        
        /**
         * equals(Object anObject) 
         *将此字符串与指定的对象比较。
         */
        System.out.println(s.equals(s1));//false
        
        /**
         * equalsIgnoreCase(String anotherString) 
         *将此 String 与另一个 String 比较,不考虑大小写。
         */
        System.out.println(s.equalsIgnoreCase("Ebcd"));//true
        
        /**
         * format(Locale l, String format, Object... args) 
         *使用指定的语言环境、格式字符串和参数返回一个格式化字符串。
         */
        System.out.format(Locale.CHINA, "%10.2f", Math.PI);//3.14
        
        /**
         * format(String format, Object... args) 
         * 使用指定的格式字符串和参数返回一个格式化字符串。
         */
        System.out.format("%10.2f", Math.PI);//3.14
        
        /**
         * getBytes() 
         *使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
         */
        byte[] by = s.getBytes();
        
        /**
         * getBytes() 
         * 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
         */
        byte[] by1 = "abcd".getBytes(Charset.forName("UTF-8"));//97 98 99 100
        for(int i = 0; i < by1.length; i++){
            System.out.println(by1[i]);
        }
        
        /**
         * indexOf(int ch) 
         *返回指定字符在此字符串中第一次出现处的索引。
         */
        System.out.println(s.indexOf(98));//1 不存在返回-1
        
        /**
         * indexOf(int ch, int fromIndex) 
         *返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
         */
        System.out.println(s.indexOf(98, 0));//1 不存在返回-1
        
        /**
         * indexOf(String str) 
         * 返回指定子字符串在此字符串中第一次出现处的索引。
         */
        System.out.println(s.indexOf("bc"));//1 不存在返回-1
        
        /**
         * intern() 
         * 返回字符串对象的规范化表示形式。
         */
        System.out.println(s.intern());//ebcd
        
        System.out.println(s.isEmpty());//false 空字符串返回true
        
        /**
         * lastIndexOf(int ch) 
         *返回指定字符在此字符串中最后一次出现处的索引。
         */
        System.out.println(s.lastIndexOf(98));//1
        
        /**
         * lastIndexOf(String str) 
         * 返回指定子字符串在此字符串中最右边出现处的索引。
         */
        System.out.println(s.lastIndexOf("cd"));//2
        
        System.out.println(s.length());
        
        /**
         * trim() 
         *返回字符串的副本,忽略前导空白和尾部空白。
         */
        System.out.println("   ab cd   ".trim());//ab cd
        
        /**
         * toCharArray() 
         * 将此字符串转换为一个新的字符数组。
         */
        System.out.println(s.toCharArray());//ebcd
        
        /**
         * startsWith(String prefix) 
         *测试此字符串是否以指定的前缀开始。
         */
        System.out.println(s.startsWith("e"));//true
        
        /**
         * startsWith(String prefix, int toffset) 
         *测试此字符串从指定索引开始的子字符串是否以指定前缀开始。
         */
        
        /**
         * matches(String regex) 
         *告知此字符串是否匹配给定的正则表达式。
         */
        
        /**
         * replace(char oldChar, char newChar) 
         *返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的
         */
        System.out.println(s.replace('b', 'f'));//efcd
        
        /**
         * replaceAll(String regex, String replacement) 
         * 使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
         */
        
        
    }

}
原文地址:https://www.cnblogs.com/sunjf/p/java_string.html