I have 2 similar string except some special character. Is there a better way to compare and check for equality?  string str1 = "Hello (T) How"; string str2 = "Hello [T] How";  if (str1.Replace("(", "").Replace(")", "").Replace("[", "").Replace("]", "") == str2.Replace("(", "").Replace(")", "").Replace("[", "").Replace("]", "")) {     Console.WriteLine("Equal"); }
123543
I wonder if there is an easy way to check if two strings match by excluding certain characters in the strings. See example below.  I can easily write such a method by writing a regular expression to find the "wild card" characters, and replace them with a common character. Then compare the two strings str1 and str2. I am not looking for such implementations, but like to know whether there are any .Net framework classes that can take care of this. Seems like a common need, but I couldn't find any such method.  For example:  string str1 = "ABC-EFG";     string str2 = "ABC*EFG";   The two strings must be declared equal.  Thanks!
118546
Is there a regular expression that matches valid regular expressions?   (I know there are several flavors of regexps. One would do.)
21022
I would like to compare the contents of a couple of collections in my Equals method.  I have a Dictionary and an IList.  Is there a built-in method to do this?  Edited: I want to compare two Dictionaries and two ILists, so I think what equality means is clear - if the two dictionaries contain the same keys mapped to the same values, then they're equal.
18112
For example:     Is (A &or; B &or; C) &and; (D &or; E &or; F) the same as   (A &and; D) &or; (A &and; E) &or; (A &and; F) &or; (B &and; D) &or; (B &and; E) &or; ( B &and; F) &or; (C &and; D) &or; (C &and; E) &or; (C &and; F)?   How would I check equivalence in general?
453
I need to write a Java Comparator class that compares Strings, however with one twist.  If the two strings it is comparing are the same at the beginning and end of the string are the same, and the middle part that differs is an integer, then compare based on the numeric values of those integers.  For example, I want the following strings to end up in order they're shown:   aaa bbb 3 ccc bbb 12 ccc ccc 11 ddd eee 3 ddd jpeg2000 eee eee 12 ddd jpeg2000 eee   As you can see, there might be other integers in the string, so I can't just use regular expressions to break out any integer.  I'm thinking of just walking the strings from the beginning until I find a bit that doesn't match, then walking in from the end until I find a bit that doesn't match, and then comparing the bit in the middle to the regular expression "[0-9]+", and if it compares, then doing a numeric comparison, otherwise doing a lexical comparison.  Is there a better way?  Update I don't think I can guarantee that the other numbers in the string, the ones that may match, don't have spaces around them, or that the ones that differ do have spaces.
30755
How can I check if a string ends with a particular character in JavaScript?  Example: I have a string   var str = "mystring#";   I want to know if that string is ending with #. How can I check it?   Is there a endsWith() method in JavaScript? One solution I have is take the length of the string and get the last character and check it.   Is this the best way or there is any other way?
102259
I need to search a string in the string array. I dont want to use any for looping in it  string [] arr = {"One","Two","Three"};  string theString = "One"   I need to check whether theString variable is present in arr.
22356
Comparing string in C# is pretty simple.  In fact there are several ways to do it.  I have listed some in the block below.  What I am curious about are the differences between them and when one should be used over the others?  Should one be avoided at all costs?  Are there more I haven't listed?    string testString = "Test"; string anotherString = "Another";  if (testString.CompareTo(anotherString) == 0) {} if (testString.Equals(anotherString)) {} if (testString == anotherString) {}   (Note: I am looking for equality in this example, not less than or greater than but feel free to comment on that as well)
107932
How would I write the equivalent of C#'s  in JavaScript?  var haystack = 'hello world'; var needle = 'he';  haystack.startsWith(needle) == true   Note: This is an old question, and as pointed out in the comments ECMAScript 2015 (ES6) introduced the  method. However, at the time of writing this update (2015) .
6643
I am doing something where I realised I wanted to count how many /s I could find in a string, and then it struck me, that there were several ways to do it, but couldn't decide on what the best (or easiest) was.  At the moment I'm going with something like:  string source = "/once/upon/a/time/"; int count = source.Length - source.Replace("/", "").Length;   But I don't like it at all, any takers?  I don't really want to dig out RegEx for this, do I?  I know my string is going to have the term I'm searching for, so you can assume that...  Of course for strings where length > 1,  string haystack = "/once/upon/a/time"; string needle = "/"; int needleCount = ( haystack.Length - haystack.Replace(needle,"").Length ) / needle.Length;
40567
I'm working on a project where i find i'm checking for the following in many, many places:  if(item.Rate == 0 || item.Rate == null) { }   more as a curiousity than anything, what's the best way to check for both cases?  I've added a helper method which is:  public static bool nz(object obj) {     var parsedInt = 0;     var parsed = int.TryParse(obj.ToString(), out parsedInt);     return IsNull(obj) || (parsed &amp;&amp; parsedInt == 0); }   Is there a better way?
87499
Can someone provide a regular expression for parsing name/value pairs from a string?  The pairs are separated by commas, and the value can optionally be enclosed in quotes.  For example:  AssemblyName=foo.dll,ClassName="SomeClass",Parameters="Some,Parameters"
50739
I know how to do this if I iterate through all of the characters in the string but I am looking for a more elegant method.
43149
I did a search for A$, but a whole bunch of results containing "Å", "A's" and "as" were return, could not find one containing "A$":    Same thing for other special characters such as "#".
5510
When comparing two strings in c# for equality, what is the difference between InvariantCulture and Ordinal comparison?
7172
What would be the best way to look in a string[] to see if it contains a element. This was my first shot at it. But perhaps there is something that I am overlooking. The array size will be no larger than 200 elements.  bool isStringInArray(string[] strArray, string key) {     for (int i = 0; i &lt;= strArray.Length - 1; i++)         if (strArray[i] == key)             return true;     return false; }
22593
I would like to compare two collections (in C#), but I'm not sure of the best way to implement this efficiently.  I've read the other thread about , but it's not exactly what I'm looking for.  In my case, two collections would be equal if they both contain the same items (no matter the order).  Example:  collection1 = {1, 2, 3, 4}; collection2 = {2, 4, 1, 3};  collection1 == collection2; // true   What I usually do is to loop through each item of one collection and see if it exists in the other collection, then loop through each item of the other collection and see if it exists in the first collection. (I start by comparing the lengths).  if (collection1.Count != collection2.Count)     return false; // the collections are not equal  foreach (Item item in collection1) {     if (!collection2.Contains(item))         return false; // the collections are not equal }  foreach (Item item in collection2) {     if (!collection1.Contains(item))         return false; // the collections are not equal }  return true; // the collections are equal   However, this is not entirely correct, and it's probably not the most efficient way to do compare two collections for equality.  An example I can think of that would be wrong is:  collection1 = {1, 2, 3, 3, 4} collection2 = {1, 2, 2, 3, 4}   Which would be equal with my implementation. Should I just count the number of times each item is found and make sure the counts are equal in both collections?    The examples are in some sort of C# (let's call it pseudo-C#), but give your answer in whatever language you wish, it does not matter.  Note: I used integers in the examples for simplicity, but I want to be able to use reference-type objects too (they do not behave correctly as keys because only the reference of the object is compared, not the content).