Java 中是否有一种方法可以用给定的字符串替换第一次出现的字符串?
Is there a method in Java that will replace the first occurrence of a String with a given String?
我正在尝试在 Java 中编写一个 madlibs 程序。有一种方法采用模板字符串、该字符串中所有占位符的 ArrayList 以及将替换这些模板的所有用户输入的 ArrayList 作为参数。
该方法如下所示:
private String replacePlaceHolder(String template, ArrayList<String> placeholders, ArrayList<String> replacements){
for (int i = 0; i < placeholders.size(); i++){
template = template.replace(placeholders.get(i), replacements.get(i));
}
return template;
}
问题是,该方法是用替换项替换给定模板的所有出现,例如“[Adjective]”,而不仅仅是第一个。我尝试改用 template = template.replaceFirst(placeholders.get(i), replacements.get(i))
,但它用所有用户输入替换了第一个占位符,并忽略了其余部分。
这是我使用的模板:
Computer programming, also known as [-ing Verb], is a
process that leads from a [Adjective] problem
to an executable [Noun].
Programming often involves [-ing Verb],
[-ing Verb], and [-ing Verb], and can be learned
by anyone!
Source code is written in a programming language,
such as [Animal]code, or Java.
The first ever programmer was [Name of Celebrity],
who invented [Plural Noun] in the year [Year].
Since then, programming has become a
[Adjective] practice all across the world.
我知道占位符的 ArrayList 与模板中的占位符匹配,并且 ArrayList 与用户输入的 ArrayList 的长度相同。
我应该怎么做?
replaceFirst
是正确答案,但是参数是正则表达式,所以你需要引用搜索文本,特别是因为它使用 []
,这是一个字符的特殊正则表达式 class.
要引用值,请使用 Pattern.quote(String s)
for the first parameter, and Matcher.quoteReplacement(String s)
作为第二个参数。
private static String replacePlaceHolder(String template, List<String> placeholders, List<String> replacements) {
for (int i = 0; i < placeholders.size(); i++){
template = template.replaceFirst(Pattern.quote(placeholders.get(i)),
Matcher.quoteReplacement(replacements.get(i)));
}
return template;
}
这是一个 Minimal, Reproducible Example,您应该在问题中提供的内容:
String template = "Computer programming, also known as [-ing Verb], is a\n" +
"process that leads from a [Adjective] problem\n" +
"to an executable [Noun].\n" +
"Programming often involves [-ing Verb],\n" +
"[-ing Verb], and [-ing Verb], and can be learned\n" +
"by anyone!\n" +
"Source code is written in a programming language,\n" +
"such as [Animal]code, or Java.\n" +
"The first ever programmer was [Name of Celebrity],\n" +
"who invented [Plural Noun] in the year [Year].\n" +
"Since then, programming has become a\n" +
"[Adjective] practice all across the world.";
List<String> placeholders = Arrays.asList(
"[-ing Verb]", "[Adjective]", "[Noun]",
"[-ing Verb]", "[-ing Verb]", "[-ing Verb]",
"[Animal]", "[Name of Celebrity]", "[Plural Noun]",
"[Year]", "[Adjective]" );
List<String> replacements = Arrays.asList(
"AA", "BB", "CC",
"DD", "EE", "FF",
"GG", "HH", "II",
"JJ", "KK" );
String result = replacePlaceHolder(template, placeholders, replacements);
System.out.println(result);
输出
Computer programming, also known as AA, is a
process that leads from a BB problem
to an executable CC.
Programming often involves DD,
EE, and FF, and can be learned
by anyone!
Source code is written in a programming language,
such as GGcode, or Java.
The first ever programmer was HH,
who invented II in the year JJ.
Since then, programming has become a
KK practice all across the world.
我正在尝试在 Java 中编写一个 madlibs 程序。有一种方法采用模板字符串、该字符串中所有占位符的 ArrayList 以及将替换这些模板的所有用户输入的 ArrayList 作为参数。
该方法如下所示:
private String replacePlaceHolder(String template, ArrayList<String> placeholders, ArrayList<String> replacements){
for (int i = 0; i < placeholders.size(); i++){
template = template.replace(placeholders.get(i), replacements.get(i));
}
return template;
}
问题是,该方法是用替换项替换给定模板的所有出现,例如“[Adjective]”,而不仅仅是第一个。我尝试改用 template = template.replaceFirst(placeholders.get(i), replacements.get(i))
,但它用所有用户输入替换了第一个占位符,并忽略了其余部分。
这是我使用的模板:
Computer programming, also known as [-ing Verb], is a
process that leads from a [Adjective] problem
to an executable [Noun].
Programming often involves [-ing Verb],
[-ing Verb], and [-ing Verb], and can be learned
by anyone!
Source code is written in a programming language,
such as [Animal]code, or Java.
The first ever programmer was [Name of Celebrity],
who invented [Plural Noun] in the year [Year].
Since then, programming has become a
[Adjective] practice all across the world.
我知道占位符的 ArrayList 与模板中的占位符匹配,并且 ArrayList 与用户输入的 ArrayList 的长度相同。
我应该怎么做?
replaceFirst
是正确答案,但是参数是正则表达式,所以你需要引用搜索文本,特别是因为它使用 []
,这是一个字符的特殊正则表达式 class.
要引用值,请使用 Pattern.quote(String s)
for the first parameter, and Matcher.quoteReplacement(String s)
作为第二个参数。
private static String replacePlaceHolder(String template, List<String> placeholders, List<String> replacements) {
for (int i = 0; i < placeholders.size(); i++){
template = template.replaceFirst(Pattern.quote(placeholders.get(i)),
Matcher.quoteReplacement(replacements.get(i)));
}
return template;
}
这是一个 Minimal, Reproducible Example,您应该在问题中提供的内容:
String template = "Computer programming, also known as [-ing Verb], is a\n" +
"process that leads from a [Adjective] problem\n" +
"to an executable [Noun].\n" +
"Programming often involves [-ing Verb],\n" +
"[-ing Verb], and [-ing Verb], and can be learned\n" +
"by anyone!\n" +
"Source code is written in a programming language,\n" +
"such as [Animal]code, or Java.\n" +
"The first ever programmer was [Name of Celebrity],\n" +
"who invented [Plural Noun] in the year [Year].\n" +
"Since then, programming has become a\n" +
"[Adjective] practice all across the world.";
List<String> placeholders = Arrays.asList(
"[-ing Verb]", "[Adjective]", "[Noun]",
"[-ing Verb]", "[-ing Verb]", "[-ing Verb]",
"[Animal]", "[Name of Celebrity]", "[Plural Noun]",
"[Year]", "[Adjective]" );
List<String> replacements = Arrays.asList(
"AA", "BB", "CC",
"DD", "EE", "FF",
"GG", "HH", "II",
"JJ", "KK" );
String result = replacePlaceHolder(template, placeholders, replacements);
System.out.println(result);
输出
Computer programming, also known as AA, is a
process that leads from a BB problem
to an executable CC.
Programming often involves DD,
EE, and FF, and can be learned
by anyone!
Source code is written in a programming language,
such as GGcode, or Java.
The first ever programmer was HH,
who invented II in the year JJ.
Since then, programming has become a
KK practice all across the world.