简体   繁体   中英

Java String Manipulation ReplaceAll

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM