简体   繁体   中英

Format specific section of String

I have a String : a%sb%sc%s . I need to format b before I format a or c , but I'm not sure how or even if I can specify only to format b while keeping the rest of the String unformatted.

In other words, I'm trying to do this:

String.format(foo, "test");

With the outcome:

a%sbtestc%s

Is it possible to manipulate a String like this or should I just use String.replace instead?

A little more detail. The ultimate String will look something like: aA-PARMbB-PARMcC-PARM and then used to fetch some data. a and c are much more dynamic than b , so I'm trying to format b before hand.

So, again. I'm trying to achieve the following:

String.format(foo, "B-PARM");

With the results:

a%sbB-PARMc%s

Then format the rest:

String.format(formattedFoo, "A-PARM", "C-PARM");

You could do your formatting in steps,

String aString = String.format("something %s something else", "a string");
String bString = String.format("...%s...", "test");
String cString = // ....

String completeString = String.format("a%sb%sc%s", aString, bString, cString);

but again, I have to wonder what is going on, and whether this represents an XY Problem , one that is best solved by a completely different approach. Consider giving us the details of the overall problem that you're trying to solve and perhaps not the code tactics that you're using to try to solve it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM