JavaのStringBuilderクラス

WBOY
リリース: 2024-08-30 16:01:37
オリジナル
802 人が閲覧しました

StringBuilder クラスは、String クラスの代替となる Java クラスです。 String クラスが同様に変更可能な文字シーケンスを作成するのと同様に、StringBuilder クラスも変更可能な文字シーケンスを作成します。 StringBuilder クラスは、文字列を操作するための重要なクラスの 1 つです。パフォーマンスを向上させるために Java 1.5 で追加されました。簡単に言えば、StringBuilder クラスは変更可能な String を作成するために使用されると言えます。 StringBuffer クラスとは同期を保証しないなどの点が異なります。 StringBuilder クラスは同期されていないため、マルチスレッドの場合は安全ではありません。 StringBuilder クラスの実装は、StringBuffer クラスの実装より高速です。

Java の StringBuilder クラスとは何ですか?

StringBuilder クラスは java.lang パッケージによってインポートされます。 StringBuilder クラスは、オブジェクト クラスを拡張してそのプロパティを使用し、オブジェクト クラスは Serializable と CharSequence を使用します。 StringBuilder オブジェクトは String オブジェクトに似ています。 StringBuffer クラスは、StringBuffer クラスのドロップイン置換として使用するために作成されます。 StringBuilder クラスの一部のメソッドは、それ自体への参照を返します。複数の操作を 1 つのステートメント内の StringBuilder クラス インスタンスとして適用できます。単一のステートメントでのこのような操作方法は、メソッド チェーンとして知られています。

広告 このカテゴリーの人気コース JAVA マスタリー - スペシャライゼーション | 78 コース シリーズ | 15 回の模擬テスト

構文:

public final class StringBuilder extends Object implements Serializable, CharSequence
ログイン後にコピー

文字列ビルダークラスのコンストラクター

StringBuilder クラスは、さまざまな文字列を操作するためのコンストラクターとそのパラメーターを提供します。

  • StringBuilder(): パラメーターを指定しない StringBuilder コンストラクターは、最初に 16 文字を保持できる文字列ビルダーを作成します。
  • StringBuilder(int stringLength): 指定された文字列長の StringBuilder コンストラクターは、指定された長さの文字列ビルダーを作成します。
  • StringBuilder(String str): 指定された文字列を含む StringBuilder コンストラクターは、指定された文字列を含む文字列ビルダーを作成します。

Java の String Builder クラスのメソッド

StringBuilder クラスは、文字列を操作するための重要なメソッドをいくつか提供します。

1. append()

append メソッドにパラメータとして渡される文字列は、append メソッドが適用される文字列の後に連結されます。

コード:

class StringBuilderExample{
//main method of class
public static void main(String args[]){
StringBuilder strSB = new StringBuilder("This is an example of ");
strSB.append("append method.");
System.out.println(strSB);
}
}
ログイン後にコピー

出力:

JavaのStringBuilderクラス

2. insert()

挿入メソッドにパラメータとして渡された文字列は、挿入メソッドが適用される文字列の指定されたインデックス (最初のパラメータとして渡された) に挿入されます。

コード:

class StringBuilderExample{
public static void main(String args[]){
StringBuilder strSB = new StringBuilder("This is a program");
strSB.insert(10, "java ");
System.out.println(strSB);
}
}
ログイン後にコピー

出力:

JavaのStringBuilderクラス

3. replace()

replace メソッドにパラメータとして渡された文字列は、開始インデックスと最後のインデックスの文字の間にあるすべての文字を置き換えます。この最初と最後のインデックスは、replace メソッドの最初と 2 番目のパラメーターとして渡されます。

コード:

class StringBuilderExample{
public static void main(String args[]){
StringBuilder strSB = new StringBuilder("This is a program");
strSB.replace(0, 9, "java");
System.out.println(strSB);
}
}
ログイン後にコピー

出力:

JavaのStringBuilderクラス

4. delete()

delete() は、開始インデックスと最後のインデックスの文字の間にあるすべての文字を置き換えます。この最初と最後のインデックスは、delete() メソッドの最初と 2 番目のパラメーターとして渡されます。

コード:

class StringBuilderExample{
public static void main(String args[]){
StringBuilder strSB = new StringBuilder("This is a program");
strSB.delete(0, 10);
System.out.println(strSB);
}
}
ログイン後にコピー

出力:

JavaのStringBuilderクラス

5. reverse()

reverse() メソッドは、reverse メソッドが適用される文字列を反転します。

コード:

class StringBuilderExample{
public static void main(String args[]){
StringBuilder strSB = new StringBuilder("This is a program");
strSB.reverse();
System.out.println(strSB);
}
}
ログイン後にコピー

出力:

JavaのStringBuilderクラス

6.容量()

StringBuilder のデフォルトの容量は 16 です。ビルダーの容量は (容量 * n) + n だけ増やすことができます。

コード:

class StringBuilderExample{
public static void main(String args[]){
StringBuilder strSB = new StringBuilder();
System.out.println(strSB.capacity());
strSB.append("This is a program");
System.out.println(strSB.capacity());
}
}
ログイン後にコピー

出力:

JavaのStringBuilderクラス

7.長さ()

length() メソッドは、指定された文字列の長さを返します。

コード:

class StringBuilderExample{
public static void main(String args[]){
StringBuilder strSB = new StringBuilder("This is a java program");
System.out.println(strSB.length());
}
}
ログイン後にコピー

出力:

JavaのStringBuilderクラス

8. deleteCharAt()

deleteCharAt() メソッドは、パラメータとして渡された指定されたインデックスの文字を削除します。

コード:

class StringBuilderExample{
public static void main(String args[]){
StringBuilder strSB = new StringBuilder("This is a java program");
strSB.deleteCharAt(6);
System.out.println(strSB);
}
}
ログイン後にコピー

出力:

JavaのStringBuilderクラス

9. setCharAt(int index, char ch)

setCharAt() method will set the specified char at the specified index passed as the first parameter while the second parameter will be the character.

Code:

class StringBuilderExample{
public static void main(String args[]){
StringBuilder strSB = new StringBuilder("This is a java program");
strSB.setCharAt(8, 'n');
System.out.println(strSB);
}
}
ログイン後にコピー

Output:

JavaのStringBuilderクラス

10. indexOf()

This method returns the first position’s index in the string for the specified substring passed as a parameter.

Code:

class StringBuilderExample{
public static void main(String args[]){
StringBuilder strSB = new StringBuilder("This is a java program");
System.out.println(strSB.indexOf("java"));
}
}
ログイン後にコピー

Output:

JavaのStringBuilderクラス

StringBuilder class also has some other methods to work with the string which are listed below setLength(), toString(), trimToSize(), substring() etc.

Conclusion

StringBuilder class has some important methods such as speed & capacity, which other classes don’t have. The use of the StringBuilder class makes manipulation of string much easier. It is a useful class to work with the strings in java. If a string is changing frequently & accessible by a single thread, in that case, StringBuilder is better than other classes like String & StringBuffer.

以上がJavaのStringBuilderクラスの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!