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 8905405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:23:09+00:00 2026-06-15T02:23:09+00:00

I have a string which has certain tokens. Example: Someone e.g. X here is

  • 0

I have a string which has certain “tokens”.
Example:

"Someone e.g. X here is a # and the other i.e. X is not but over is something else like #"  

I also have a list of String e.g. {"John", "doctor", "Jim","engineer"}

What is the best way to do the following:
I want to replace all the # characters with the corresponding element in the list.

I.e. I want to skip X and John and replace Jim from # and engineer for the other #.
I thought to just loop over the string#toCharArray() but I was interested if there is a better way to do this.

Note: The values in the second list match the corresponding tokens. So the first value in the list i.e. John maps to the first occurence of X or # which ever that is.

Example:

Input: "Someone e.g. X here is a # and the other i.e. X is not but the other is something else like # but X is at least X but not #"
{"John", "doctor", "Jim","John", "engineer", "doctor"}
Output:
"Someone e.g. X here is a doctor and the other i.e. X is not but the other is something else like Jim but X is at least X but not doctor"

  • 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-06-15T02:23:11+00:00Added an answer on June 15, 2026 at 2:23 am

    You might be interested to look at MessageFormat that allows something similar to this kind of replacement.

    E.g.

    MessageFormat.format(""
        + "Someone e.g. {0} here is a {1} and the other i.e. {2} " 
        + "is not but over is something else like {3}", 
        new String [] {"John", "doctor", "Jim","engineer"});
    

    Edit:

    If the input string cannot be modified to include the placeholders and also the placeholders have special meaning as you mentioned in your update (i.e. X should be ignored, # should be replaced), then you just have to

    • initialize the counter to 0.
    • create an object of StringBuilder.
    • tokenize the input string on space
    • iterate through each token
      • if it is X, increment the counter, append the token as is to the StringBuilder object.
      • if it is #, then read the value at index counter from the input array and append that to the StringBuilder object.
      • append a space.
    • StringBuilder.toString() and trim to remove the trailing space.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which has two HashSet<String> collections as private members. Other classes
I'm building something which has a countdown to a certain date/time. I have it
I have a string which has the below content String msg = The variables
I have a string which has an xml inside. I want to write this
i have a string which has the following format (3,1,Mayer,Jack). I need all 4
I have a bean of type string[] which has two or more values. I
I have a JSON string (external file) which has an element which can either
I have a Person class which has a String collection of aliases representing additional
I have a table in db2 which has the following fields int xyz; string
I have a model for assessment criteria which has a :label field containing string

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.