Is there a formatting flag that converts to a lowercase String in Java?

There are two placeholders in Java that convert to a String:

  • %s -- converts to a String as-is
  • %S -- converts to an Uppercase String.

Thus, given:

String result = String.format(template, "Hi James!");
  • If template = "%s", the result will be "Hi James!"
  • If template = "%S", the result will be "HI JAMES!"

Question:

Generally, is there a way to convert an argument to a lowercase String using only Java's format conversion syntax? (In other words, without using toLowerCase().)Specifically, is there any possible value for template such that the result will be "hi james!"?

Answers


No, there is not. But, according to Java Docs:

Conversions denoted by an upper-case character (i.e. 'B', 'H', 'S', 'C', 'X', 'E', 'G', 'A', and 'T') are the same as those for the corresponding lower-case conversion characters except that the result is converted to upper case according to the rules of the prevailing Locale. The result is equivalent to the following invocation of String.toUpperCase()

In other words, the following

String result = String.format("%S", "Hi James!");

is equivalent to

String result = String.format("%s", "Hi James!").toUpperCase();

So, if you want to get a lower case string, you can just do:

String result = String.format("%s", "Hi James!").toLowerCase();

There won't be an optimization by doing the conversion using a flag.


Need Your Help

How to wait for all goroutines to finish without using time.Sleep?

go synchronization goroutine

This code selects all xml files in the same folder, as the invoked executable and asynchronously applies processing to each result in the callback method (in the example below, just the name of the...

How to create realistic .scn files?

ios scenekit arkit

Looking at the apple sample AR app, there are many realistic looking objects (cup, candle, etc). However working with the scene kit editor on Xcode it is clear that this only allows you to create b...