gpt4 book ai didi

java - Java实践中修改字符串

转载 作者:行者123 更新时间:2023-12-01 12:30:29 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com