我在Processing中做了一些工作,基本上是Java。我通常只在Ruby中工作,而且我已經習慣了很多相當優雅和漂亮的代碼約定。Java中的內聯字符串替換?
如果我有一個字符串,我想插入其他字符串,在Java中執行它最美麗的方法是什麼?
在Ruby中,我做這樣的事情一般(其中每個變量是一個字符串):
p "The #{person_title} took a #{mode_of_transit} to the #{holiday_location} for a nice #{verb} in the #{noun}"
這樣看來在Java中,我需要手動將它們連接起來是這樣的:
println("The " + personTitle + " took a " + modeOfTransit + " to the " holidayLocation + for a nice " + verb + " in the " + noun)
這只是感覺不對我。它的工作原理,但它不光滑。有沒有辦法在Java中做到這一點?
您可以使用String.format:http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#format(java.lang.String,java.lang。對象......)我只是覺得很亂。 – eboix 2011-12-26 03:28:16