开发者

Java equivalent of ruby's "Some sentence that I need a dynamic #{value}."

Do I have to formulate all of my strings in java like "Some static words" + variable + " some more static words." or is there a way to do something like the way ruby allows you to evaluate a section in a string: "Some sta开发者_开发技巧tic words with a #{dynamic_value} inserted into the statement."

I haven't found much in the way of googling, this would really speed up formatting strings and make them much more viewable.


I believe what you are looking for is the String.format() method.

String.format("Your static string with %s some variables %d", "string", 3445);

The general idea is that you put your static string first with special place holder formatting. ("%s") Then you pass in the variables to fill into those placeholders.


I don`t know Ruby, but I think that this is what you want:

String text = MessageFormat.format("This is {0} a dynamic value", dynamicValue);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜