logo

Javaの文字列クラス

文字列は一連の文字です。 Java では、String のオブジェクトは不変、つまり定数であり、一度作成すると変更することはできません。

文字列の作成

Java で文字列を作成するには 2 つの方法があります。

1. 文字列リテラル

String s = techcodeview.com;>

2. 使用方法 新しい キーワード

String s = new String (techcodeview.com);>

Java の文字列コンストラクター

1. 文字列(byte[] byte_arr)

デコードして新しい文字列を構築します。 バイト配列 。デコードにはプラットフォームのデフォルトの文字セットが使用されます。



例:

byte[] b_arr = {71, 101, 101, 107, 115}; String s_byte =new String(b_arr); //Geeks>

2. 文字列(byte[] byte_arr, Charset char_set)

デコードして新しい文字列を構築します。 バイト配列 。それは、 文字コード デコード用。

例:

byte[] b_arr = {71, 101, 101, 107, 115}; Charset cs = Charset.defaultCharset(); String s_byte_char = new String(b_arr, cs); //Geeks>

3. 文字列(byte[] byte_arr, String char_set_name)

デコードして新しい文字列を構築します。 バイト配列 。それは、 文字セット名 デコード用。これは上記の構造に似ており、同様の関数の前に表示されますが、 文字列(char_set_nameを含む) 上記のコンストラクターはパラメータとして 文字コード。

例:

byte[] b_arr = {71, 101, 101, 107, 115}; String s = new String(b_arr, 'US-ASCII'); //Geeks>

4. 文字列(byte[] byte_arr, int start_index, int length)

から新しい文字列を構築します。 バイト配列 に応じて start_index(開始位置) そして length(開始位置からの文字数)。

例:

byte[] b_arr = {71, 101, 101, 107, 115}; String s = new String(b_arr, 1, 3); // eek>

5. String(byte[] byte_arr、int start_index、int length、Charset char_set)

から新しい文字列を構築します。 バイト配列 に応じて start_index(開始位置) そして length(開始位置からの文字数) .用途 文字コード デコード用。

例:

配列リストとリンクリスト
byte[] b_arr = {71, 101, 101, 107, 115}; Charset cs = Charset.defaultCharset(); String s = new String(b_arr, 1, 3, cs); // eek>

6. String(byte[] byte_arr, int start_index, int length, String char_set_name)

から新しい文字列を構築します。 バイト配列 に応じて start_index(開始位置) そして length(開始位置からの文字数) .用途 文字セット名 デコード用。

例:

byte[] b_arr = {71, 101, 101, 107, 115}; String s = new String(b_arr, 1, 4, 'US-ASCII'); // eeks>

7. 文字列(char[] char_arr)

指定された文字列から新しい文字列を割り当てます。 文字配列

例:

char char_arr[] = {'G', 'e', 'e', 'k', 's'}; String s = new String(char_arr); //Geeks>

8. String(char[] char_array, int start_index, int count)

指定された文字列から文字列を割り当てます。 文字配列 でも選ぶ カウント のキャラクター スタートインデックス

例:

char char_arr[] = {'G', 'e', 'e', 'k', 's'}; String s = new String(char_arr , 1, 3); //eek>

9. String(int[] uni_code_points, int オフセット, int カウント)

から文字列を割り当てます。 uni_code_array しかし、選択してください カウント のキャラクター スタートインデックス

CSSリスト

例:

int[] uni_code = {71, 101, 101, 107, 115}; String s = new String(uni_code, 1, 3); //eek>

10. 文字列(StringBuffer s_buffer)

の文字列から新しい文字列を割り当てます。 s_buffer

例:

StringBuffer s_buffer = new StringBuffer('Geeks'); String s = new String(s_buffer); //Geeks>

11. 文字列(StringBuilder s_builder)

の文字列から新しい文字列を割り当てます。 s_builder

例:

StringBuilder s_builder = new StringBuilder('Geeks'); String s = new String(s_builder); //Geeks>


Java の文字列メソッド

1. int の長さ()

文字列内の文字数を返します。

'techcodeview.com'.length(); // returns 13>

2. Char charAt(int i)

i の文字を返します番目索引。

'techcodeview.com'.charAt(3); // returns ‘k’>

3. 文字列の部分文字列 (int i)

i から部分文字列を返します番目終了までのインデックス文字。

'techcodeview.com'.substring(3); // returns ksforGeeks>

4. 文字列の部分文字列 (int i, int j)

i から j-1 インデックスまでの部分文字列を返します。

Javaを印刷する方法
 'techcodeview.com'.substring(2, 5); // returns eks>

5. 文字列 concat( 文字列 str)

指定された文字列をこの文字列の末尾に連結します。

 String s1 = Geeks;  String s2 = forGeeks;  String output = s1.concat(s2); // returns techcodeview.com>

6. int IndexOf (文字列)

指定された文字列が最初に出現する文字列内のインデックスを返します。

String s が入力文字列に存在しない場合は、デフォルト値として -1 が返されます。

1. String s = Learn Share Learn;  int output = s.indexOf(Share); // returns 6 2. String s = 'Learn Share Learn'  int output = s.indexOf(Play); // return -1>

7。 int IndexOf (文字列 s, int i)

指定された文字列が最初に出現する文字列内の、指定されたインデックスから始まるインデックスを返します。

 String s = Learn Share Learn;  int output = s.indexOf('ea',3);// returns 13>

8. Int lastIndexOf( String )

指定された文字列が最後に出現した文字列内のインデックスを返します。

String s が入力文字列に存在しない場合は、デフォルト値として -1 が返されます。

 1. String s = Learn Share Learn;  int output = s.lastIndexOf('a'); // returns 14 2. String s = 'Learn Share Learn'  int output = s.indexOf(Play); // return -1>

9. ブール値等しい(オブジェクトotherObj)

この文字列を指定されたオブジェクトと比較します。

 Boolean out = Geeks.equals(Geeks); // returns true  Boolean out = Geeks.equals(geeks); // returns false>

10. ブール値等しい IgnoreCase (String anotherString)

大文字と小文字を区別せずに、文字列を別の文字列と比較します。

 Boolean out= Geeks.equalsIgnoreCase(Geeks); // returns true  Boolean out = Geeks.equalsIgnoreCase(geeks); // returns true>

11. int CompareTo( String anotherString)

2 つの文字列を辞書編集的に比較します。

 int out = s1.compareTo(s2);  // where s1 and s2 are // strings to be compared  This returns difference s1-s2. If :  out <0 // s1 comes before s2  out = 0 // s1 and s2 are equal.  out>0 // s1 は s2 の後に来ます。>>

12. int CompareToIgnoreCase( String anotherString)

大文字と小文字を区別せずに、2 つの文字列を辞書順に比較します。

 int out = s1.compareToIgnoreCase(s2);  // where s1 and s2 are  // strings to be compared  This returns difference s1-s2. If :  out <0 // s1 comes before s2  out = 0 // s1 and s2 are equal.  out>0 // s1 は s2 の後に来ます。>>

注記: この場合、文字の大文字と小文字は考慮されません(大文字か小文字かは無視されます)。

13. 文字列をLowerCase()に変換

文字列内のすべての文字を小文字に変換します。

String word1 = HeLLo; String word3 = word1.toLowerCase(); // returns hello'>

14. 文字列 toUpperCase()

文字列内のすべての文字を大文字に変換します。

String word1 = HeLLo; String word2 = word1.toUpperCase(); // returns HELLO>

15。 文字列トリム()

両端の空白を削除して、文字列のコピーを返します。中間の空白には影響しません。

String word1 = Learn Share Learn ; String word2 = word1.trim(); // returns Learn Share Learn>

16. 文字列置換 (char oldChar, char newChar)

すべての出現箇所を置き換えて新しい文字列を返します。 古いチャー 新しいChar。

String s1 = feeksforfeeks; String s2 = feeksforfeeks.replace(‘f’ ,’g’); // return geeksforgeeks>

注記: s1 は依然としてfeeksforfeeks、s2 は geeksgorgeeks です

17. ブール値に含まれる(文字列) :

string contains に指定された文字列が含まれている場合に true を返します

String s1='geeksforgeeks'; String s2='geeks'; s1.contains(s2) // return true>

18. Char[] toCharArray():

この String を新しい文字配列に変換します。

Cの配列の文字列
String s1='geeksforgeeks'; char []ch=s1.toCharArray(); // returns [ 'g', 'e' , 'e' , 'k' , 's' , 'f', 'o', 'r' , 'g' , 'e' , 'e' , 'k' ,'s' ]>

19. ブール値のstarWith(文字列):

文字列がこのプレフィックスで始まる場合は true を返します。

String s1='geeksforgeeks'; String s2='geeks'; s1.startsWith(s2) // return true>

文字列コンストラクターと文字列メソッドの例

以下は、上記のトピックの実装です。

ジャワ
// Java code to illustrate different constructors and methods // String class. import java.io.*; import java.util.*; // Driver Class class Test {  // main function  public static void main (String[] args)  {  String s= 'techcodeview.com';  // or String s= new String ('techcodeview.com');  // Returns the number of characters in the String.  System.out.println('String length = ' + s.length());  // Returns the character at ith index.  System.out.println('Character at 3rd position = '  + s.charAt(3));  // Return the substring from the ith index character  // to end of string  System.out.println('Substring ' + s.substring(3));  // Returns the substring from i to j-1 index.  System.out.println('Substring = ' + s.substring(2,5));  // Concatenates string2 to the end of string1.  String s1 = 'Geeks';  String s2 = 'forGeeks';  System.out.println('Concatenated string = ' +  s1.concat(s2));  // Returns the index within the string  // of the first occurrence of the specified string.  String s4 = 'Learn Share Learn';  System.out.println('Index of Share ' +  s4.indexOf('Share'));  // Returns the index within the string of the  // first occurrence of the specified string,  // starting at the specified index.  System.out.println('Index of a = ' +  s4.indexOf('a',3));  // Checking equality of Strings  Boolean out = 'Geeks'.equals('geeks');  System.out.println('Checking Equality ' + out);  out = 'Geeks'.equals('Geeks');  System.out.println('Checking Equality ' + out);  out = 'Geeks'.equalsIgnoreCase('gEeks ');  System.out.println('Checking Equality ' + out);  //If ASCII difference is zero then the two strings are similar  int out1 = s1.compareTo(s2);  System.out.println('the difference between ASCII value is='+out1);  // Converting cases  String word1 = 'GeeKyMe';  System.out.println('Changing to lower Case ' +  word1.toLowerCase());  // Converting cases  String word2 = 'GeekyME';  System.out.println('Changing to UPPER Case ' +  word2.toUpperCase());  // Trimming the word  String word4 = ' Learn Share Learn ';  System.out.println('Trim the word ' + word4.trim());  // Replacing characters  String str1 = 'feeksforfeeks';  System.out.println('Original String ' + str1);  String str2 = 'feeksforfeeks'.replace('f' ,'g') ;  System.out.println('Replaced f with g ->' + str2);  } }>>

出力
String length = 13 Character at 3rd position = k Substring ksforGeeks Substring = eks Concatenated string = techcodeview.com Index of Share 6 Index of a = 8 Checking Equality false Checking Equality ...>

セット – 2 については、以下を参照してください。 Java の Java.lang.String クラス |セット2

この記事は次の寄稿者です Rahul Agrawal と役立つユーザー。