⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 teststringsearch.java

📁 Java the UML Way 书中所有源码
💻 JAVA
字号:
/*
 * TestStringSearch.java   E.L. 2001-05-25
 */

class TestStringSearch {
  public static void main(String[] args) {
    String text = "This is a testing text with many e's";
    System.out.println("Testing the text: " + text);
    System.out.println("This text's length is: " + text.length());
    System.out.println("The positions are numbered from 0, and up to and including "
        + (text.length() - 1));

    /* Searching from the beginning with the method indexOf() */
    int pos = text.indexOf('e');
    System.out.println("The first e is at the position: " + pos);
    pos = text.indexOf('e', pos + 1);  // The search continues after the first e
    System.out.println("The next e is at the position: " + pos);
    pos = text.indexOf("text");
    System.out.println("The first occurrence of \"text\" is at the position: " + pos);
    pos = text.indexOf("text", pos + 1);
    System.out.println("The next occurrence of \"text\" is at the position: " + pos);

    /* Searching from the end with the method lastIndexOf() */
    pos = text.lastIndexOf('e');
    System.out.println("The last e is at the position: " + pos);
    pos = text.lastIndexOf('e', pos - 1);  // The search continues before the last e
    System.out.println("The last but one e is at the position: " + pos);
    pos = text.lastIndexOf("text");
    System.out.println("The last occurrence of \"text\"  is at the position: " + pos);
    pos = text.lastIndexOf("text", pos - 1);
    System.out.println("The last occurrence but one of \"text\"  is at the position: " + pos);

    /* Finding substrings */
    String partOfText = text.substring(10);
    System.out.println("From position 10: " + partOfText);
    partOfText = text.substring(7, 14);
    System.out.println("From position 7 including position 13: " + partOfText);

    /* Replacing all e's with a's */
    String newText = text.replace('e', 'a');
    System.out.println("The new text with a's instead of e's: " + newText);

    System.out.println("None of all these operations has changed the original text: "
        + text);
  }
}

/* Example Run:
Testing the text: This is a testing text with many e's
This text's length is: 36
The positions are numbered from 0, and up to and including 35
The first e is at the position: 11
The next e is at the position: 19
The first occurrence of "text" is at the position: 18
The next occurrence of "text" is at the position: -1
The last e is at the position: 33
The last but one e is at the position: 19
The last occurence of "text"  is at the position: 18
The last occurance but one of "text"  is at the position: -1
From position 10: testing text with many e's
From position 7 including position 13:  a test
The new text with a's in stead of e's: This is a tasting taxt with many a's
None of all these operations has changed the original text: This is a testing text with many e's
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -