java string length method with examples
このチュートリアルでは、Java String length()メソッドと、概念を理解するのに役立つ複数のプログラミング例とFAQについてすべて説明します。
さらに、String Java length()メソッドに関連するさまざまなシナリオについても説明します。 Java String length()メソッドに関連するよくある質問もこのチュートリアルの一部になります。
このチュートリアルを完了すると、文字列の長さを計算して、さまざまなケースやシナリオで使用できるようになります。このメソッドは、他のJavaStringメソッドとうまく機能します。
=> ここで完璧なJavaトレーニングガイドをチェックしてください
学習内容:
Java文字列の長さ
文字列の長さは、文字列に含まれる文字数に他なりません。 Javaには、任意の文字列の文字数を検索するためのlength()というメソッドが組み込まれています。
構文:
構文は次のように与えられます
int length();
ここで、length()は文字数を検索し、結果をとして返すメソッドです。 整数 。
文字列の長さを見つける
この例では 、Java String length()メソッドの最も単純な形式について説明します。文字列をある値で初期化してから、長さを計算します。
public class length { public static void main(String() args) { // Initialized a String variable String str = 'Testing'; // Initialized a count variable which will store the length int count = str.length(); // Printed the count variable or the length of String. System.out.println('The String has ' +count +' characters'); } }
出力:
文字配列の長さを見つける
この例では 、文字配列「chars」を作成し、それらの文字を文字列変数「str」にマージしてから、変数と長さを出力しました。
public class length { public static void main(String() args) { // Initialized a character array char chars() = { 'T', 'e', 's', 't', 'i', 'n', 'g' }; // Initialized a String variable str with chars characters String str = new String(chars); // Printed the String variable System.out.println(str + ' has '); // Printed the length of the String Variable System.out.println(str.length()+ ' characters'); } }
出力:
Java文字列の長さのシナリオ
シナリオ1:シナリオ1: 空白のある文字列の長さを検索します。
説明: このシナリオでは、複数の単語または部分文字列があり、それらが空白で区切られている文字列の長さがわかります。
ここでは、文字として扱われる単一および二重の空白で2つの文字列変数を初期化しました。次に、長さを格納する2つのカウント変数を初期化しました。
最後に、カウント変数を出力しました。
4年の経験のためのセレンウェブドライバーインタビューの質問
public class length { public static void main(String() args) { // Initialized a String variable with a single whitespace String str1 = 'This is'; // Initialized another String variable with two whitespace String str2 = 'Software Testing Help'; /* * Initialized a count1 variable which will store the length of the first String. */ int count1 = str1.length(); /* * Initialized a count2 variable which will store the length of the second String. */ int count2 = str2.length(); // Printed the count1 variable. System.out.println('The First String has ' + count1 + ' characters'); // Printed the count2 variable. System.out.println('The Second String has ' + count2 + ' characters'); } }
出力:
シナリオ2:シナリオ2: 特殊文字を含む文字列の長さを検索します。
説明: ここでは、文字列を特殊文字で初期化し、文字列の長さを取得しようとします。
public class length { public static void main(String() args) { // Initialized a String variable with special characters String str = 'P@!.90$%'; /* * Initialized a count variable which will store the length of the String. */ int count = str.length(); // Printed the count variable. System.out.println('The String has ' + count + ' characters'); } }
出力:
よくある質問
Q#1)JavaでString length()は何をしますか?
回答: 文字列の文字数を返します。 Javaのインデックスは0から始まり、文字列のn番目の文字まで続きます。
長さは、最後の要素のインデックス+1になります。
jsonファイルを開く方法
例えば:
String str =“ Hello World”
ここで、Hはインデックス(0)にあり、eはインデックス(1)にあります。
最後の要素は、index (10)にあるdです。したがって、全長は11です。
Q#2) Javaの文字とは何ですか?
回答: 文字は、文字列を形成するために一緒に結合する文字に他なりません。 Javaは、空白も文字と見なします。空白や特殊文字などを含む文字列の長さを計算する場合、それらは文字として扱われます。
すべての文字のサイズは1です。
Q#3) Javaで指定されたサイズの文字列を作成するにはどうすればよいですか?
回答: このプログラムでは、2つの定数を作成しました。最初の定数は文字列で繰り返し発生する文字であり、2番目の定数はそれが発生する回数です。次に、文字配列のすべての要素をStringに格納しました。
その後、すべてのNULL文字を最初の定数文字に置き換えました。最後に、文字列を返し、値を出力しました。
public class length { // Initialized a constant character which will repeatedly occur static final char chars = '$'; // Specied a constant length limit as 5 static final int StrLen = 5; public static void main(String() args) { // printing the return value of the create method System.out.println(create()); } public static String create(){ //created a new String from the character array String str = new String(new char(StrLen)); //replaced all NULL chars ' ' with specified character $ str = str.replace(' ', chars); return str; } }
出力:
Q#4) 文字列の長さを変更するにはどうすればよいですか?
回答: 以下のプログラムでは、部分文字列を空白に置き換えることにより、文字列の長さを変更しています。
入力文字列を取得し、文字列と文字列の長さを出力しました。次に、メイン文字列の部分文字列を空白の値に置き換えました。
ここでも、文字列と文字列の長さを出力しました。
public class length { public static void main(String() args) { // Initialized a String variable String str = 'Software Test'; // Printed the String and the length System.out.println(str + ' has ' +str.length()+ ' characters'); // Replaced the substring Test with a blank value str = str.replace(' Test', ''); // Printed the String and the length System.out.println(str + ' has ' +str.length()+ ' characters'); } }
出力:
Q#5) Javaの配列の長さはどれくらいですか? String length()とどう違うのですか?
回答: 配列では、長さは配列の長さを取得するために使用される変数です。 Array.lengthを入力するだけで、長さがわかります。
Stringでは、length()は、Stringの長さを取得するために使用されるメソッドです。 String.length()を入力して長さを取得します
以下のプログラムでは、それがどのように機能するかを見てみましょう。
public class length { public static void main(String() args) { // Specified the length of an Array as 4. int() arr = new int(4); // returned the length of an Array System.out.println('Array length is ' + arr.length); String str = 'Saket'; // returned the length of the String System.out.println('String length() is ' + str.length()); } }
出力:
結論
このチュートリアルでは、Java String length()メソッドについて詳しく理解しました。これは、目的の結果を達成するために他のStringメソッドと連携して使用される最も基本的なStringメソッドです。
理解を深めるために、文字列の長さに関連するさまざまなケースまたはシナリオとFAQを示しました。この方法の機能領域は小さいですが、アプリケーション領域は他の方法と同じくらい大きいです。
これは、文字列クラスの最も単純で基本的なメソッドです。
=> 独占的なJavaトレーニングチュートリアルシリーズについては、こちらをご覧ください。