[英]Replacing Variables with Data in Java
I am getting a String, for ex.: "The user << a1 >> is << a2 >> 50 years old." 我得到一个String,例如:“用户<< a1 >>的年龄为<< a2 >> 50岁。”
And I also have an array with the data that needs to go in the String! 而且我还有一个数组,其中包含需要放入String的数据! For ex.
对于前。 a[0]= "John" ;
a [0] =“约翰”; a[1]= "30"
a [1] =“ 30
So, for this example, I would like to replace << a1 >>
with John
and << a2 >>
with 30. 因此,对于此示例,我想用
John
替换<< a1 >>
,并用30替换<< a2 >>
。
The only thing I was able to find was the following question: How to replace a set of tokens in a Java String? 我唯一能找到的是以下问题: 如何替换Java字符串中的一组标记? , but to be honest I did not understand anything and wasn't sure if that's what I'm really looking for.
,但老实说,我什么都不懂,也不确定那不是我真正想要的。
So, is that really what I need to work with? 那么,这真的是我需要处理的吗? If it is, I'll go read some tutorials.
如果是这样,我将去阅读一些教程。
Thanks in advance. 提前致谢。
EDIT: NOTE : I don't have control over the String coming in though. 编辑: 注意 :虽然我无法控制传入的字符串。 So it will be exactly the way I typed it.
所以这正是我键入的方式。 All variables are in the form
<< a0 >>
and the number of variable is unknown (there might even be 10 variables). 所有变量的形式均为
<< a0 >>
并且变量的数量未知(甚至可能有10个变量)。
String.format("The user %s is %s 50 years old.", a[0],a[1]);
If you must use <<a1>>
and <<a2>>
then something like this instead... 如果必须使用
<<a1>>
和<<a2>>
则应使用类似...
String s = "The user <<a1>> is <<a2>> years old.";
String output = s.replace("<<a1>>", a[0]).replace("<<a2>>", a[1]);
If you don't have control over the string, you can replace instances of << a# >>
as follows: 如果您无法控制字符串,则可以按以下方式替换
<< a# >>
实例:
String yourString = "The user << a1 >> is << a2 >> years old.";
String transformedString = yourString.replaceAll("<< a\d >>", "%s");
This will replace any instance of << a# >>
with %s
. 这将用
%s
替换任何<< a# >>
实例。 That will change your "The user << a1 >> is << a2 >> years old."
这将更改您
"The user << a1 >> is << a2 >> years old."
string to "The user %s is %s years old"
which can be used with the string formatter. 字符串
"The user %s is %s years old"
可用于字符串格式化程序"The user %s is %s years old"
。
Then use the string formatter as follows. 然后按如下所示使用字符串格式化程序。
String finalString = String.format(transformedString, a);
for(int i = 0; str.contains("<< a"+i+" >>"), i++) {
str = str.replace("<< a"+i+" >>", a[i]);
}
There's nothing stopping you from changing the format of your input string, before processing it further. 在进一步处理之前,没有什么可以阻止您更改输入字符串的格式。 It would give you more options.
它会给您更多选择。
That said, this uses a "reluctant" regex to replace all "gaps" as you require. 就是说,这使用“不情愿”正则表达式来替换您需要的所有“空白”。 It assumes that there's exactly one character before the index number, but the index can be multiple digits.
它假定索引号之前只有一个字符,但是索引可以是多个数字。
import java.util.regex.Pattern;
import java.util.regex.Matcher;
/**
<P>{@code java FillGaps}</P>
**/
public class FillGaps {
public static final void main(String[] ignored) {
String sRegex = "<< (.+?) >>";
String sToSearch = "The user << a1 >> is << a2 >> years old.";
String[] values = new String[] {"John", "30"};
Matcher m = Pattern.compile(sRegex).matcher(sToSearch);
StringBuffer sb = new StringBuffer();
while(m.find()) {
String gapName = m.group(1);
String idxPlus1AsStr = gapName.substring(1, gapName.length());
int arrayIdx = Integer.parseInt(idxPlus1AsStr) - 1;
m.appendReplacement(sb, values[arrayIdx]);
}
m.appendTail(sb);
System.out.println(sb);
}
}
Output: 输出:
[C:\java_code\]java FillGaps
The user John is 30 years old.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.