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

  • SEARCH
  • Home
  • 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 9301911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:08:13+00:00 2026-06-18T23:08:13+00:00

I have a bunch of strings which contain some specific markers that should be

  • 0

I have a bunch of strings which contain some specific markers that should be replaced with images. So at first I created hash map with markers and images:

Map<String, String> images = new HashMap<String, String>();
images.put(":img:howdy:", "path/images/hello.png");
images.put(":img:code:", "path/images/code.png");
images.put(":img:smile:", "path/images/sm.png");
//...and 70 more records

Strings look like:

This is a string :img:howdy:, you know it :img:smile:

And even more:img:smile:

This is a string with images:img:code::img:smile:

After parsing all markers are planned to be replaced with images.

I stuck at the following point:

Map<String, String> images = new HashMap<String, String>();
images.put(":img:howdy:", "path/images/hello.png");
images.put(":img:code:", "path/images/code.png");
images.put(":img:smile:", "path/images/sm.png");

String[] strings = {"This is a string :img:howdy:, you know it :img:smile:",
                 "And even more:img:smile:",
                 "This is a string with images:img:code::img:smile:"};

for (String text : images.keySet()) {
    for (String string : strings) {
        if(string.contains(text)) {
            string.replace(text, images.get(text));
        }
    }
}

Firstly, markers are replaced, but there’re to much iterations.
Secondly, if I use, for example, StringBuilder, I have a lot of duplications with some markers replaced and some not.

I’m not strong with string parsing and corresponding algorithms (at the moment), so don’t throw stones at me after viewing approach I chose.

Added
Reworked a little as Dariusz Waver suggested and here is what I’ve got:

private final String IMAGE_PATTERN = ":s:\\w+:";

//.......

Pattern p = Pattern.compile(IMAGE_PATTERN);
Matcher m = p.matcher(message);
while(m.find())
{
    String imgPattern = message.substring(m.start(), m.end());
    String imgPath = ImgPaths.images.get(imgPattern);
    
    //If there's no such image in Images Map
    if(imgPattern != null) {
        message = message.replace(imgPattern, imgPath);
        m.reset(message);
    }
}

StringBuilder result = new StringBuilder();
result.append(timestamp).append(" - ").append(sender)
        .append(": ").append(message);

Is there any way to make it more optmized taking into account that there’s a really huge amount of text?

  • 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-18T23:08:14+00:00Added an answer on June 18, 2026 at 11:08 pm

    I’d suggest doing it the other way around: first find all occurences of regex :\w+:\w+, then search for it’s values in the map, and then, if they are found, replace the strings.

    For larger amounts of images and larger amounts of text it will be the better way to do it.

    Your code, however, is pretty clean and if you don’t have any performance issues you can leave it as it is – provided you actually make the fix posted by jlordo:

    for (int i = 0; i < strings.length; i++) {
        if(strings[i].contains(text)) {
            strings[i] = strings[i].replace(text, images.get(text));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have bunch of strings, some of which are fairly long, like so: movie.titles
I have a bunch of strings of the form: AAA.BBB[0].CCC.DDD[5].EEE = 123. Some contain
I have a bunch of strings that are dependent on static dictionaries and each
I have a bunch of strings which may of may not have random symbols
Basically, I have a Map<String, String[] > which contains a bunch of strings with
I have a bunch of files which contain prefix codes in brackets. I'd like
I have a List<> collection which contains a bunch of images. Each object within
I have an mdb which used to contain a bunch of linked tables. These
Suppose you have a String[] that has been created after a .split() which then
I have a list testList that contains a bunch of strings. I would like

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.