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 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

Ask A Question

Stats

  • Questions 506k
  • Answers 506k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The System.ServiceModel.dll has some items in the System.ServiceModel.Activation namespace. However,… May 16, 2026 at 3:47 pm
  • Editorial Team
    Editorial Team added an answer S(ii) is considered to be a matrix with well defined… May 16, 2026 at 3:47 pm
  • Editorial Team
    Editorial Team added an answer The best way to test for this kind of emptiness… May 16, 2026 at 3:47 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a very simple Java RMI Server that looks like the following: import
I have built a variety of little scripts using Ruby's very simple Queue class,
I have a very simple interface which needs to communicate between processes. It's currently
I have a very simple subclass of UIViewController : @interface RoastChickenViewController : UIViewController {
I have had a good trawl through related questions and implemented suggestions found, but
I am trying to implement some very simple Data Binding in my User Control
I am attempting to implement a very simple Trie in Java that supports 3
this seems like very simple maths but somehow, my brain cant think ... i
Been using a Copy method with this code in it in various places in
I suppose that this is an interesting code example. We have a class --

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.