我有一个问题,我无法解决。
我有一个段落,其中包含一些关键词,需要用新的值来替换,这些值存储在一个数组中。
例子。
段落:"我最喜欢的水果是[0],但我也喜欢[1]和[3] "我最喜欢的水果是[0],但我也喜欢[1]和[3]"
阵列:"水果"=["香蕉"、"橙子"、"苹果"、"葡萄"]。 水果 = ["香蕉"、"橙子"、"苹果"、"葡萄"]
我的期望是。My most favorite fruit is Banana, but I also like Orange and Grape
你能不能帮我找到一个解决方案?
我试着把我的句子转换成字符串阵列,就像这样。
["My most favorite fruit is ","[0]",", but I also like ","[1]"," and ","[3]"]
然后,我替换掉 [0]
到 0
,我得到了这个。
["My most favorite fruit is ","0",", but I also like ","1"," and ","3"]
我倾向于更换 0
, 1
和 3
的值,并将其转化为上述数组中的 fruits[0]
, fruits[1]
, fruits[3]
然后将该数组转换为一个完整的字符串
但我认为这不是最好的解决方案,因为如果我得到一个这样的输入句。"2[2]"
那么我将收到的输出是 苹果苹果而期望值是 2苹果
Java的 字符串格式化 有一个内置的语法。一般的格式是。
%[argument_index$][flags][width][.precision]conversion
所以你可以用e. g. %1$s
来表示第一个格式参数。%2s
第二种等。注意,指数是基于一的,而不是基于零的。
如
String[] fruits = {"Banana", "Orange", "Apple", "Grape"};
System.out.format(
"My most favorite fruit is %1$s, but I also like %2$s and %4$s",
fruits);
使用String.replace
String sentence = "My most favorite fruit is [0], but I also like [1] and [3]";
String[] replacements = {"Banana", "Orange", "Apple", "Grape"};
for(int i = 0; i < replacements.length; i++)
sentence = sentence.replace("[" + i + "]", replacements[i]);
如果您能够改变 paragraph
那么你可以从这个片段开始。
String paragraph = "My most favorite fruit is %s, but I also like %s and %s";
String[] fruits = {"Banana", "Orange", "Apple", "Grape"};
System.out.printf(paragraph, fruits[0], fruits[1], fruits[2]);
输出
My most favorite fruit is Banana, but I also like Orange and Apple
编辑 另一种解决方案是不必保持水果的位置参数。
String paragraph = "My most favorite fruit is {0}, but I also like {1} and {3}";
Object[] fruits = {"Banana", "Orange", "Apple", "Grape"};
MessageFormat mf = new MessageFormat(paragraph);
System.out.println(mf.format(fruits));
输出
My most favorite fruit is Banana, but I also like Orange and Grape
您可以使用以下代码(见 演示):
ArrayList<String> fruits_arr = new ArrayList<String>(); // Just initializing the array
fruits_arr.add("Banana");
fruits_arr.add("Orange");
fruits_arr.add("Apple");
fruits_arr.add("Grape");
String[] fruits = fruits_arr.toArray(new String[0]);
String s = "My most favorite fruit is [0], but I also like [1] and [3]";
StringBuffer result = new StringBuffer();
Matcher m = Pattern.compile("\\[(\\d+)\\]").matcher(s); // Initializing Matcher
while (m.find()) { // Iterate over matches
int num = Integer.parseInt(m.group(1)); // If there is a match, Group 1 has digits
String replacement = "";
if (num < fruits.length) { // If the number is lower than fruit element count
replacement =fruits[num]; // Get the array element
} else {
replacement = m.group(); // Else, use the whole match (e.g. [9])
}
m.appendReplacement(result, replacement); // Append this replacement
}
m.appendTail(result);
System.out.println(result.toString());
有了 \[(\d+)]
匹配任何带有 [
+digits
+]
,并将数字序列捕捉到组1中。
您可以使用以下方法。
String str = "My most favorite fruit is [0], but I also like [1] and [3]";
String[] fruits = { "Banana", "Orange", "Apple", "Grape" };
for (int i = 0; i < fruits.length; i++)
{
str = str.replaceAll("\\[" + i + "\\]", fruits[i]);
}