I just want to add a space between each character of a string. Can anyone help me figuring out how to do this?
E.g. given "JAYARAM", I need "J A Y A R A M" as the result.
解决方案
Unless you want to loop through the string and do it "manually" you could solve it like this:
yourString.replace("", " ").trim()
This replaces all "empty substrings" with a space, and then trims off the leading / trailing spaces.
An alternative solution using regular expressions:
yourString.replaceAll(".(?=.)", "$0 ")
Basically it says "Replace all characters (except the last one) with with the character itself followed by a space".
Documentation of...
String.replaceAll (including the $0 syntax)
The positive look ahead (i.e., the (?=.) syntax)
本文介绍如何在Java中使用String类的replace和replaceAll方法,以及正则表达式实现,让输入如JAYARAM转换为J A Y A R A M。提供两种高效解决方案,适合处理字符串操作需求。
269

被折叠的 条评论
为什么被折叠?



