Add quotes around each word

痞子三分冷 提交于 2021-02-06 12:42:49

问题


I want to transform in Java:

dd fdas dd fdas fads f das fdasf + - || dasf
into:
"dd" "fdas" "dd" "fdas" "fads" "f" "das" "fdasf" + - || "dasf"

basically I want to add quotes around words. \w* -> "\w*\"


回答1:


replaceAll can do this:

String result = input.replaceAll("(\w+)", "\"$1\"");



回答2:


It's fairly simple. Use preg_replace and sorround all text captures with quotes.

preg_replace("/([a-z0-9]+)/i", '"$1"', $string);

You will have to find the replacement of preg_replace for java :)



来源:https://stackoverflow.com/questions/3838741/add-quotes-around-each-word

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!