我有一个字符串与多个空间是不必要的,我想删除该空间,需要一个良好的结构输出。我需要在单词之间有一个空格。 INPUT:有数量后空格数,之前的第一句话。5新的生产线,与@line有一些空间[java]删除空间空间
1
An item one
area city, country
type of area
time of event 1
2
An item one
area city, country
type of area
time of event 1
3 ... idem
`
,我想什么是结构只喜欢:
An item one; area city, country; type of area; time of event 1;
An item two; area city, country; type of area; time of event 2; . . .
我试着用 st.replaceAll("\n", "")
但它不是我期待.. 需要帮助..
更新部分解决和已经存在的另一个问题
问题1
s = s.replaceAll("\\n+", " "); s = s.trim(); s = s.replaceAll(" \\s+", ";");
输出:
1 An item one; area city, country; type of area; time of event 1 2 An item one; area;city, country; type of area; time of event 1
而且现在第二个问题是存在的,例如当有一个词
area city
,结果给area;city
,所以帮我这个存在超过1个空间...
会推荐使用分隔符 – KRUKUSA
失败,您的结果不符合您的原始问题。看到我的答案正确的方式。 –
如果单词之间有多个空格怎么办?修剪不会删除。修剪只从右端和左端删除 – tgkprog