当项目本身用空格分隔时,如何分隔由空格组成的文件中的项目?
我有一个来自批发商的很长的价格文件,我很难将其读入我的程序,因为每一列都用 x 个空格分隔。像这样;
99995116273 34 mm asasa 00472,50100
99998375442 11 lalaaasdsddfgdfgdf 00503,00206
99998375443 1 1/4 Microkupling 00867,00206
如何使用 Java 中的 Scanner 类将每一列分成零件号、描述和价格?
I have a very long price file from my wholesaler that I have som dificulties to read into my program because each column is seperated with x number of white spaces. Like this;
99995116273 34 mm asasa 00472,50100
99998375442 11 lalaaasdsddfgdfgdf 00503,00206
99998375443 1 1/4 Microkupling 00867,00206
How can I use the Scanner class in Java to sperate each column into Part no, Description and Price ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 split 方法。此方法采用正则表达式作为参数,因此这样的东西应该适合您:
每次找到两个或多个空格时都会创建一个新字符串(空格将被丢弃)。结果将是一个包含您需要的单词的数组。
{2,}
意味着要破坏字符串,它需要有两个或更多空格。Use the split method. This method takes a regular expression as a parameter, so somthing like this should work for you:
This will create a new string each time it finds two or more spaces (the spaces will be discarded). The result will be an array containing the words you need.
The
{2,}
implies that for the string to be broken, it will need to have two or more spaces.早上好,我不是一名java开发人员,但您是否尝试过将值分隔符视为空格,而将其视为“制表符”?我过去处理过制表符分隔的文件,这里可能就是这种情况。
Good morning, I am not a java developer by trade but instead of thinking about the value delimiter as a space, have you tried thinking about it as a "tab"? I have dealt with tab demlimeted files in the past and this could be the case here.
假设每行有 1 个项目,您可以使用以下内容:
因此 s.next 将检索包含项目的字符串,然后单独扫描每一行或简单地拆分它。
Assuming that there's 1 item per line you can use the following:
So s.next will retrieve a string containing an item and then scan each line individually or simply split it.
查看粘贴的文本,原始文本似乎使用制表符来对齐列。如果您正在处理的文本包含制表符,并且字段(项目)本身不包含空格,那么您可以再使用一个
tab
字符作为分隔符。如果制表符已经转换为空格并且结果是上面的输出,那么这将成为一个更加困难的问题,并且只能通过启发式解决。
再次查看文本,格式似乎是
如果您可以确认这是格式,那么解决方案不是非常复杂 实施。
Looking at the pasted text it seems the original text is using
tab
characters to align columns. If the text you are processing has the tabs and the fields (items) themselves do not contain spaces then you can use the one moretab
character as the delimiter.If the tab characters have already been converted to spaces and the result is the above output then this becomes a much more difficult problem and can be solved only heuristically.
Again looking at the text, the fornat seems to be
If you can confirm this is the format, then the solution is not very complex to implement.
与其拆分字符串,为什么不从字符串的开头读取零件号,从末尾读取价格,而中间剩下的就是描述。
Instead of splitting the string why not read the part number from the beginning of the string, the price from the end and what is left in the middle is the description.