C# - Which is faster: String.Contains() or Regex.isMatch()? [duplicate] - c#

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Regex.IsMatch vs string.Contains
Which is faster, preferable and why?
What the difference in mechanisms between two?
I need to search for some values from UserAgent, most of values can be used without wildcards (e.g. if I want to catch cellular phones I search for iPhone instead of *iPhone* wildcards).

What is faster
Try measuring. But this is the wrong question, see below.
preferable
If I want to match a fixed string String.Contains does just what I need. If I need to pattern match, then String.Contains is useless.
Comparing the performance of these is irrelevant, they do completely different things. Use the right tool first, and only then if your performance is a problem use profiling to identify hot parts of your code to look at.

Related

what is the regex to use for Version numbers like 1.0.10.13 in C# [duplicate]

This question already has answers here:
Regular expression for version numbers
(8 answers)
Closed 2 years ago.
I have a required to validate the version numbers using regex. I tried few but not working as expected.
Any idea what regex to use to validate versions like 1.0.10.135 in C#. Every number should grow upto 3 digits in each section.
Update I tried this "[\d]{1,3}.[\d]{1,3}.[\d]{1,3}.[\d]{1,3}" and it accepted "1.0.0.3##", which is not correct.
If you want to go with a regex, I'm surprised you haven't found one that worked. Here's a quick one (it worked the first time I tried it):
(?<major>\d{1,3})\.(?<minor>\d{1,3})\.(?<patch>\d{1,3})(\.(?<build>\d{1,3}))?
It names the parts, and makes the last part optional

What is the use of an empty statement [duplicate]

This question already has answers here:
C# Empty Statement
(13 answers)
Closed 5 years ago.
I've come across this example of an empty statement in a C# textbook.
Code:
public void empty()
{
;
}
Some quick googling found that it's a redundant feature and I can't see the use of this as it seems pointless?
I was curious to know when this would've been useful and if it's still used to date even though it's obsolete?
In the given example it is pointless and/or cosmetic.
The empty statement is "useful" in places where a statement is required but you have nothing to do, like
while (condition_with_side_effects) ;
Because of the side effects required, this will not match with most coding guidelines or best practices.
Consider it a leftover from C.

Performance StringBuilder Insert versus string Concat [duplicate]

This question already has answers here:
How to use StringBuilder wisely?
(3 answers)
Closed 7 years ago.
What is more efficient in performance to prepend a string to another?
Using the StringBuilder.Insert method or the string.Concat method?
messageString.Insert(0, prependedString);
or
string.Concat(prependedString, messageString);
In my case the message string is relatively big, the prepended string is short.
string.Concat is the fastest method if the number of items is fixed. This statement holds true in all cases. It does not matter how long the strings are.
string.Concat calculates the final string size and then copies over the bits into a freshly allocated string. It cannot be done any faster.
In fact, you should write a + b instead of calling Concat (if that is possible in the specific situation).
for huge strings use string builder
False. Why would that be the case?!
If you're concating more than two strings use StringBuilder
False. If the number is fixed, use Concat. StringBuilder gains you nothing but adds overhead.
the answer depends on how many strings you are concatenating, and how big they are
False. The algorithm that I described above is always the fastest possible solution.
The myths around StringBuilder are an amazing variety. If you understand how both options work internally you can answer all these questions yourself. I did not study and memorize all these answers. I generate them from my understanding of internals.
This is a duplicate of How to use StringBuilder Wisely you can read my full answer over there, in short:
Concat function is faster than working with StringBuilder for the number of strings entering the function is known.

Is it a bad idea to convert byte arrays to strings then parse with regular expressions? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Here's the scenario: I've been recently tasked to write a rs232 serial device communication interface for our existing application. This application has base classes in place to do the actual communication. Basically all I do is accept a byte array into my class then process it.
Part of the issue is that the byte array delivered can be no more than 1000 bytes at a time yet there could be more data waiting to come in that belongs to that transaction. So I have no idea if what was delivered to me is complete. What I am doing is converting that 1000 byte array into a string and stuffing it into a buffer. This buffer then runs a regex to see if what was added creates a complete transaction. I know it's complete if it matches a particular signature (basically a series of control codes at the beginning and end). This buffer will only append data up to 3 times before giving up if no match is found in case of garbage data coming in and no match is ever possible. This isn't a high data volume device so I don't expect tons of data to come pouring in constantly. And the regular expression is only ever executed on, at most, 3000 characters.
So far it works pretty good, but my question is are regular expressions terrible for this? Are there any ramifications in regards to performance for what I'm using them for? My understanding is that regular expressions are typically bad for large volumes of data but I feel this is quite small.
are regular expressions terrible for this?
On the contrary, regular expressions are great for matching patterns in data sequences.
Are there any ramifications in regards to performance for what I'm using them for?
Regular expressions can be written in really inefficient ways, but that is usually a problem with a particular regular expression, not with regular expressions as a technique.
My understanding is that regular expressions are typically bad for large volumes of data but I feel this is quite small.
There is no universal definition of "large" and "small". Depending on a regex engine, your expression is usually translated into a state machine described by the expression. These machines are really efficient at what they do, in which case the size of the data block can be very considerable. On the other hand, one could write a regex with a lot of backtracking, causing unacceptable performance even on input strings of hundred characters or less.
nothing about what you're doing is raising any red flags.
Some things to keep in mind
Don't preoccupy yourself with performance. Just design your program first, and optimize for performance afterwards, and do so only if you have a performance problem.
Some tasks are unsuitable for regular expressions. Regular expressions can't parse XML very well, and they also can't parse patterns like XnYn Without knowing specifically what you're trying to match for with your regex, I can't really analyze whether it's suitable for your problem. Just be careful that you don't have any odd edge cases.
Regex being bad for large amounts of data is not something that I've heard before, and I've been looking around for it online, I'm still not finding much warning against it.
Normally, the most simple solution is the best one. If you can think of a more straight forward and simple solution to your problem, then go ahead with that. If not, then don't worry too much.

How do I parse a string and get formula? in C# [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Evaluate C# string with math operators
So lets say I have a basic string with the value "1*2+4"
How would I go about parsing the information and then doing the calculation?
I am self teaching c# but my assumption is this is a common issue in programming but without using a library how would one do this?
My results so far have got me splitting the string up and putting them into an array of chars, but this is where I've stopped since I am trying to figure out how to compare chars to operators and chars to ints.
I am not sure if I am going the right way about this but would be great if someone could point me in the right direction.
Thank you for your help in advanced.
What you're looking for is the Shunting-yard algorithm.
You'll need at least two stacks; one for storing operators and one for operands. After you fill the stacks you can make a RPN and calculate the answer.
Well c# (or any other language) might provide you with various tools to help you, but the overall approach to the problem will always remain the same whatever the programming language be.
So yes, you do split up into operators & integers. You do recognize the characters one by one, but try to do it in the most efficient way of the language. Fosco's anser points to the right link. Use Ncalc Library than doing manual labor.
However, to complete what you started :
int.Parse(str)
int.TryParse(str, out num)
...are the functions you may consider to convert character strings into integers (which you got, by using split() function?) in C#. You can read about them here...(Parse, TryParse)
If you want to learn how the various existing libraries do it, you should learn about parsing, lexical and syntactic analysis, expression trees, compiler theory, etc. Also, go through the source-code of any of the multiple open-source libraries that do it.

Categories