在.NET(也许只是VB.NET)的转换的String.Format {0},{1},...为确定字符串,例如:在Java中等效的String.Format(.NET)?
Dim St As String = "Test: {0}, {1}"
Console.WriteLine(String.Format(St, "Text1", "Text2"))
我试图在这两个搜索Google和StackOverflows,但它们都返回数字字符串格式。
在.NET(也许只是VB.NET)的转换的String.Format {0},{1},...为确定字符串,例如:在Java中等效的String.Format(.NET)?
Dim St As String = "Test: {0}, {1}"
Console.WriteLine(String.Format(St, "Text1", "Text2"))
我试图在这两个搜索Google和StackOverflows,但它们都返回数字字符串格式。
其他建议当然不错,但更多的是printf
及其更新版本的Java血统。您发布的代码看起来受到MessageFormat
的启发。
String format = "Test: {0}, {1}"
System.out.println(MessageFormat.format(format, "Text1", "Text2"))
我真的不某些关于什么'Return:
语句尽管这样做。
System.out.printf()
System.out.format()
对于其他方法使用:
StringBuilder sb = new StringBuilder();
Formatter f = new Formatter(sb, Locale.US);
f.format("my %s", "string");
它看起来比spinning_plate的方法长很多,它有什么区别? –
这是一样的结果,虽然Formatter我相信可以做更多的事情,但我会用他的方法去,我只是想展示一些其他的东西,以防你正在做不同类型的格式化。 –
String.format("Test: %s, %s",string1,string2)
谢谢,所以是%s,不是{}。 –
使用MessageFormat.format
,您也可以提供格式参数在更换令牌。
message = MessageFormat.format("This is a formatted percentage " +
"{0,number,percent} and a string {1}", varNumber, varText);
System.out.println(message);
message = MessageFormat.format("This is a formatted {0, number,#.##} " +
"and {1, number,#.##} numbers", 25.7575, 75.2525);
System.out.println(message);
或者,可以使用String.format
,但这并不保证位置,例如, String.format("What do you get if you multiply %d by %s?", varNumber, varText);
。
这个答案不完整。这不是直接替代。 .NET中的{0}和{1}可以定义参数的位置。例如,它可能是“测试:{1},{0}”,因此%s,%s会替换错误的位置。 – frankish
相关问题:http://stackoverflow.com/questions/187676/java-equivalents-of-c-sharp-string-format-and-string-join?rq = 1 –
可能的重复[Java等效于。 NET的String.Format](http://stackoverflow.com/questions/3754597/java-equivalent-to-nets-string-format) –