Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.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_Variables_Insert_Include_Quotation Marks - Fatal编程技术网

Java-在字符串中包含变量?

Java-在字符串中包含变量?,java,variables,insert,include,quotation-marks,Java,Variables,Insert,Include,Quotation Marks,好的,我们都应该知道,您可以通过执行以下操作将变量包含到字符串中: String string = "A string " + aVariable; 有没有这样的方法: String string = "A string {aVariable}"; 换句话说:不必关闭引号和添加加号。它很不吸引人。您可以始终使用String.format(..)。i、 e 我不确定这是否对你有足够的吸引力,但它非常方便。语法与printf和java.util.Formatter的语法相同。我经常使用它,尤其是

好的,我们都应该知道,您可以通过执行以下操作将变量包含到字符串中:

String string = "A string " + aVariable;
有没有这样的方法:

String string = "A string {aVariable}";

换句话说:不必关闭引号和添加加号。它很不吸引人。

您可以始终使用String.format(..)。i、 e


我不确定这是否对你有足够的吸引力,但它非常方便。语法与printf和java.util.Formatter的语法相同。我经常使用它,尤其是当我想显示表格数字数据时。

这称为字符串插值;它在Java中并不存在

一种方法是使用String.format:

String string = String.format("A string %s", aVariable);

另一种方法是使用模板库,如OR.

也可以考虑,它使用具有数值参数索引的相关语法。比如说,

String aVariable = "of ponies";
String string = MessageFormat.format("A string {0}.", aVariable);
结果为
字符串
,包含以下内容:

A string of ponies.

更常见的是,该类用于其数字和时间格式。描述了标签格式化的示例;该类设置游戏状态窗格的格式。

您可以使用字符串格式在字符串中包含变量

我使用此代码在字符串中包含2个变量:

String myString=String.format(“这是我的字符串%s%2d”,variable1Name,variable2Name)


自Java15以来,您可以使用名为
string::formatted(Object…args)

例如:

String foo = "foo";
String bar = "bar";

String str = "First %s, then %s".formatted(foo, bar);     
输出:

“先是foo,然后是bar”

可以使用apachecommons

import org.apache.commons.text.StringSubstitutor;
// ...
Map<String, String> values = new HashMap<>();
values.put("animal", "quick brown fox");
values.put("target", "lazy dog");
StringSubstitutor sub = new StringSubstitutor(values);
String result = sub.replace("The ${animal} jumped over the ${target}.");
// "The quick brown fox jumped over the lazy dog."
要使用递归变量替换,请调用
setEnableSubstitution不变量(true)

Map values=newhashmap();
价值。看跌期权(“b”、“c”);
值。放置(“ac”、“测试”);
StringSubstitutor sub=新StringSubstitutor(值);
sub.SetEnableSubstitution不变量(真);
字符串结果=sub.replace(“${a${b}”);
//“测试”

@Chandra请不要问为什么,如果可能的话请解释。这正是我喜欢的方式。谢谢。使用Groovy,您就可以随心所欲地使用“字符串${aVariable}”。中讨论的类似内容有各种各样的技术,但是
string.format()
内置于该语言中。@KalebBrasee听起来很完美,但在修改语言时我总是犹豫不决。我不想让自己落后。@Gray Adams Groovy不会让你落后,它会让你自由!:我知道这是一个意见问题,但我不认为
格式
比简单的字符串串联表达式更有吸引力。
format
是在你需要填充、数字格式化等的时候出现的。@StephenC:我一点也不反对你。但对于字符串格式来说,它是一种有用的替代方法,特别是当需要按照您和我上面所说的那样填充时(即,显示表格数字数据)。我经常使用它来格式化血液化学和CBC结果报告。@StephenC我喜欢这种格式有几个原因:首先,'%d'使用平台线分隔符。其次,您可以很容易地在末尾找到所有变量。您可以轻松地重新格式化甚至重新排序您输入的变量。更容易避免错误(如
1+“oops”
),特别是如果使用FindBugs(它解析格式字符串和输入参数)。而且,正如提问者所说,在许多情况下,它更具可读性。当然,
format
方法是静态的,这是一个相当愚蠢的设计错误。@owlstead-“当然,格式化方法是静态的,这是一个相当愚蠢的设计错误,这是一个遗憾。”。嗯?
格式如何与目标字符串关联?您肯定不是建议我们编写
“字符串%s%2d”。格式(aStringVar,anitvar)
。。。是吗?@owlstead-“asFormat”是不对的。这意味着该方法正在创建并返回一个format对象。对于来自CSharp的人来说,这种方法更简单,因为它类似于C#中的string.format。
import org.apache.commons.text.StringSubstitutor;
// ...
Map<String, String> values = new HashMap<>();
values.put("animal", "quick brown fox");
values.put("target", "lazy dog");
StringSubstitutor sub = new StringSubstitutor(values);
String result = sub.replace("The ${animal} jumped over the ${target}.");
// "The quick brown fox jumped over the lazy dog."
String result = sub.replace("The number is ${undefined.property:-42}.");
// "The number is 42."
Map<String, String> values = new HashMap<>();
values.put("b", "c");
values.put("ac", "Test");
StringSubstitutor sub = new StringSubstitutor(values);
sub.setEnableSubstitutionInVariables(true);
String result = sub.replace("${a${b}}");
// "Test"