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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:57:49+00:00 2026-05-15T03:57:49+00:00

What is the most efficient way to split a string by a very simple

  • 0

What is the most efficient way to split a string by a very simple separator?

Some background:

I am porting a function I wrote in C with a bunch of pointer arithmetic to java and it is incredibly slow(After some optimisation still 5* slower).
Having profiled it, it turns out a lot of that overhead is in String.split

The function in question takes a host name or ip address and makes it generic:

123.123.123.123->*.123.123.123

a.b.c.example.com->*.example.com

This can be run over several million items on a regular basis, so performance is an issue.

Edit: the rules for converting are thus:

  • If it’s an ip address, replace the first part
  • Otherwise, find the main domain name, and make the preceding part generic.

foo.bar.com-> *.bar.com
foo.bar.co.uk-> *.bar.co.uk

I have now rewritten using lastIndexOf and substring to work myself in from the back and the performance has improved by leaps and bounds.

I’ll leave the question open for another 24 hours before settling on the best answer for future reference

Here’s what I’ve come up with now(the ip part is an insignificant check before calling this function)

private static String hostConvert(String in) {
    final String [] subs = { "ac", "co", "com", "or", "org", "ne", "net", "ad", "gov", "ed" };

    int dotPos = in.lastIndexOf('.');
    if(dotPos == -1)
        return in;
    int prevDotPos = in.lastIndexOf('.', dotPos-1);
    if(prevDotPos == -1)
        return in;
    CharSequence cs = in.subSequence(prevDotPos+1, dotPos);
    for(String cur : subs) {
        if(cur.contentEquals(cs)) {
            int start = in.lastIndexOf('.', prevDotPos-1);
            if(start == -1 || start == 0)
                return in;
            return "*" + in.substring(start);
        }
    }

    return "*" + in.substring(prevDotPos);
}

If there’s any space for further improvement it would be good to hear.

  • 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-15T03:57:49+00:00Added an answer on May 15, 2026 at 3:57 am

    Something like this is about as fast as you can make it:

    static String starOutFirst(String s) {
        final int K = s.indexOf('.');
        return "*" + s.substring(K);
    }
    static String starOutButLastTwo(String s) {
        final int K = s.lastIndexOf('.', s.lastIndexOf('.') - 1);
        return "*" + s.substring(K);
    }
    

    Then you can do:

        System.out.println(starOutFirst("123.123.123.123"));
        // prints "*.123.123.123"
    
        System.out.println(starOutButLastTwo("a.b.c.example.com"));
        // prints "*.example.com"
    

    You may need to use regex to see which of the two method is applicable for any given string.

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

Sidebar

Related Questions

Trying to get data in the most efficient way possible for some reports, using
What's the simplest, most standard, and/or most efficient way to split a List into
Possible Duplicate: Most memory efficient way to split an NSString in to substrings I'm
What is the shortest and most efficient way to decompose a string array into
Is there an efficient way to split a string containing a list of email
Using jQuery, what's the most efficient way to split a list <ul class=columnar><li></li>... <li></li></ul>
What is the most efficient way to remove accents from a string e.g. ÈâuÑ
What's the most efficient way to convert a Dictionary to a formatted string. e.g.:
What is the most efficient way to get Elastic Map Reduce output into SimpleDB?
What is the most efficient way to sort a list, [0,0,1,0,1,1,0] whose elements are

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.