简体   繁体   English

替换Java中的特定字符串

[英]Replacing a specific string in java

I am having problems with replacing a specific string in a user input string but is not generating the expected output. 我在替换用户输入字符串中的特定字符串时遇到问题,但未生成预期的输出。

String inputString="hellol lol";
String result = inputString.replaceAll("lol", "laugh out loud");
System.out.println("Normal Form:" + result);

Input: hellol lol 输入: hellol lol

Code output: hellaugh out loud laugh out loud 代码输出: hellaugh out loud laugh out loud

Expected output: hellol laugh out loud 预期输出: hellol laugh out loud

how do I fix this? 我该如何解决? thanks. 谢谢。

replaceAll() method takes regex to find the matches and replace them. replaceAll()方法使用正则表达式查找匹配项并替换它们。 The appropriate regular expression will do your job. 适当的正则表达式将完成您的工作。 Use \\\\blol\\\\b as regex. 使用\\\\blol\\\\b作为正则表达式。

inputString.replaceAll("\\blol\\b", "laugh out loud");

Here \\\\b is the word boundary that helps to identify the lol token \\\\b是有助于识别lol标记的单词边界

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

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