Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please add rich Strings.format() #1142

Closed
gissuebot opened this issue Oct 31, 2014 · 6 comments
Closed

Please add rich Strings.format() #1142

gissuebot opened this issue Oct 31, 2014 · 6 comments
Labels

Comments

@gissuebot
Copy link

Original issue created by ivan.ivanenko on 2012-09-10 at 09:53 AM


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"

@gissuebot
Copy link
Author

Original comment posted by ogregoire on 2012-09-10 at 11:37 AM


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?

@gissuebot
Copy link
Author

Original comment posted by ivan.ivanenko on 2012-09-10 at 01:36 PM


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");

        }
    });

@gissuebot
Copy link
Author

Original comment posted by ogregoire on 2012-09-10 at 03:02 PM


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

@gissuebot
Copy link
Author

Original comment posted by SeanPFloyd on 2012-09-10 at 03:16 PM


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/StrSubstitutor.html

@gissuebot
Copy link
Author

Original comment posted by kevinb@google.com on 2013-04-08 at 06:58 PM


(No comment entered for this change.)


Labels: Package-Base

@gissuebot
Copy link
Author

Original comment posted by kak@google.com on 2013-08-22 at 10:12 PM


At this time, we don't plan to add any templating tools to Guava.


Status: WontFix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant