使用正则表达式在java中分隔字符串的数组
我有一个String str="[12] word1 word2 (12.4%)"
我需要的是只获取word1 word2
并用下划线替换空格,输出应该是这样的word1_word2
在那之后,我如何使它动态地,比如str may,单词可能会像word1_word2_word3_etc
一样递增
如何使代码尽可能短
你可以在下面搜索框中键入要查询的问题!
我有一个String str="[12] word1 word2 (12.4%)"
我需要的是只获取word1 word2
并用下划线替换空格,输出应该是这样的word1_word2
在那之后,我如何使它动态地,比如str may,单词可能会像word1_word2_word3_etc
一样递增
如何使代码尽可能短
# 1 楼答案
可以使用split()拆分单词。然后使用字符串生成器将它们组合在一起
总体思路如下(可能不是100%正确):