Java String Manipulation ReplaceAll

夙愿已清 提交于 2019-12-12 02:58:18

问题


I am new to java but not programming in general. I've been trying to understand Java String replaceAll...specifically I am reading in Strings from a text file...an example would be "I JUMP UP HIGH IN THE AIR TO GET TO YOU."

1) I want to change "I" to "A" where I is not the beginning of a word, and 2) U to "O" where U is at the end of a word. Any help would be appreciated. (Also, if you can point me to a good tutorial on the topic [I learn best by looking at examples] that would be appreciated)


回答1:


Try this.

String s = "I JUMP UP HIGH IN THE AIR TO GET TO YOU.";
s = s.replaceAll("(?!\\b)I", "A")
     .replaceAll("U\\b", "O");
System.out.println(s);
// -> I JUMP UP HAGH IN THE AAR TO GET TO YOO.


来源:https://stackoverflow.com/questions/35835596/java-string-manipulation-replaceall

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