Java基礎--String(按照指定編碼截取文字)

/**
 * 字符串按照字節來截取。例如:abc中國
 * 存在的問題:   
 *  1.字符是一個字節,容易截取.但是一個文字是兩個字節,應該按照什麼方式截取呢? 
 *  2.可以按照文件的編碼方式來截取,GBK的編碼方式是中文全部是負數(奇數位也有可能是正數).
 *  3.根據編碼表來判斷即可。
 */
public class StringByteSplitDemo {
    public static void main(String[] args) throws IOException {
        String str = "abc中國國";
        for(int i = 0;i<str.length()+1;i++){
            String s = splitMethod(str,i+1);
            System.out.println("按照長度爲"+(i+1)+"截取的字符是:"+s);
        }
    }

    private static String splitMethod(String str, int leng) throws IOException {
        //將字符串用指定的編碼表,來進行編碼
        byte[] be = str.getBytes("GBK");
        //記錄負數的個數
        int count = 0;
        //根據字節編碼來判斷是否是完整的字符,按照指定的位置來截取
        for(int i=leng-1;i>=0;i--){
            if(be[i]<0){
                count++;
            }else{//都是大於0
                break;
            }
        }
        if(count%2==0){
            //按照指定編碼重新解碼
            return new String(be,0,leng,"GBK");
        }else{
            return new String(be,0,leng-1,"GBK");
        }
    }
}
運行結果: 
按照長度爲1截取的字符是:a 
按照長度爲2截取的字符是:ab 
按照長度爲3截取的字符是:abc 
按照長度爲4截取的字符是:abc 
按照長度爲5截取的字符是:abc中 
按照長度爲6截取的字符是:abc中 
按照長度爲7截取的字符是:abc中國

API參考:

類 String

java.lang.Object
  |--java.lang.String
構造方法摘要
String() 
          初始化一個新創建的 String 對象,使其表示一個空字符序列。
String(byte[] bytes) 
          通過使用平臺的默認字符集解碼指定的 byte 數組,構造一個新的 String
String(byte[] bytes, Charset charset) 
          通過使用指定的 charset 解碼指定的 byte 數組,構造一個新的 String
String(byte[] ascii, int hibyte) 
          已過時。 該方法無法將字節正確地轉換爲字符。從 JDK 1.1 開始,完成該轉換的首選方法是使用帶有 Charset、字符集名稱,或使用平臺默認字符集的 String 構造方法。
String(byte[] bytes, int offset, int length) 
          通過使用平臺的默認字符集解碼指定的 byte 子數組,構造一個新的 String
String(byte[] bytes, int offset, int length, Charset charset) 
          通過使用指定的 charset 解碼指定的 byte 子數組,構造一個新的 String
String(byte[] ascii, int hibyte, int offset, int count) 
          已過時。 該方法無法將字節正確地轉換爲字符。從 JDK 1.1 開始,完成該轉換的首選方法是使用帶有 Charset、字符集名稱,或使用平臺默認字符集的 String 構造方法。
String(byte[] bytes, int offset, int length, String charsetName) 
          通過使用指定的字符集解碼指定的 byte 子數組,構造一個新的 String
String(byte[] bytes, String charsetName) 
          通過使用指定的 charset 解碼指定的 byte 數組,構造一個新的 String
String(char[] value) 
          分配一個新的 String,使其表示字符數組參數中當前包含的字符序列。
String(char[] value, int offset, int count) 
          分配一個新的 String,它包含取自字符數組參數一個子數組的字符。
String(int[] codePoints, int offset, int count) 
          分配一個新的 String,它包含 Unicode 代碼點數組參數一個子數組的字符。
String(String original) 
          初始化一個新創建的 String 對象,使其表示一個與參數相同的字符序列;換句話說,新創建的字符串是該參數字符串的副本。
String(StringBuffer buffer) 
          分配一個新的字符串,它包含字符串緩衝區參數中當前包含的字符序列。
String(StringBuilder builder) 
          分配一個新的字符串,它包含字符串生成器參數中當前包含的字符序列。
方法摘要
 charcharAt(int index) 
          返回指定索引處的 char 值。
 intcodePointAt(int index) 
          返回指定索引處的字符(Unicode 代碼點)。
 intcodePointBefore(int index) 
          返回指定索引之前的字符(Unicode 代碼點)。
 intcodePointCount(int beginIndex, int endIndex) 
          返回此 String 的指定文本範圍中的 Unicode 代碼點數。
 intcompareTo(String anotherString) 
          按字典順序比較兩個字符串。
 intcompareToIgnoreCase(String str) 
          按字典順序比較兩個字符串,不考慮大小寫。
 Stringconcat(String str) 
          將指定字符串連接到此字符串的結尾。
 booleancontains(CharSequence s) 
          當且僅當此字符串包含指定的 char 值序列時,返回 true。
 booleancontentEquals(CharSequence cs) 
          將此字符串與指定的 CharSequence 比較。
 booleancontentEquals(StringBuffer sb) 
          將此字符串與指定的 StringBuffer 比較。
static StringcopyValueOf(char[] data) 
          返回指定數組中表示該字符序列的 String。
static StringcopyValueOf(char[] data, int offset, int count) 
          返回指定數組中表示該字符序列的 String。
 booleanendsWith(String suffix) 
          測試此字符串是否以指定的後綴結束。
 booleanequals(Object anObject) 
          將此字符串與指定的對象比較。
 booleanequalsIgnoreCase(String anotherString) 
          將此 String 與另一個 String 比較,不考慮大小寫。
static Stringformat(Locale l, String format, Object... args) 
          使用指定的語言環境、格式字符串和參數返回一個格式化字符串。
static Stringformat(String format, Object... args) 
          使用指定的格式字符串和參數返回一個格式化字符串。
 byte[]getBytes() 
          使用平臺的默認字符集將此 String 編碼爲 byte 序列,並將結果存儲到一個新的 byte 數組中。
 byte[]getBytes(Charset charset) 
          使用給定的 charset 將此 String 編碼到 byte 序列,並將結果存儲到新的 byte 數組。
 voidgetBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin) 
          已過時。 該方法無法將字符正確轉換爲字節。從 JDK 1.1 起,完成該轉換的首選方法是通過 getBytes() 方法,該方法使用平臺的默認字符集。
 byte[]getBytes(String charsetName) 
          使用指定的字符集將此 String 編碼爲 byte 序列,並將結果存儲到一個新的 byte 數組中。
 voidgetChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 
          將字符從此字符串複製到目標字符數組。
 inthashCode() 
          返回此字符串的哈希碼。
 intindexOf(int ch) 
          返回指定字符在此字符串中第一次出現處的索引。
 intindexOf(int ch, int fromIndex) 
          返回在此字符串中第一次出現指定字符處的索引,從指定的索引開始搜索。
 intindexOf(String str) 
          返回指定子字符串在此字符串中第一次出現處的索引。
 intindexOf(String str, int fromIndex) 
          返回指定子字符串在此字符串中第一次出現處的索引,從指定的索引開始。
 Stringintern() 
          返回字符串對象的規範化表示形式。
 booleanisEmpty() 
          當且僅當 length() 爲 0 時返回 true
 intlastIndexOf(int ch) 
          返回指定字符在此字符串中最後一次出現處的索引。
 intlastIndexOf(int ch, int fromIndex) 
          返回指定字符在此字符串中最後一次出現處的索引,從指定的索引處開始進行反向搜索。
 intlastIndexOf(String str) 
          返回指定子字符串在此字符串中最右邊出現處的索引。
 intlastIndexOf(String str, int fromIndex) 
          返回指定子字符串在此字符串中最後一次出現處的索引,從指定的索引開始反向搜索。
 intlength() 
          返回此字符串的長度。
 booleanmatches(String regex) 
          告知此字符串是否匹配給定的正則表達式
 intoffsetByCodePoints(int index, int codePointOffset) 
          返回此 String 中從給定的 index 處偏移 codePointOffset 個代碼點的索引。
 booleanregionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) 
          測試兩個字符串區域是否相等。
 booleanregionMatches(int toffset, String other, int ooffset, int len) 
          測試兩個字符串區域是否相等。
 Stringreplace(char oldChar, char newChar) 
          返回一個新的字符串,它是通過用 newChar 替換此字符串中出現的所有 oldChar 得到的。
 Stringreplace(CharSequence target, CharSequence replacement) 
          使用指定的字面值替換序列替換此字符串所有匹配字面值目標序列的子字符串。
 StringreplaceAll(String regex, String replacement) 
          使用給定的 replacement 替換此字符串所有匹配給定的正則表達式的子字符串。
 StringreplaceFirst(String regex, String replacement) 
          使用給定的 replacement 替換此字符串匹配給定的正則表達式的第一個子字符串。
 String[]split(String regex) 
          根據給定正則表達式的匹配拆分此字符串。
 String[]split(String regex, int limit) 
          根據匹配給定的正則表達式來拆分此字符串。
 booleanstartsWith(String prefix) 
          測試此字符串是否以指定的前綴開始。
 booleanstartsWith(String prefix, int toffset) 
          測試此字符串從指定索引開始的子字符串是否以指定前綴開始。
 CharSequencesubSequence(int beginIndex, int endIndex) 
          返回一個新的字符序列,它是此序列的一個子序列。
 Stringsubstring(int beginIndex) 
          返回一個新的字符串,它是此字符串的一個子字符串。
 Stringsubstring(int beginIndex, int endIndex) 
          返回一個新字符串,它是此字符串的一個子字符串。
 char[]toCharArray() 
          將此字符串轉換爲一個新的字符數組。
 StringtoLowerCase() 
          使用默認語言環境的規則將此 String 中的所有字符都轉換爲小寫。
 StringtoLowerCase(Locale locale) 
          使用給定 Locale 的規則將此 String 中的所有字符都轉換爲小寫。
 StringtoString() 
          返回此對象本身(它已經是一個字符串!)。
 StringtoUpperCase() 
          使用默認語言環境的規則將此 String 中的所有字符都轉換爲大寫。
 StringtoUpperCase(Locale locale) 
          使用給定 Locale 的規則將此 String 中的所有字符都轉換爲大寫。
 Stringtrim() 
          返回字符串的副本,忽略前導空白和尾部空白。
static StringvalueOf(boolean b) 
          返回 boolean 參數的字符串表示形式。
static StringvalueOf(char c) 
          返回 char 參數的字符串表示形式。
static StringvalueOf(char[] data) 
          返回 char 數組參數的字符串表示形式。
static StringvalueOf(char[] data, int offset, int count) 
          返回 char 數組參數的特定子數組的字符串表示形式。
static StringvalueOf(double d) 
          返回 double 參數的字符串表示形式。
static StringvalueOf(float f) 
          返回 float 參數的字符串表示形式。
static StringvalueOf(int i) 
          返回 int 參數的字符串表示形式。
static StringvalueOf(long l) 
          返回 long 參數的字符串表示形式。
static StringvalueOf(Object obj) 
          返回 Object 參數的字符串表示形式。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章