java - Java实践中修改字符串

标签 java string methods

public static void displayWords(String line)
{
  // while line has a length greater than 0
  //   find the index of the space.
  //   if there is one,
  //    print the substring from 0 to the space
  //    change line to be the substring from the space to the end
  //   else
  //    print line and then set it equal to the empty string

}
  line = "I love you";
  System.out.printf("\n\n\"%s\" contains the words:\n", line );
  displayWords(line);

嗨,我在上面的代码中遇到了麻烦,这是一个练习问题,但是“将行更改为从空格到末尾的子字符串”确实让我感到困惑。我知道如何找到空间索引并从开头打印字符串到索引。

预期输出:

I
love
you

最佳答案

SLaks is recommending in his comment您可以使用 substring() 方法。检查API documentation for String :

public String substring(int beginIndex, int endIndex)

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

Examples:

  • "hamburger".substring(4, 8) returns "urge"
  • "smiles".substring(1, 5) returns "mile"

所以,考虑一下:

public static void displayWords(String line) {
    while(line.length() > 0) { // This is the first part (the obvious one)
        /*
           You should do something here that tracks the index of the first space 
           character in the line. I suggest you check the `charAt()` method, and use 
           a for() loop.
           Let's say you find a space at index i
         */
         System.out.println(line.substring(0,i)); // This prints the substring of line
                                                  // that goes from the beginning
                                                  // (index 0) to the position right
                                                  // before the space
         /*
           Finally, here you should assign a new value to line, that value must start
           after the recently found space and end at the end of the line.
           Hint: use substring() again, and remember that line.lengh() will give you
                 the position of the last character of line plus one.
          */
    }
}
<小时/>

你已经有足够的时间了...今晚我感觉很慷慨。所以这是解决方案:

public static void displayWords(String line) {
    int i; // You'll need this
    while(line.length() > 0) { // This is the first part (the obvious one)
        /*
           Here, check each character and if you find a space, break the loop
         */
        for(i = 0; i < line.length(); i++) {
            if(line.charAt(i) == ' ') // If the character at index i is a space ...
                break;                // ... break the for loop
        }
        System.out.println(line.substring(0,i)); // This prints the substring of line
                                                 // that goes from the beginning
                                                 // (index 0) to the position right
                                                 // before the space
        /*
          Here, the new value of line is the substring from the position of the space
          plus one to the end of the line. If i is greater than the length of the line
          then you're done.
         */
        if(i < line.length())
            line = line.substring(i + 1, line.length());
        else
            line = "";
    }
}
<小时/>

再次阅读你的问题后,我意识到它应该是一个递归解决方案......所以这是递归方法:

public static void displayWords(String line) {
    int i;
    for(i = 0; i < line.lengh(); i++) {
        if(line.charAt(i) = ' ')
            break;
    }
    System.out.println(line.substring(0, i);
    if(i < line.lengh())
        line = line.substring(i + 1, line.lengh();
    else
        line = "";
    if(line.lengh() > 0)
        displayWords(line);
}

关于java - Java实践中修改字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25955669/

相关文章:

java - 生成集合的笛卡尔幂

java - Gradle 任务 - 将参数传递给 Java 应用程序

python : Joining separated numeric characters in text but leave alphabetic texts

c++ - 将 int 转换为 base 2 cstring/string

java - 方法 sum(int, int, int, int) 不适用于参数 (int)

JavaScript 数组#map : index argument

javascript - 在 JavaScript 中创建二维数组的方法

java - Android onclicklistener 在不同的布局下,我一直失败

java - Android Fragmenttransaction .replace 错误的第二个参数

regex - 在正则表达式中将一组模式中的模式列入受控多次出现的白名单