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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:48:14+00:00 2026-05-18T12:48:14+00:00

Given a string as a form of input (parsed from an input file) which

  • 0

Given a string as a form of input (parsed from an input file) which represents a number and a mathematical operator (<, >, <=, >=, !, !=, and a few others), what is a really fast, efficient way to chop off that operator, compare it to a list of valid operators, and then set an “operator” variable to a state (i.e., Enum) representing the identified operator, then return just the number (as a string)?

I’m open to various ideas and implementations. I’ve tried several (about 6-7) myself, and find I’m not really satisfied with the speed. The fastest so far is a For Each loop that walks my list of “valid operators”, and compares that operator’s string representation against the chopped off bit from the numeric string. I determine the amount to chop off by the length of each valid operator in the valid list.

Here’s a code example of the fastest implementation. Assume input like <378 and a valid ops list of <, >, !, or >=79 and a valid ops list of <=, >=:

Friend Function FindMatchingOp(ByVal Haystack As String,
                               ByVal ValidOps() As <OperatorType>) As String
    Dim tmpBit As String, tmpOpName As String, tmpOpLen As Int32

    For Each tmpOp As <OperatorType> In ValidOps
        tmpOpName = tmpOp.Name
        tmpOpLen = tmpOpName.Length
        tmpBit = Strings.Left(Haystack, tmpOpLen)

        If String.Equals(tmpBit, tmpOpName) Then
            <Code to set the correct operator>
            Return Haystack.Remove(0, tmpOpLen)
            Exit For
        End If
    Next

    Return vbNullString
End Function

Not all of the numeric strings I expect to parse will utilize the same math operators (hence the need for the ValidOps variable). Some might only support < and >, others might do <=, >=, and !. This is why I cannot hardcode the assumption that the operator will be only one character in length, and have to test for both one-or-two character operators. I believe it’s these specific string checks that slow my other implementations down.

I’ve also tried putting ValidOps into things like a Dictionary, HashTable, ListDictionary, and even an Arraylist. The standard array beats all of them every time.

Thoughts?

PS, VB code only, please, in any advice or solutions.

EDIT:
I am going to try and implement a Trie to handle this and see what its performance is. I got the idea from this StackOverflow question.
Not going to work for me.

  • 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-18T12:48:15+00:00Added an answer on May 18, 2026 at 12:48 pm

    You could somewhat improve your function by changing:

    tmpOpLen = tmpOpName.Length
    tmpBit = Strings.Left(Haystack, tmpOpLen)
    
    If String.Equals(tmpBit, tmpOpName) Then
        <Code to set the correct operator>
        Return Haystack.Remove(0, tmpOpLen)
        Exit For
    End If
    

    to…

    If Haystack.StartsWith(tmpOp.Name) Then
        <Code to set the correct operator>
        Return Haystack.Remove(0, tmpOp.Name.Length)
        Exit For
    End If
    

    But that is probably going to be marginal. All you’ll have is the removal of all of your intermediate strings.

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

Sidebar

Related Questions

No related questions found

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.