java中分割字符串并删除每隔一个空格
我有原始文件中的日志数据。看起来像这样:
"2 A U W 2 0 1 1 1 1 1 6 0 0 1 0 0 0 U S E R N 1 F T Y 0 0 0 0 0 0 D 9 2 A I L 2 0 1 1 1 1 1 6"
等等。
我需要在相同长度的消息上对该字符串进行子串化(从“2 AU W”到“4 AI L”400 字节)。这是由 String as = s.substring(0,400).toLowerCase()
创建的。但这对我来说还不够。我想传达这样的信息:
2auw
20011111600100
usern1
fty000000d9
有人可以帮忙吗?
I have log data in a raw file. It looks like this:
"2 A U W 2 0 1 1 1 1 1 6 0 0 1 0 0 0 U S E R N 1 F T Y 0 0 0 0 0 0 D 9 2 A I L 2 0 1 1 1 1 1 6"
and so on.
I need to substring this string on messages of equal length (From "2 A U W" to "4 A I L" 400 bytes). This is made by String as = s.substring(0,400).toLowerCase()
. But this is not enough for me. I want to make a message like this:
2auw
20011111600100
usern1
fty000000d9
Can anyone help with it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以添加反斜杠 n“\n”以向字符串添加换行符。
我不确定这是否是最好的方法,但这是一种方法:
如果您知道在哪里需要换行符,您可以对字符串进行子串并添加反斜杠 n。类似这样的:
希望有帮助
You can add a back slash n "\n" to add line breaks to your string.
I'm not sure if this is the best way, but it is one way to do it:
If you know where you need the line breaks you can substring and add the back slash n. Something like this:
Hope it helps
这将完成你的工作。如果您有很多像上面一行那样的数据,我们将不得不找出一些其他逻辑。我认为它不适合数百万条记录。
请注意,
"USERN 1"
和"FTY 0 0 0 0 0 0 D 9"
之间的空格具有最小同时空格数。因此,我使用了该数量的空格来插入新行字符。如果字符之间的空格大于此
<6 space>
字符串的两倍,逻辑将导致错误的输出。即:将连续出现<12空格>
。要解决这个问题,您可以在打印之前插入此代码。但仅当此类数据发生时才使用它。
This will get your work done. If you are having a lot of data like the above line, we will have to figure some other logic. I don't think it will be apt for millions of records.
Please note the space between
"U S E R N 1"
and"F T Y 0 0 0 0 0 0 D 9"
is having minumum number of simultaneous spaces. So, I have used that number of spaces to insert new line character.The logic will result in wrong output if space between characters is greater than two times this
<6 space>
string. ie: there will be<12 spaces>
continuously.To solve that you can insert this code just before printing. But use it only if such data happens.
您是否尝试过:
您可能需要调整reg-exp。
Have you tried:
You may need to tweak the reg-exp.