JAVA字符串的写法规范
JAVA字符串的写法规范
//错误写法
String.format("First {0} and then {1}", "foo", "bar")
String.format("Too many arguments %d and %d", 1, 2, 3);
String.format("Display %3$d and then %d", 1, 2, 3);
String.format("First Line
");
String.format("Is myObject null ? %b", myObject);
String.format("value is " + value);
String s = String.format("string without arguments");
MessageFormat.format("Result {0}.", value);
MessageFormat.format("Result {0}.", value, value);
MessageFormat.format("Result {0}.", myObject.toString());
logger.log(java.util.logging.Level.SEVERE, "Result {0}.", myObject.toString());
logger.log(java.util.logging.Level.SEVERE, "Result.", new Exception());
logger.log(java.util.logging.Level.SEVERE, "Result {0}", 14);
slf4jLog.debug(marker, "message {}");
slf4jLog.debug(marker, "message ", 1);
正确写法,与前面错误写法对照
String.format(“First %s and then %s”, “foo”, “bar”); String.format(“Display %2$d and then %d”, 1, 3); String.format(“Too many arguments %d %d”, 1, 2); String.format(“First Line%n”); String.format(“Is myObject null ? %b”, myObject == null); String.format(“value is %d”, value); String s = “string without arguments”; MessageFormat.format(“Result {0}.”, value); MessageFormat.format(“Result ‘{0}’ = {0}”, value); MessageFormat.format(“Result {0}.”, myObject); java.util.Logger logger; logger.log(java.util.logging.Level.SEVERE, “Result {0}.”, myObject); logger.log(java.util.logging.Level.SEVERE, “Result {0}’”, 14); org.slf4j.Logger slf4jLog; org.slf4j.Marker marker; slf4jLog.debug(marker, “message {}”); slf4jLog.debug(marker, “message {}”, 1);
