如何将文本拆分为字符串?
我想将一个文本文件拆分为字符串,您能告诉我如何拆分它吗?例如,给出以下文本文件:
this course in, a style i
will have to a modern, language that encourages
writing clean; and elegant code in a good
是否有可能将文本文件拆分为如下字符串,例如 2 个单词:
this course
in a
style i
will have
to a
modern language
that encourages
writing clean
and elegant
code in
a good
您能给我一些提示吗?先感谢您。
I want to split a text file into strings, can you please tell me how to split it. For example, the following text file is given:
this course in, a style i
will have to a modern, language that encourages
writing clean; and elegant code in a good
Is there any possibility to split the text file into strings like following, for example by 2 words:
this course
in a
style i
will have
to a
modern language
that encourages
writing clean
and elegant
code in
a good
Can you please give me some hints? Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一些想法:
1) 使用
java.util.Scanner
使用next(pattern: String)
方法直接从文件中读取标记,或者
2) 读取所有行 (参见 scala.io.Source ),将它们连接成一个字符串,将字符串分割成一个数组,然后使用 grouped 方法进行分割将其放入 2 个元素的子数组中
Some ideas:
1) Use
java.util.Scanner
to read in tokens direct from the file using thenext(pattern: String)
methodor
2) Read in all lines (see
scala.io.Source
), concatenate them into a single string,split
the string into an array, then use thegrouped
method to split that into sub-arrays of 2 elements除了路易吉的回答。
3)你应该考虑过滤掉标点符号。
4)另一个提示:
In addition to Luigi´s answer.
3) You should think about filtering out the punctuation.
4) Another hint: