简体   繁体   English

Java字符串操作ReplaceAll

[英]Java String Manipulation ReplaceAll

I am new to java but not programming in general. 我是Java的新手,但总体而言不是编程人员。 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." 我一直在尝试理解Java String replaceAll ...特别是我正在从文本文件中读取String ...一个例子是“我在空中跃跃欲试”。

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. 1)我想将“ I”更改为“ A”(其中我不是单词的开头),并将2)U更改为“ O”(其中U是单词的结尾)。 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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