关于 indexof(" ")
about the indexof(" ")
String str = "1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th";
int start = 0;
int end = 0;
int temp3 = str.indexOf(" ");
int i = 0;
int x = 6;
while(i < x){
System.out.println("current start: " + start);
start = str.indexOf(" ", temp3);
i++;
temp3 += str.indexOf(" ");
}
end = str.indexOf(" ", start + 1);
String sample = str.substring(start, end);
System.out.println("HERE: " + sample);
我正在编写一个程序,允许用户输入数字并打印字符串中的特定位置,例如
String str = "1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th";
当用户输入 0 时,它打印 1st 或者当他输入 1 时,它打印 2nd
所以我决定如何制作它的方法是找到包含特定字符串的两个空格的索引:"1st" or "2nd" or "3rd".....
,并将这些索引分配给两个名为 start 和 end 的变量。和将两者都作为子字符串的参数来打印特定的字符串
在上面的代码中,它一直工作到变量 x 为 6,这是输出:
current start: 0
current start: 3
current start: 7
current start: 11
current start: 15
current start: 15
HERE: 6th
它重复 15 两次,字符串不应该是第 6 个,它应该是:
0: 第一,
1:第二,
2:第三,
3:第 4,
4:5,
5:6,
6: 7
等等...
而且不只是6,当变量x为10时,它也会重复一个数字27两次
我试图找到问题,但我不知道
有人知道问题出在哪里吗?以及如何解决?
谢谢
代码中有几处需要注意,但主要是您的错误归结为这一行:
start = str.indexOf(" ", temp3); // why temp3?
...结合莫名:
temp3 += str.indexOf(" "); // no idea what this is trying to do.
相反,只需完全删除 temp3
变量,然后像这样执行 indexOf
:
start = str.indexOf(" ", start + 1); // start + 1: look right after the last space that was found.
您的解决方案存在一些问题。该字符串未以 space 结尾,因此如果您尝试在
处找到结尾
end = str.indexOf(" ", start + 1);
对于最后一个元素,您将得到 -1。
此外,当您在循环中时,您希望 temp3 指向下一个 space 位置,但您将 temp 指定为
temp3 += str.indexOf(" ");
这将始终将 3 添加到 temp3,但是当元素数量增加时,字符数并不总是 3,例如“10th”需要 4 个字符,因此您不能只添加 3 个字符到温度 3.
我想你需要的是
temp3 = start+1
您很快就会意识到您根本不需要 temp3。
一个更简单的解决方案是将字符串按 space 拆分,然后 return 第 x 个元素,就像这样。
String str = "1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th";
int x = 6;
String[] tokens = str.split(" ");
System.out.println(tokens[x]);
String str = "1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th";
int start = 0;
int end = 0;
int temp3 = str.indexOf(" ");
int i = 0;
int x = 6;
while(i < x){
System.out.println("current start: " + start);
start = str.indexOf(" ", temp3);
i++;
temp3 += str.indexOf(" ");
}
end = str.indexOf(" ", start + 1);
String sample = str.substring(start, end);
System.out.println("HERE: " + sample);
我正在编写一个程序,允许用户输入数字并打印字符串中的特定位置,例如
String str = "1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th";
当用户输入 0 时,它打印 1st 或者当他输入 1 时,它打印 2nd
所以我决定如何制作它的方法是找到包含特定字符串的两个空格的索引:"1st" or "2nd" or "3rd".....
,并将这些索引分配给两个名为 start 和 end 的变量。和将两者都作为子字符串的参数来打印特定的字符串
在上面的代码中,它一直工作到变量 x 为 6,这是输出:
current start: 0
current start: 3
current start: 7
current start: 11
current start: 15
current start: 15
HERE: 6th
它重复 15 两次,字符串不应该是第 6 个,它应该是:
0: 第一, 1:第二, 2:第三, 3:第 4, 4:5, 5:6, 6: 7 等等...
而且不只是6,当变量x为10时,它也会重复一个数字27两次
我试图找到问题,但我不知道
有人知道问题出在哪里吗?以及如何解决?
谢谢
代码中有几处需要注意,但主要是您的错误归结为这一行:
start = str.indexOf(" ", temp3); // why temp3?
...结合莫名:
temp3 += str.indexOf(" "); // no idea what this is trying to do.
相反,只需完全删除 temp3
变量,然后像这样执行 indexOf
:
start = str.indexOf(" ", start + 1); // start + 1: look right after the last space that was found.
您的解决方案存在一些问题。该字符串未以 space 结尾,因此如果您尝试在
处找到结尾end = str.indexOf(" ", start + 1);
对于最后一个元素,您将得到 -1。
此外,当您在循环中时,您希望 temp3 指向下一个 space 位置,但您将 temp 指定为
temp3 += str.indexOf(" ");
这将始终将 3 添加到 temp3,但是当元素数量增加时,字符数并不总是 3,例如“10th”需要 4 个字符,因此您不能只添加 3 个字符到温度 3.
我想你需要的是
temp3 = start+1
您很快就会意识到您根本不需要 temp3。
一个更简单的解决方案是将字符串按 space 拆分,然后 return 第 x 个元素,就像这样。
String str = "1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th";
int x = 6;
String[] tokens = str.split(" ");
System.out.println(tokens[x]);