Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6359989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:35:28+00:00 2026-05-24T23:35:28+00:00

My question is similar to this question , which asks how to substitute properties

  • 0

My question is similar to this question, which asks how to substitute properties into a string such as

Transfer {0} from {1} to {2} on {3}

The answer to that question, the MessageFormat class, won’t work with my needs. I’d like to substitute named parameteres into a string like:

The {weather} in {location} stays mainly in the {terrain}.

or perhaps

The ${weather} in ${location} stays mainly in the ${terrain}.

Am I lucky enough that there already exists a class like MessageFormat to help with that or should I cobble together something on my own to do it? Ant does this with build.xml – but short of lifting their code I’m wondering if there is already an existing class.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:35:29+00:00Added an answer on May 24, 2026 at 11:35 pm

    May I suggest creating a new class called CustomMessageFormat:

    public class CustomMessageFormat
    {
        public static String format( String message, Object[] params )
        {
            Pattern pattern = Pattern.compile( "\\{(.*?)\\}" );
            Matcher matcher = pattern.matcher( message );
            StringBuffer sb = new StringBuffer();
            int i = 0;
            while ( matcher.find() )
            {
                matcher.appendReplacement( sb, "{" + ( i++ ) + "}" );
            }
            matcher.appendTail( sb );
    
            return MessageFormat.format( sb.toString(), params );
        }
    }
    

    That all that does is replace all your {sometext} tokens with sequential ones ({1}, {2} etc.) as required by MessageFormat.format method.

    The you can simply use:

    public static void main( String[] args )
    {
        String inputMessage = "The {def1} in {def2} stays mainly in the {def3}.";
        String result = CustomMessageFormat.format( inputMessage, new Object[] { "sun", "Paris", "suburbs" } );
        System.out.println( result );
    }
    

    This is, of course, a rough example, but I hope you get the idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firstly, I realise that this is a very similar question to this one: Which
i have a table which looks similar to this <asp:TableRow><asp:TableCell>Question 1</asp:TableCell><asp:TableCell ID =Question1Text></asp:TableCell></asp:TableRow> <asp:TableRow><asp:TableCell
I found this on Google, click here , which someone asked a similar question,
This question is similar to this other one , with the difference that the
I asked a similar question about this previously, but I did not specify that
My question is similar to this one , which I've tried the solution to,
I have a problem similar to this question which was previously asked but my
I'll preface this question with the note that I have looked at this similar
My question is similar to this thread which says to lock the iPhone in
While similar to this question which gave me the LINQ for part of my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.