Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.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 - Fatal编程技术网

Java从基于模板的字符串中获取单词

Java从基于模板的字符串中获取单词,java,Java,我希望能够做到以下几点 String template = "My name is {name}!"; String nameGet = "My name is john!"; System.out.printIn(GetWordFromTemplate(template, nameGet, "name")); //Should print john. 但我不知道从哪里开始。经过一些头脑风暴后,我想出了这个办法,但它不适用于超过1名的占位者 public String GetWordFrom

我希望能够做到以下几点

String template = "My name is {name}!";
String nameGet = "My name is john!";

System.out.printIn(GetWordFromTemplate(template, nameGet, "name")); //Should print john.

但我不知道从哪里开始。

经过一些头脑风暴后,我想出了这个办法,但它不适用于超过1名的占位者

public String GetWordFromTemplate(String template, String input, String placeholder)
{
    if(!template.contains(placeholder)){return "";}
    String temp = input.substring(template.indexOf(placeholder));
    return temp.substring(0, temp.length() - template.substring(template.indexOf(placeholder) + placeholder.length()).length());
)

您的方法具有模板的名称,
“name”
。因此,您只需使用
nameGet
搜索并替换
“{name}”
。你做过什么调查吗?搜索“java搜索并替换字符串”或类似内容。投票被否决,因为没有研究/尝试,请查看,谢谢。