yf0994 / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Please add rich Strings.format() #1142

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago

Strings.format("htpp?value1={VALUE_2}&value2=${VALUE_1}", new HashMap<String, 
Object>() {
            {
                put("{VALUE_1}", 123);
                put("{VALUE_2}", "321");
            }
        });

should return "htpp?value1=321,value2=123"

Original issue reported on code.google.com by ivan.iva...@gmail.com on 10 Sep 2012 at 9:53

GoogleCodeExporter commented 9 years ago
Why does none of these not suit you?

    @Test
    public void testFormat() {
        int value1 = 123, value2 = 321;
        assertEquals("http?value1=321,value2=123", String.format("http?value1=%2$s,value2=%1$s", value1, value2));
        assertEquals("http?value1=321,value2=123", MessageFormat.format("http?value1={1},value2={0}", value1, value2));
    }

Do you absolutely need named value or is it just needed for inversion?

Original comment by ogregoire on 10 Sep 2012 at 11:37

GoogleCodeExporter commented 9 years ago
The idea is to set key name but not key number in Strings.format()
Because order 1,3,2,5,7 may change and may be mistake if many numbers.

With names key it's look better.

Like in Python http://docs.python.org/library/string.html#format-examples
Python sample:
'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', 
longitude='-115.81W')

Java sample: (maybe you can improve)

Strings.format("'Coordinates: {latitude}, {longitude}'", new HashMap<String, 
Object>() {
            {
                put("longitude", "-115.81W");
                put("latitude", "37.24N");

            }
        });

Original comment by ivan.iva...@gmail.com on 10 Sep 2012 at 1:36

GoogleCodeExporter commented 9 years ago
Okay, so that's really a variable interpolation[1] you want. It is used inside 
Maven and Ant, and Apache Configuration supports it, but doesn't exactly show 
an isolated way to use it as it is rather integrated. It's indeed not really 
easy to find such implementation.

[1] http://en.wikipedia.org/wiki/Variable_interpolation

Original comment by ogregoire on 10 Sep 2012 at 3:02

GoogleCodeExporter commented 9 years ago
So basically what you are looking for is what StrSubstitutor in Apache Commons 
/ Lang does:

http://commons.apache.org/lang/api-3.1/org/apache/commons/lang3/text/StrSubstitu
tor.html

Original comment by SeanPFl...@googlemail.com on 10 Sep 2012 at 3:16

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 8 Apr 2013 at 6:58

GoogleCodeExporter commented 9 years ago
At this time, we don't plan to add any templating tools to Guava.

Original comment by kak@google.com on 22 Aug 2013 at 10:12

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:13

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08