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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:07:30+00:00 2026-05-22T20:07:30+00:00

Greetings, Let’s say you wanted to test a string to see if it’s an

  • 0

Greetings,

Let’s say you wanted to test a string to see if it’s an exact match, or, if it’s a match with an _ and any number of characters appended following the _

Valid match examples:

MyTestString
MyTestString_
MyTestString_1234

If performance was a huge concern, which methods would you investigate? Currently I am doing the following:

if (String.equals(stringToMatch)) {
            // success
        } else {
            if (stringToMatch.contains(stringToMatch + "_")) {
                // success
            }
            // fail
        }

I tried replacing the pattern the String.contains _ with a Java.util.regex.Pattern match on _*, but that performed much worse. Is my solution here ideal or can you think of something more cleaver to improve performance a bit more?

Thanks for any thoughts

  • 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-22T20:07:31+00:00Added an answer on May 22, 2026 at 8:07 pm

    You can do something like

    if(string.startsWith(testString)) {
        int len = testString.length();
        if(string.length() == len || string.charAt(len) == '_')
              // success
    }
    

    I assume you want the testString to appear even if you have a “_”?


    EDIT: On whether to use one long condition or nested if statements, there is no difference in code or performance.

    public static void nestedIf(boolean a, boolean b) {
        if (a) {
            if (b) {
                System.out.println("a && b");
            }
        }
    }
    
    public static void logicalConditionIf(boolean a, boolean b) {
        if (a && b) {
            System.out.println("a && b");
        }
    }
    

    compiles to the same code. If you do javap -c

    public static void nestedIf(boolean, boolean);
      Code:
       0:   iload_0
       1:   ifeq    16
       4:   iload_1
       5:   ifeq    16
       8:   getstatic       #7; //Field java/lang/System.out:Ljava/io/PrintStream;
       11:  ldc     #8; //String a && b
       13:  invokevirtual   #9; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
       16:  return
    
    public static void logicalConditionIf(boolean, boolean);
      Code:
       0:   iload_0
       1:   ifeq    16
       4:   iload_1
       5:   ifeq    16
       8:   getstatic       #7; //Field java/lang/System.out:Ljava/io/PrintStream;
       11:  ldc     #8; //String a && b
       13:  invokevirtual   #9; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
       16:  return
    

    The complied code is identical.

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

Sidebar

Related Questions

Greetings. 1 - Let's say I have about 500 folders of variable size with
Greetings, Basically, I have two vectors of data (let's call it experimental and baseline).
i am trying to pass a string through the XmlHttp method. Let me show
Let's say I was lexing a ruby method definition: def print_greeting(greeting = hi) end
Greetings I have a question regarding the combobox in WPF, let me sketch the
Greetings, It's a general SQL question not tied to any specific implementation but more
I have the following question: Let's assume the code below: ... <h:form id=..> <table>
Greetings, I'm uploading an image to my server using the following code: http://cocoadev.com/index.pl?HTTPFileUploadSample However,
How can i add pass Model-Specific urls to the Template. Let's say, i want
Greetings, I have a class (let's call it Car) that has a property of

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.