Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.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
Python字符串';模板&x27;java中的等效_Java_Python_String_Equivalent - Fatal编程技术网

Python字符串';模板&x27;java中的等效

Python字符串';模板&x27;java中的等效,java,python,string,equivalent,Java,Python,String,Equivalent,Python支持以下操作: >>> s = Template('$who likes $what') >>> s.substitute(who='tim', what='kung pao') 'tim likes kung pao' (示例取自Python的官方版本) Java中是否有一种等效的方法来执行确切的任务 谢谢我不知道是否有同等条件,但您可以: String s = "$who likes $what"; s.replace("$who", "ti

Python支持以下操作:

>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'
(示例取自Python的官方版本)

Java中是否有一种等效的方法来执行确切的任务


谢谢

我不知道是否有同等条件,但您可以:

String s = "$who likes $what";
s.replace("$who", "tim").replace("$what", "kung pao");

您将得到相同的结果。

String s=String.format(“%s喜欢%s”、“tim”、“宫保”)

System.out.printf(“%s喜欢%s”、“tim”、“宫保”)

你也可以很容易地用这个做模板

String s = "%s likes %s";
String.format(s, "tim", "kung pao");

我认为您可以使用
String.format
来实现这一点

看一看。以下是一个例子:

ST hello = new ST("Hello, <name>");
hello.add("name", "World");
System.out.println(hello.render());
区块模板()使这类事情变得非常简单:

Chunk c = new Chunk();
c.append("{$who} likes {$what}");
c.set("who", "tim");
c.set("what", "kung pao");
String output = c.toString();
或者如果您已经有了
地图

Chunk c = new Chunk();
c.append("{$who} likes {$what}");
Map<String,String> tagValues = getTagValues();
c.setMultiple(tagValues);
c.render(System.out);
Chunk c=新Chunk();
c、 追加({$who}喜欢{$what});
Map tagValues=getTagValues();
c、 setMultiple(标记值);
c、 渲染(系统输出);

Chunk还可以轻松地从一个文件或一组文件加载模板,并支持循环、分支和演示过滤器。

还有另一个选项。为了方便起见,我会重复一遍

有一个带教室的图书馆。这就是它的工作原理:

 // Build map
 Map<String, String> valuesMap = new HashMap<>();
 valuesMap.put("animal", "quick brown fox");
 valuesMap.put("target", "lazy dog");
 String templateString = "The ${animal} jumped over the ${target}.";

 // Build StringSubstitutor
 StringSubstitutor sub = new StringSubstitutor(valuesMap);

 // Replace
 String resolvedString = sub.replace(templateString);
//构建映射
映射值Map=newhashmap();
价值Map.put(“动物”、“快速棕色狐狸”);
价值映射放置(“目标”、“懒狗”);
String templateString=“该${animal}跳过了${target}。”;
//构建替换器
StringSubstitutor sub=新StringSubstitutor(值Map);
//替换
String resolvedString=sub.replace(templateString);
还有一句话
StringSubstitutor
实例使用替换映射创建,然后使用其
replace
方法解析模板字符串。这意味着它无法预解析模板字符串,因此使用不同的替换映射处理同一模板可能效率较低

Python的工作方式正好相反。它使用模板字符串创建,然后使用其
substitute
safe\u substitute
方法处理替换映射。因此,理论上,它可以预解析模板字符串,从而提高性能

此外,默认情况下,Python将处理以太
$variable
${variable}
。到目前为止,找不到如何调整
StringSubstitutor
以实现此目的


默认情况下,
StringSubstitutor
解析值中可能导致无限循环的占位符<代码>stringSubstitutor.setDisableSubstitutionInValues(true)
将禁用此行为。

这对我没有帮助,因为我想在代码的一部分启动模板,然后在其他地方进行替换。太好了,谢谢。我想这就是我想要的。
String.format
更像是Python自己的字符串格式(使用
%
或类似的
“String.format()
)。
Template
的基本功能是能够替换自定义命名区域。它允许基于条件的替换吗?是的,如果支持elseif-else块,则完整:它允许基于条件的模板吗?@gaurav你的意思是这样的:无法访问该链接,但使用
 // Build map
 Map<String, String> valuesMap = new HashMap<>();
 valuesMap.put("animal", "quick brown fox");
 valuesMap.put("target", "lazy dog");
 String templateString = "The ${animal} jumped over the ${target}.";

 // Build StringSubstitutor
 StringSubstitutor sub = new StringSubstitutor(valuesMap);

 // Replace
 String resolvedString = sub.replace(templateString);