標籤:ann ignore encode sub name following sequence data char
字元與字串:1.將字元數組變為字串(構造方法)
public String(char[] value)Allocates a new String so that it represents the sequence of characters currently contained in the character array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string.Parameters:value - The initial value of the string
2.將部分字元數組變為字串(構造方法)
public String(char[] value, int offset, int count)Allocates a new String that contains characters from a subarray of the character array argument. The offset argument is the index of the first character of the subarray and the count argument specifies the length of the subarray. The contents of the subarray are copied; subsequent modification of the character array does not affect the newly created string.Parameters:value - Array that is the source of charactersoffset - The initial offsetcount - The lengthThrows:IndexOutOfBoundsException - If the offset and count arguments index characters outside the bounds of the value array
3.返回指定索引對應的字元(普通方法)
charAt(int index)Returns the char value at the specified index.
public class test1 { public static void main(String args[]) { String str = "hello"; char c = str.charAt(1); System.out.println(c); }}//e
4.將字串變為字元數組(普通方法)
toCharArray()Converts this string to a new character array.
public class test1 { public static void main(String args[]) { String str = "hello"; char [] data = str.toCharArray(); for(int i=0;i<data.length;i++) { data[i]-=32;//轉大寫 System.out.print(data[i]+","); } System.out.println(new String(data));//將字元數組轉化為字串 }}位元組與字串
1。將位元組數群組轉換為字串(構造方法)
String(byte[] bytes)Constructs a new String by decoding the specified array of bytes using the platform‘s default charset.
2.將位元組數組的一部分轉換為字串(構造方法)
String(byte[] bytes, int offset, int length)Constructs a new String by decoding the specified subarray of bytes using the platform‘s default charset.
3.將字串變為位元組資料(普通)
public byte[] getBytes()Encodes this String into a sequence of bytes using the platform‘s default charset, storing the result into a new byte array.The behavior of this method when this string cannot be encoded in the default charset is unspecified. The CharsetEncoder class should be used when more control over the encoding process is required.Returns:The resultant byte arraySince:JDK1.1
4.用於編碼轉換(普通)
public byte[] getBytes(String charsetName) throws UnsupportedEncodingExceptionEncodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.The behavior of this method when this string cannot be encoded in the given charset is unspecified. The CharsetEncoder class should be used when more control over the encoding process is required.Parameters:charsetName - The name of a supported charsetReturns:The resultant byte arrayThrows:UnsupportedEncodingException - If the named charset is not supportedSince:JDK1.1
字串比較判斷
1.equals(普通),進行相等判斷,區分大小寫
public boolean equals(Object anObject)Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.Overrides:equals in class ObjectParameters:anObject - The object to compare this String againstReturns:true if the given object represents a String equivalent to this string, false otherwiseSee Also:compareTo(String), equalsIgnoreCase(String)
2.進行相等判斷,不區分大小寫
public boolean equalsIgnoreCase(String anotherString)Compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length and corresponding characters in the two strings are equal ignoring case.Two characters c1 and c2 are considered the same ignoring case if at least one of the following is true:The two characters are the same (as compared by the == operator)Applying the method Character.toUpperCase(char) to each character produces the same resultApplying the method Character.toLowerCase(char) to each character produces the same resultParameters:anotherString - The String to compare this String againstReturns:true if the argument is not null and it represents an equivalent String ignoring case; false otherwiseSee Also:equals(Object)
3.判斷兩個字元大小,(按照字元編碼比較)
public int compareTo(String anotherString)Specified by:compareTo in interface Comparable<String>Parameters:anotherString - the String to be compared.Returns:the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.compareToIgnoreCase
字串尋找
1.
JAVA基礎學習之路(九)[2]String類常用方法