查找字符串中一行的最后一个字符
我有一个程序可以读取文件的内容并将其作为字符串。字符串输入如下所示:
Age: 0-11,25203,18,54% ~
Age: 12-19,26722,35,68% ~
Age: 20-39,28427,46,72%. ~
Note: Each ICU admission is also included in the total number of hospitalization.,,, ~
其中每个“~”都是跳行。
我希望将每个年龄行放入一个字符串数组中。我设法找到了放入计数器的字符串中“\n”的数量。所以,我尝试这样做
for(int i=0; i<counter;i++){
array[i]=input.substring(0, input.indexOf("\n")); //Puts the first line of the String in the array
input=input.replaceFirst(array[i],""); //To remove the first line of the String
,但这不起作用,因为字符串不会比第二行进一步剪切。因此,“~”不等于“\n”,因为一旦我的字符串被切碎,我就找不到“\n”
Age: 12-19,26722,35,68% ~
Age: 20-39,28427,46,72%. ~
Note: Each ICU admission is also included in the total number of hospitalization.,,, ~
另外,一旦字符串被切碎,我发现了这种奇怪的行为:
System.out.println("A: "+input.contains("\n"));
System.out.println("B "+input.indexOf('\n'));
这呈现了输出:
A: true
B: 0
所以,我我很困惑,因为“\n”应该位于行尾,而不是索引 0。因此,我认为我无法进一步切割字符串,因为它将“\n”定位在 0 处。
快速说明: 我无法找到确切的信息行中的字符数,然后将其截断,因为它只是一个示例文件,行的长度可能会有所不同。
I have a program that reads the content of a file and puts it as a String. The String input looks like this:
Age: 0-11,25203,18,54% ~
Age: 12-19,26722,35,68% ~
Age: 20-39,28427,46,72%. ~
Note: Each ICU admission is also included in the total number of hospitalization.,,, ~
Where each "~" is a line skip.
I am looking to put each of the Age lines in a String array. I managed to find the number of '\n' in my String that I put into counter. So, I tried to do
for(int i=0; i<counter;i++){
array[i]=input.substring(0, input.indexOf("\n")); //Puts the first line of the String in the array
input=input.replaceFirst(array[i],""); //To remove the first line of the String
But this doesn't work as the String won't cut further than the second line. Therefore, "~" is not equal to '\n' as I cannot find a '\n' once my String is chopped to
Age: 12-19,26722,35,68% ~
Age: 20-39,28427,46,72%. ~
Note: Each ICU admission is also included in the total number of hospitalization.,,, ~
Also, I found this strange behaviour once the String was chopped:
System.out.println("A: "+input.contains("\n"));
System.out.println("B "+input.indexOf('\n'));
Which renders the output:
A: true
B: 0
And so, I am confused because "\n" should be at the end of my line and not at index 0. As such, I think I can't chop my string further because it locates the "\n" at 0.
Quick note: I am not allowed to find the exact number of characters in the line and chop it after since it's only a sample file and the length of the line could vary.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
substring
的第二个参数是子字符串的末尾。substring(a,b) 获取从索引 a 到索引 b-1 的子字符串。
示例:
这意味着对于您的示例,您将删除除换行符之外的所有内容,因此第二次循环运行时,输入将换行符作为第一个字符。你得到
indexOf('\n')
= 0,什么也没有发生。要将换行符包含在 array[i] 中,您必须执行以下操作
substring
's second argument is the end of the substring, exclusively.substring(a,b) get you the substring from index a to index b-1.
Example:
Which means for your example, you are cutting everything away except the newline, so the second time your loop runs, input has a newline as the first character. You get
indexOf('\n')
= 0, nothing happens.To include the newline in array[i], you have to do