如何在逗号处分割字符串但保留空格? [英] How to split string at comma but keep white spaces?
本文介绍了如何在逗号处分割字符串但保留空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在逗号处分割字符串,以便保留所有空格.示例:
How to split string at comma so that it keeps all spaces. Example:
Input: [" hello , world "]
需要将其分隔成数组,使其看起来像这样:
Need to separate this in array so that it looks like this:
Array[0] = " hello "
Array[1] = " world "
并在连接它们之后:
Output: " hello world "
尝试使用如下拆分:input.split("\\s*,\\s*"))
,但随后我将其分离而没有空格...
Tried using split like this: input.split("\\s*,\\s*"))
but then I get it separated without white space...
Array[0] = "hello"
Array[1] = "world"
任何想法如何做到这一点?
Any ideas how to do this?
推荐答案
String s = " hello , world ";
String s1[] = s.split(",");
System.out.println(s1[0]+s1[1]);
output: hello world
这篇关于如何在逗号处分割字符串但保留空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文