Mar*_*eli -1 java string split
在Java中,如果要通过char或String拆分String,可以通过split方法执行此操作,如下所示:
String[] stringWords = myString.split(" ");
Run Code Online (Sandbox Code Playgroud)
但是,假设我现在想要使用stringWords中的字符串创建一个新的String,使用*它们之间的字符串.有没有for/while指令可以做任何解决方案吗?
这是一个明显的例子:
String myString = "This is how the string should be";
String iWant = "This*is*how*the*string*should*be";
Run Code Online (Sandbox Code Playgroud)
有人让我更清楚为什么我不想只使用replace()函数.我不想仅仅因为字符串数组的内容(我的示例中的数组stringWords)改变了它的内容.
这是一个例子:
String myString = "This is a string i wrote"
String[] stringWords = myString.split(" ");
myAlgorithmFucntion(stringWords);
Run Code Online (Sandbox Code Playgroud)
以下是最终字符串更改的示例:
String iWant = "This*is*something*i*wrote*and*i*don't*want*to*do*it*anymore";
Run Code Online (Sandbox Code Playgroud)
如果您不想使用replace或类似,可以使用Apache Commons StringUtils:
String iWant = StringUtils.join(stringWords, "*");
Run Code Online (Sandbox Code Playgroud)
或者,如果你不想使用Apache下议院,然后按comment通过罗里·亨特,你可以实现自己的如图所示这里.
| 归档时间: |
|
| 查看次数: |
127 次 |
| 最近记录: |