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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:41:18+00:00 2026-05-16T06:41:18+00:00

I have implemented a very simple method: private String getProfileName(String path) { String testPath

  • 0

I have implemented a very simple method:

private String getProfileName(String path) {
    String testPath = null;
    for (int i = 0; i < path.length(); i++) {
       testPath = path.substring(0, i);
          if ( testPath.endsWith("1") || testPath.endsWith("2") || testPath.endsWith("3") || testPath.endsWith("4") || testPath.endsWith("5") || testPath.endsWith("6") || testPath.endsWith("7") || testPath.endsWith("8") || testPath.endsWith("9") ) {
            break;
          }
    }
    return testPath.substring(0, (testPath.length() - 1));
}

I don’t like the whole method because I think it’s more complicated than necessary, especially the if condition.

So I thought of a way to refactor this method. First I thought of using Regex to replace the if condition, but isn’t regex a little bit too much for this simple case?

Any other ideas how to reafctor this?

  • 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-16T06:41:18+00:00Added an answer on May 16, 2026 at 6:41 am

    Use this pattern with a matcher:

    "^[^1-9]*"
    

    Example code:

    private String getProfileName(String path) {
        Pattern pattern = Pattern.compile("^[^1-9]*");
        Matcher matcher = pattern.matcher(path);
        matcher.find();
        return matcher.group();
    }
    

    I think this is much easier to understand than your code. It took me several minutes to work out your logic, and I had to run it to be sure. I think that with a regular expression it is quite clear what the code is doing. If you wish you can compile the regular expression just once and reuse it by moving it to a static member of the class.

    Regular expressions have quite a bad stigma on Stack Overflow (mostly from people trying to use them to parse HTML, email addresses, URLs, and all sorts of other nasty inappropriate uses of regular expressions). But for this sort of task a regular expression is just fine.

    You may also wish to consider why you are omitting 0 and if that is a good idea.

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

Sidebar

Related Questions

I have a very simple class: People: class People { private string LastName =
Very simple Qt GUI application: On the scene I have multiple circles implemented as
I have implemented a very basic sign up using email address+name, although I would
I have implemented lucene for my application and it works very well unless you
I have implemented a test method with Jersey to run on my Google AppEngine
I have built a variety of little scripts using Ruby's very simple Queue class,
I have implemented a really simple C# Http Server using HttpListener within a windows
In a very short and simple LVL callback function, I have: public void applicationError(ApplicationErrorCode
I'm getting mad with webservices. I have a very simple soap webservice : @Remote
I have a very simple Java RMI Server that looks like the following: import

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.