Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否可以在替换功能中使用或调节/操作员?_Java_String_Algorithm_Replace - Fatal编程技术网

Java 是否可以在替换功能中使用或调节/操作员?

Java 是否可以在替换功能中使用或调节/操作员?,java,string,algorithm,replace,Java,String,Algorithm,Replace,我有一个简单的Java字符串程序。我需要使用replace()函数。其中,如果给定字符串中出现一些单词,我必须借助或条件替换它们 e.g. String s = "I am a boy"; s = s.replace("I", "something"); //works ok s = s.replace("I | am", "something"); //wants to do it System.out.println(s); 我知道通过编写自己的代码编程是可能的,但我只是想在进入最终方式之

我有一个简单的Java字符串程序。我需要使用replace()函数。其中,如果给定字符串中出现一些单词,我必须借助或条件替换它们

e.g. String s = "I am a boy";
s = s.replace("I", "something"); //works ok
s = s.replace("I | am", "something"); //wants to do it
System.out.println(s);
我知道通过编写自己的代码编程是可能的,但我只是想在进入最终方式之前把问题放在这里


提前感谢。

使用
replaceAll
/
replaceFirst
,它允许您使用regex语句匹配要替换的片段

String result "I am I am a boy".replaceAll("I|am", "something");
// result = "something something something something a boy"


使用
replaceAll
/
replaceFirst
,可以使用regex语句匹配要替换的片段

String result "I am I am a boy".replaceAll("I|am", "something");
// result = "something something something something a boy"


你可以这样做

s = s.replace(/(I|am)/g, "something");

replace函数可以搜索文本或正则表达式,也可以使用正则表达式搜索两个或多个单词。通过这种方式,您可以使用替换字符串中的多个单词。

您可以这样做

s = s.replace(/(I|am)/g, "something");

replace函数可以搜索文本或正则表达式,也可以使用正则表达式搜索两个或多个单词。这样,您可以使用替换字符串中的多个单词。

您可以使用
replaceAll
来使用正则表达式。为什么不执行两次替换呢?谢谢您的评论。Will tryYou可以使用
replaceAll
来使用正则表达式。为什么不执行两次替换呢?谢谢您的评论。我会努力等待答复。将使用更多信息进行编辑。不鼓励只编写代码和“尝试此”答案,因为它们不包含可搜索的内容,并且不解释为什么有人应该“尝试此”。谢谢您的建议谢谢您的回复。将使用更多信息进行编辑。不鼓励只编写代码和“尝试此”答案,因为它们不包含可搜索的内容,并且不解释为什么有人应该“尝试此”。谢谢您的建议谢谢您的回复。我会努力等待答复。将尝试