在Java实践中修改字符串

时间:2014-09-21 04:06:30

标签: 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

2 个答案:

答案 0 :(得分:1)

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

  

public String substring(int beginIndex, int endIndex)

  返回一个新字符串,该字符串是此字符串的子字符串。子字符串从指定的beginIndex开始,并扩展到索引endIndex - 1处的字符。因此子串的长度为endIndex-beginIndex

  例子:

     
      
  • "hamburger".substring(4, 8)返回"urge"
  •   
  • "smiles".substring(1, 5)返回"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);
}

答案 1 :(得分:0)

另一种方法。

public static void displayWords(String line)
{
  Scanner s = new Scanner(line)
  while (s.hasNext())
  {
    System.out.println(s.next())
  }
}

http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html&lt;使用此作为参考。