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

📄 extractsubstring.java

📁 Java2入门经典第四章源码
💻 JAVA
字号:
public class ExtractSubstring
{
  public static void main(String[] args)
  {
    String text = "To be or not to be";        // String to be segmented
    int count = 0;                             // Number of substrings
    char separator = ' ';                      // Substring separator

    // Determine the number of substrings
    int index = 0;
    do
    {
      ++count;                           // Increment count of substrings
      ++index;                                 // Move past last position
      index = text.indexOf(separator, index);
    }
    while (index != -1);

    // Extract the substring into an array
    String[] subStr = new String[count];       // Allocate for substrings
    index = 0;                                 // Substring start index
    int endIndex = 0;                          // Substring end index
    for(int i = 0; i < count; i++)
    {
      endIndex = text.indexOf(separator,index);  // Find next separator

      if(endIndex == -1)                       // If it is not found
        subStr[i] = text.substring(index);     // extract to the end
      else                                             // otherwise
        subStr[i] = text.substring(index, endIndex);   // to end index

      index = endIndex + 1;                    // Set start for next cycle
    }

    // Display the substrings
    for(int i = 0; i < subStr.length; i++)
      System.out.println(subStr[i]);
  }
}

⌨️ 快捷键说明

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