KonjacのJavaエントリージャーニー-String、StringBuffer、StringBuilder

1.サンプルAPI

1.弦:

(1).codePointAt(int index)メソッド:
このメソッドを使用して、文字列内の対応するシリアル番号の文字のASCIIコード値を出力できます。

public class Test{
    
    
	public static void main(String[] args){
    
    		
		String a=new String("123");
		System.out.println(a.codePointAt(1));//输出对应字符的Ascii码值
	}

}

(2).codePointCount(int begin、int end):
このメソッドはlength()に似ており、文字列の長さを計算するために使用されます。それらの違いは、length()は文字列内のコード単位を検出するために使用されますが、codePointCount()はコードポイントを検出するために使用されることです。一般的に、この2つは共通して使用できます。
コードポイントとコードユニット(複製)については、ここをクリックしてください

文字列のlength()メソッド:

public int length() {
    
    
        return value.length >> coder();
    }

文字列のcodePointCount()メソッド:

public int codePointCount(int beginIndex, int endIndex) {
    
    
        if (beginIndex < 0 || beginIndex > endIndex ||
            endIndex > length()) {
    
    
            throw new IndexOutOfBoundsException();
        }
        if (isLatin1()) {
    
    
            return endIndex - beginIndex;
        }
        return StringUTF16.codePointCount(value, beginIndex, endIndex);
    }

使用例:

public class Test{
    
    
	public static void main(String[] args){
    
    		
		String a=new String("123");
		System.out.println(a.codePointCount(0,3));
	}

}

(3).contentEquals(StringBuffer sb)メソッド:
このメソッドは、Stringの文字列値がStringBufferの文字列値と同じであるかどうかを比較するために使用されます。
メソッドコード:

    public boolean contentEquals(StringBuffer sb) {
    
    
        return contentEquals((CharSequence)sb);
    }

実装例:

public class Test{
    
    
	public static void main(String[] args){
    
    		
		String a=new String("123");
		String b=new String("234");
		StringBuffer c = new StringBuffer( "234");
		System.out.println(a.contentEquals(c));
		System.out.println(b.contentEquals(c));
	}

}

結果:
ここに写真の説明を挿入

2.StringBuffer
(1)。deleteCharAt(int index):
このメソッドは、文字列値の指定された順序位置(位置0から開始)の文字を削除するために使用されます。
メソッドコード:

public synchronized StringBuffer deleteCharAt(int index) {
    
    
        toStringCache = null;
        super.deleteCharAt(index);
        return this;
    }

使用例:

public class Test{
    
    
	public static void main(String[] args){
    
    		
	
		StringBuffer c = new StringBuffer( "234");
		System.out.println(c);
		c.deleteCharAt(2);
		System.out.println(c);
	}

}

出力結果:
ここに写真の説明を挿入
(2).replace(int start、int end、String str):
このメソッドは、指定された間隔のStringBuffer値をStringクラスの値に置き換えるために使用されます。ここで、StringIndexOutOfBoundsExceptionを発生させることができます。

メソッドコード:

public synchronized StringBuffer replace(int start, int end, String str) {
    
    
        toStringCache = null;
        super.replace(start, end, str);
        return this;
    }

使用例:

public class Test{
    
    
	public static void main(String[] args){
    
    		
	
		StringBuffer c = new StringBuffer( "783456");
		String a=new String("12");
		System.out.println(c);
		c.replace(0,2,a);
		System.out.println(c);
	}

}

出力結果は次のとおりです。
ここに写真の説明を挿入
(3).indexOf(String str)メソッド:
このメソッドの機能は文字列を比較することであり、StringBufferオブジェクト内のstrオブジェクトの値が最初に出現する位置を返します。
メソッドコード:

  public int indexOf(String str) {
    
    
        // Note, synchronization achieved via invocations of other StringBuffer methods
        return super.indexOf(str);
    }

コード例:

public class Test{
    
    
	public static void main(String[] args){
    
    		
	
		StringBuffer c = new StringBuffer( "783456");
		String a=new String("12");
		//System.out.println(c);
		//c.replace(0,2,a);
		System.out.println(c.indexOf(a));
	}

}

出力結果

-1//由于a在c中无法匹配

3. StringBuilder
(1).toString()メソッド:
このメソッドは、StringBuildクラスをStringクラスに変換するために使用されます。
メソッドコード:

  public String toString() {
    
    
        // Create a copy, don't share the array
        return isLatin1() ? StringLatin1.newString(value, 0, count)
                          : StringUTF16.newString(value, 0, count);
    }

使用例:

public class Test{
    
    
	public static void main(String[] args){
    
    		
	
		StringBuffer c = new StringBuffer( "783456");
		String b=c.toString();
		System.out.println(b);
	}

}

(2).appendCodePoint(int codePoint)メソッド:
このメソッドは、StringBuildオブジェクトのコードポイントを増やすために使用されます。
メソッドコード:

public StringBuilder appendCodePoint(int codePoint) {
    
    
        super.appendCodePoint(codePoint);
        return this;
    }

4. 3つの
変更の類似点と相違点の要約:Stringクラスにカプセル化されたデータタイプは定数文字配列であるため、Stringクラスオブジェクトの値は、作成後に変更できません(変更後に新しいオブジェクトが生成されます)。StringBufferとStringBuildは通常の文字配列をカプセル化するため、オブジェクトの値を自由に変更できます。StringBufferとStringBuildの場合、前者はスレッドセーフであり、同時プログラムで同時にアクセスできますが、後者はそうではありません。
同時に、パラメータを渡す場合、渡されたパラメータによって呼び出されたメソッドで変更された後は、文字列参照のみを変更できません。その理由は、変更された正式なパラメータが、メッセージを通じて元のオブジェクトを変更するのではなく、新しいオブジェクトを指すためです。
5.文字のエンコードとデコード
概要ローカル文字セットをマシンで認識可能なフォーマットコードに変換するプロセスはエンコードと呼ばれ、その逆はデコードです。
コーディング規則の概要:
ASCIIコード:英語の文字(大文字と小文字は区別されません)は1バイトのスペースを占め、中国語の文字は2バイトのスペースを占めます。

UTF-8エンコーディング:1つの英語文字は1バイトに等しく、1つの中国語(従来の文字を含む)は3バイトに等しい。

Unicodeエンコーディング:1つの英語は2バイトに等しく、1つの中国語(従来のを含む)は2バイトに等しい。

デコードとエンコードの例:

public class Test{
    
    
	public static void main(String[] args) throws UnsupportedEncodingException{
    
    		
		String s=new String("中文");
		System.out.println(s);
		byte[] s1=s.getBytes();//转换成字节码
		System.out.println(s1);
		s=new String(s1,"GB2312");//重新编码
		System.out.println(s);
	}

}

詳細はこちら(この部分を再現)

おすすめ

転載: blog.csdn.net/HiphopHarris/article/details/109333807