java中分割字符串并删除每隔一个空格

发布于 2024-12-16 10:50:41 字数 411 浏览 4 评论 0原文

我有原始文件中的日志数据。看起来像这样:

"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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

裸钻 2024-12-23 10:50:41

您可以添加反斜杠 n“\n”以向字符串添加换行符。

我不确定这是否是最好的方法,但这是一种方法:
如果您知道在哪里需要换行符,您可以对字符串进行子串并添加反斜杠 n。类似这样的:

String s = as.substring(0,4) + "\n" + as.substring(4,18) + "\n" + //....and so on

希望有帮助

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:

String s = as.substring(0,4) + "\n" + as.substring(4,18) + "\n" + //....and so on

Hope it helps

╰◇生如夏花灿烂 2024-12-23 10:50:41

这将完成你的工作。如果您有很多像上面一行那样的数据,我们将不得不找出一些其他逻辑。我认为它不适合数百万条记录。

String asd = "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";
asd = asd.replaceAll("      ", "\n");
asd= asd.replaceAll(" ", "");
System.out.println(asd);

请注意,"USERN 1""FTY 0 0 0 0 0 0 D 9" 之间的空格具有最小同时空格数。因此,我使用了该数量的空格来插入新行字符。
如果字符之间的空格大于此 <6 space> 字符串的两倍,逻辑将导致错误的输出。即:将连续出现<12空格>
要解决这个问题,您可以在打印之前插入此代码。但仅当此类数据发生时才使用它。

asd= asd.replaceAll("\n\n", "\n");

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.

String asd = "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";
asd = asd.replaceAll("      ", "\n");
asd= asd.replaceAll(" ", "");
System.out.println(asd);

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.

asd= asd.replaceAll("\n\n", "\n");
心头的小情儿 2024-12-23 10:50:41

您是否尝试过:

String[] split = s.split("\\s+");

您可能需要调整reg-exp。

Have you tried:

String[] split = s.split("\\s+");

You may need to tweak the reg-exp.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文