如何制作“this,字符串格式”分成两个标记(“this”和“字符串格式”)
我正在读取一个文件,每一行如下。
Derek Simons, Jason baker
Jack Smith, Rob Thomson
问题是我的分词器的
StringTokenizer st = new StringTokenizer(line, ",");
while(st.hasMoreTokens()){
System.out.println(st.nextToken());
}
输出是
Derek Simons
Jason baker
Jack Smith
Rob Thomson
如何摆脱多余的空格?这样输出就会是
Derek Simons
Jason baker
Jack Smith
Rob Thomson
任何帮助,谢谢!
Im reading in a file and each line is as follows
Derek Simons, Jason baker
Jack Smith, Rob Thomson
The problem is with my tokenizer
StringTokenizer st = new StringTokenizer(line, ",");
while(st.hasMoreTokens()){
System.out.println(st.nextToken());
}
the output is
Derek Simons
Jason baker
Jack Smith
Rob Thomson
how can I get rid of that extra blank space? so that the output would be
Derek Simons
Jason baker
Jack Smith
Rob Thomson
Any help is appreciated thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不知道你使用的是哪种编程语言,但是在很多语言中都有一个叫做 Trim() 的东西。所以你执行 s.Trim();,其中 s 是字符串。这将删除所有空白
I don't know which programming language you are using, but in many languages there is something called Trim(). so you do s.Trim();, where s is the string. That will remove all blanks
如果输入始终采用您所描述的形式,则可以避免手动修剪每个字符串:
现在
tokens
将包含每个名称,而第二个标记中没有前导空格。You can avoid having to trim each string manually if the input is always in the form you describe:
Now
tokens
will contain each name without leading spaces in the second token.您是否考虑过使用 .trim 方法?
Did you consider using the .trim method?
您可以使用trim(),甚至只是将StringTokenizer 的分隔符指定为“,”(逗号后跟空格),而不仅仅是“,”。
You could use trim() or even just specify you StringTokenizer's delimiter as ", " (a comma followed by a space) instead of just ",".