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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:58:56+00:00 2026-06-13T09:58:56+00:00

I am new to java and I am trying to learn about optimizing code

  • 0

I am new to java and I am trying to learn about optimizing code to make it production ready. I have the following code below. I would like to know how I could optimize it.I thought by using a small snippet of could, it would be a good way to learn.

Some points:
1. The function would be run many times to it needs to be fast.
2. The input is unconstrained since it might come from a user or from a file. Is there a way to deal with this so not exceptions are thrown? I was thinking of using a regular expression.
3. Is there anything else I need to do to make it production ready? e.g. unit tests. If so, what would be the best way to do this?
4. I am assuming that the string to be searched is not very long.
5. When I say optimization, I mean doing things like replacing the ‘+’ operator with something faster is it can effect memory and performance, etc.

public String strReplave(String originalStr, String oldStr, String newStr) {
    int start = 0;
    while ((start = originalStr.indexOf(oldStr, start)) > 0) {
        originalStr= originalStr.substring(0,start) + newStr+ originalStr.substring(start + oldStr.length());
    start += newStr.length();
}
return originalStr;
}

Thanks for your help and if you need me to clarify anything please let me know.

  • 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-13T09:58:57+00:00Added an answer on June 13, 2026 at 9:58 am
    public static String replace(String originalStr, String oldStr, String newStr) {
        int p = originalStr.indexOf(oldStr);
        if (p == -1) {
            return originalStr;
        } else {
            StringBuilder result = new StringBuilder();
            result.append(originalStr, 0, p);
            result.append(newStr);
            int q = p + oldStr.length();
            while ((p = originalStr.indexOf(oldStr, q)) != -1) {
                result.append(originalStr, q, p);
                result.append(newStr);
                q = p + oldStr.length();
            }
            result.append(originalStr, q, originalStr.length());
            return result.toString();
        }
    }
    
    • Use StringBuilder to concat Strings.
    • Optimization if search string is not found. You can skip this, if this doesn’t happen freuently.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now i am trying to learn more about java threading, and i have
I have a simple question about my code. I'm quite new to java and
I am new to Java and trying to learn more about it. I studied
I am trying to learn a java-based program, but I am pretty new to
I'm trying to learn about java's event handlers and keep getting errors with type
Just playing around with java trying to learn it etc. Here is my code
I am trying to learn some more about vectors and rotation in Java by
I'm trying to learn servlets and JSP and would like to do this with
I am new to Java and trying to learn the various collections that programmers
I am new to android/java and currently am trying to learn custom events and

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.