String.contains в Java

Да, блок - это наиболее часто используемая функциональность, поэтому, чтобы избежать цикла сохранения, мы должны избегать использования сильной переменной, включая self внутри блока, несмотря на использование _weak или weakself.

26
задан vitaut 27 November 2013 в 19:28
поделиться

3 ответа

Пусто - это подмножество любой строки.

Думайте о них как о том, что находится между каждыми двумя символами.

Отчасти бесконечное количество точек на строке любого размера ...

(Хм ... Интересно, что я получу, если Я использовал исчисление для объединения бесконечного числа пустых строк)

Обратите внимание, что только "" .equals ("").

45
ответ дан 28 November 2019 в 06:37
поделиться

Аналогично:

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

Следовательно, кажется, что пустая строка содержится в любой строке .

11
ответ дан 28 November 2019 в 06:37
поделиться

Java не дает реального объяснения (ни в JavaDoc, ни в очень желанных комментариях к коду), но, глядя на код, кажется, что это волшебство:

стек вызовов:

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 
4
ответ дан 28 November 2019 в 06:37
поделиться
Другие вопросы по тегам:

Похожие вопросы: