2009-06-26 5 views
26
String s1 = "The quick brown fox jumps over the lazy dog"; 
String s2 = ""; 
boolean b = s1.contains(s2); 
System.out.println(b); 

上記のJavaコードを実行すると、bがtrueを返します。s2は空であるため、なぜs1にs2が含まれていますか?JavaのString.contains

私は、Java APIをチェックし、それは書く:

戻り値を真のこの文字列が指定されたchar値の配列を含む場合にのみ。

パラメータ:

S - シーケンスは

戻り値を検索します。この文字列は、Sが含まれている

trueの場合、そうでない場合はfalse

+0

空の文字列に関するいくつかのメソッド: System.out.println( "123" .contains( "")); // true System.out.println( "123" .startsWith( "")); // true System.out.println( "123" .endsWith( "")); // true System.out.println( "123" .indexOf( "")); // 0 するSystem.out.println( "123" .lastIndexOf( "")); // 3 –

+0

空の文字列についてのいくつかのメソッド:

 System.out.println("123".contains("")); // true System.out.println("123".startsWith("")); // true System.out.println("123".endsWith("")); // true System.out.println("123".indexOf("")); // 0 System.out.println("123".lastIndexOf("")); // 3 

答えて

45

空は任意の文字列のサブセットです。

2文字の間に何があるかを考えてください。任意のサイズのライン上には無数の点がある道の

種類...

(うーん...私は空の文字列の無限の数を連結するために微積分を使用した場合、私はなるだろうのだろうか)

"。"は( "")と同じであることに注意してください。

+1

実際には無限ではありません。おそらく文字数+ 1、各文字の前に空の文字列があり、最後にもう1文字あります。 – belugabob

+12

""と手紙の間に ""があるとは思わない? :) –

+0

はもう一度あなたの答えを読んで、あなたが話していた「ライン」は紙の上に描かれた線だったことに気づきました。実際には各 ""の間にメタ ""があります;-) – belugabob

11

同様に:したがって

"".contains("");  // Returns true. 

、空の文字列が任意の文字列に含まれているように見えますString

4

本当の説明は(JavaDocのか、非常に切望されたコードのコメントのいずれかで)は、Javaで指定されていないが、コードを見て、魔法のようです:

呼び出しスタック:

String.indexOf(char[], int, int, char[], int, int, int) line: 1591 
String.indexOf(String, int) line: 1564 
String.indexOf(String) line: 1546 
String.contains(CharSequence) line: 1934  

コード:

/** 
* Code shared by String and StringBuffer to do searches. The 
* source is the character array being searched, and the target 
* is the string being searched for. 
* 
* @param source  the characters being searched. 
* @param sourceOffset offset of the source string. 
* @param sourceCount count of the source string. 
* @param target  the characters being searched for. 
* @param targetOffset offset of the target string. 
* @param targetCount count of the target string. 
* @param fromIndex the index to begin searching from. 
*/ 
static int indexOf(char[] source, int sourceOffset, int sourceCount, 
        char[] target, int targetOffset, int targetCount, 
        int fromIndex) { 
    if (fromIndex >= sourceCount) { 
     return (targetCount == 0 ? sourceCount : -1); 
    } 
     if (fromIndex < 0) { 
     fromIndex = 0; 
     } 
    if (targetCount == 0) {//my comment: this is where it returns, the size of the 
    return fromIndex; // incoming string is 0, which is passed in as targetCount 
    }      // fromIndex is 0 as well, as the search starts from the 
         // start of the source string 
    ...//the rest of the method 
1

「これはJLSの言葉です」

この現象がなぜ起こるかを考えてみると、この動作が特定の場合に役立つと考えることができます。文字列を他の文字列のセットと照合したいが、他の文字列の数は変わる可能性があるとしよう。

は、だから、このようなものを持っている:いくつかのs年代が空の文字列です

for(String s : myStrings) { 
    check(aString.contains(s)); 
} 

空の文字列が「入力なし」と解釈されている場合は、ここであなたの目的はaStringmyStrings内のすべての「入力」が含まれ、空の文字列がfalseを返すようにするために、それは誤解を招くことを確実にある場合。それは何もないので、すべての文字列に含まれています。彼らがそれを含んでいなかったと言うのは、空文字列に文字列に取り込まれていない物質があることを意味します。

2

私は数学のアナロジーを使用して、あなたの疑問にお答えします。この例では

、番号0を何も値を表しているんだろう。乱数を選んだ場合、15で何回15を減算することができますか?無限の時間は0に値がないので、15から何も外していません。エラーの代わりに15 - 0 = 15を受け入れることは難しいですか?したがって、この類推をJavaコーディングに戻すと、文字列 ""は値を表しません。ランダムな文字列を選んで、 "hello world"と言うと、 "hello world"から何回 ""引くことができますか?文字の集合として文字列の

0

思考は、数学で空のセットには、常にセットのサブセットです。

関連する問題