Java String.Format() specifier 's' - strange behaviour
I have following code:
String requestString=String.format(Constants.SEARCH_SETS_API,
Constants.DEVELOPER_KEY,
"ids:".concat(setId),
"1");
where
public static final String DEVELOPER_KEY = "3ansrfnt10cggo80";
public static final String SEARCH_SETS_API =
"http://api.quizlet.com/1.0/sets?"
+ "dev_key=%1s&"
+ "q=%2s&"
+ "sort=开发者_StackOverflow社区alphabetical&"
+ "whitespace=off&"
+ "page=%3s&"
+ "per_page=50&"
+ "time_format=unix&"
+ "images_only=off&"
+ "updated_since=0&"
+ "extended=on";
Problem: Problem is that there is "\n" is inserted before %3s specifier in case 3rd argument is 1 or 2 character long like "1", "12" etc. If it is 3 characters long and more like "123" etc no carriage return is inserted.
Question: How to get rid of the carriage return before %3s argument in case it is 1 or 2 characters long?
I think you are looking for %1$s
, %2$s
, etc. What you specified in your format string is actually padding the inserted values to the length you’re giving, e.g. 1, 2, and 3 characters.
精彩评论