简体   繁体   English

用方形胸罩劈开一根弦

[英]Splitting a string with square braquets

Similar asked thread but I'm not looking for the same thing as those already posted.类似的问题,但我不是在寻找与已经发布的相同的东西。

Say I have a String "HELLOWORLDAH".假设我有一个字符串“HELLOWORLDAH”。

And I want it formated that way :我希望它以这种方式格式化:

[HEL] [LOW] [ORL] [DAH]. [HEL] [低] [ORL] [DAH]。

The split() method seems to only work on splitting string which already have delimter. split() 方法似乎只适用于拆分已经有分隔符的字符串。 I'm looking to add delimiters and separate the strings by three.我正在寻找添加分隔符并将字符串分隔为三个。

Anyway to do this?无论如何要做到这一点? I understand Regex somewhat.我有点理解正则表达式。

The splitting already works.分裂已经起作用了。 Now use String.join() :现在使用String.join()

System.out.println("["+
    String.join("] [", "HELLOWORLDAH".split("(?<=\\G.{3})"))
+"]");

and you get "[HEL] [LOW] [ORL] [DAH]" .你会得到"[HEL] [LOW] [ORL] [DAH]"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM