Java 变量替换
Java Variable Substitution
我试图找到一种方法来替换字符串中找到的每个 $#
,其中 $
是文字字符 '$'
,而 #
是一个数字(具有 1 个或多个数字),并将 $#
替换为数组中 #
位置处的字符串值。
这里有一些例子:
- 输入 1(字符串):
hello
输入 2(数组)dave richard danny
结果:hello richard
- 输入 1(字符串):
hi
输入 2(数组)morgan ryan matthew nikoli
结果:hi nikoli
PS: 我刚从C#转回Java,所以忘记了很多东西(除非是语法之类的基础知识)
当前代码:
public static String parse(String command, String[] args) {
String substituted = "";
substituted = command;
return substituted;
}
我正在寻找一个可以用数组中的字符串替换表达式的函数。
一个简单、低效的解决方案是迭代替换数组,寻找 #1
、#2
等:
String[] arr = new String[]{"one","two","three"};
String toReplace = "first second third ";
for (int i =0; i<arr.length;i++){
toReplace = toReplace.replaceAll("\$"+(i+1), arr[i]);
}
System.out.println(toReplace);
输出:
first one second two third three
一种更有效的方法是对输入字符串本身进行一次迭代。这是一个快速而肮脏的版本:
String[] arr = new String[]{"one","two","three"};
String toReplace = "first second third ";
StringBuilder sb = new StringBuilder();
for (int i=0;i<toReplace.length();i++){
if (toReplace.charAt(i)=='#' && i<toReplace.length()-1){
int index = Character.digit(toReplace.charAt(i+1),10);
if (index >0 && index<arr.length){
sb.append(arr[index]);
continue;
}
}
sb.append(toReplace.charAt(i));
}
System.out.println(toReplace);
输出:
first one second two third three
这通常只需 String#replaceAll
即可解决,但由于您有自定义的动态替换字符串,因此您可以使用 Matcher
高效简洁地进行字符串替换。
public static String parse(String command, String... args) {
StringBuffer sb = new StringBuffer();
Matcher m = Pattern.compile("\$(\d+)").matcher(command);
while (m.find()) {
int num = Integer.parseInt(m.group(1));
m.appendReplacement(sb, args[num - 1]);
}
m.appendTail(sb);
return sb.toString();
}
我试图找到一种方法来替换字符串中找到的每个 $#
,其中 $
是文字字符 '$'
,而 #
是一个数字(具有 1 个或多个数字),并将 $#
替换为数组中 #
位置处的字符串值。
这里有一些例子:
- 输入 1(字符串):
hello
输入 2(数组)dave richard danny
结果:hello richard
- 输入 1(字符串):
hi
输入 2(数组)morgan ryan matthew nikoli
结果:hi nikoli
PS: 我刚从C#转回Java,所以忘记了很多东西(除非是语法之类的基础知识)
当前代码:
public static String parse(String command, String[] args) {
String substituted = "";
substituted = command;
return substituted;
}
我正在寻找一个可以用数组中的字符串替换表达式的函数。
一个简单、低效的解决方案是迭代替换数组,寻找 #1
、#2
等:
String[] arr = new String[]{"one","two","three"};
String toReplace = "first second third ";
for (int i =0; i<arr.length;i++){
toReplace = toReplace.replaceAll("\$"+(i+1), arr[i]);
}
System.out.println(toReplace);
输出:
first one second two third three
一种更有效的方法是对输入字符串本身进行一次迭代。这是一个快速而肮脏的版本:
String[] arr = new String[]{"one","two","three"};
String toReplace = "first second third ";
StringBuilder sb = new StringBuilder();
for (int i=0;i<toReplace.length();i++){
if (toReplace.charAt(i)=='#' && i<toReplace.length()-1){
int index = Character.digit(toReplace.charAt(i+1),10);
if (index >0 && index<arr.length){
sb.append(arr[index]);
continue;
}
}
sb.append(toReplace.charAt(i));
}
System.out.println(toReplace);
输出:
first one second two third three
这通常只需 String#replaceAll
即可解决,但由于您有自定义的动态替换字符串,因此您可以使用 Matcher
高效简洁地进行字符串替换。
public static String parse(String command, String... args) {
StringBuffer sb = new StringBuffer();
Matcher m = Pattern.compile("\$(\d+)").matcher(command);
while (m.find()) {
int num = Integer.parseInt(m.group(1));
m.appendReplacement(sb, args[num - 1]);
}
m.appendTail(sb);
return sb.toString();
}