4

I want to compare one string with many strings. How is that done in C#?

8
  • 2
    OK, after reading this question several times and also deepasundaris own answer with additional info, i think he wants the following: I have a list of several strings and need to find a unknown substring that resides in every string within that list Commented Jan 15, 2010 at 7:42
  • please re-phrase the question to reflect what you really mean. Commented Jan 15, 2010 at 7:57
  • Can you please edit your initial question to provide a bit more information? I noticed that you clarified the problem in some of your comments here, but the best way would be to update the question itself, so that people don't have to dig through all the answers to understand what you need. Commented Jan 15, 2010 at 8:02
  • 1
    Also, this should be tagged as language-agnostic Commented Jan 15, 2010 at 8:03
  • 2
    You really need to stop re-posting this question. If something is unclear, please edit this question to clarify what you mean. Commented Jan 15, 2010 at 12:52

8 Answers 8

12

If you want to check if a string is contained in a list of strings you could use the Contains extension method:

bool isStringContainedInList = 
    new[] { "string1", "string2", "string3" }.Contains("some string")
Sign up to request clarification or add additional context in comments.

1 Comment

But the problem is i dono what is "Some String" i want to find that... how to do that? to find common among many strings...
5

I recommend that you look at this wikipedia article about the longest common substring problem.

I recall from undergrad that one strategy to find the longest common substring, you can start by finding a slightly shorter substring and then expand from there (and repeat). That is, if "abcd" is a common substring, then so does "abc" and so does "ab".

This lends to a repeating algorithm where you first find all the 2-letters pairs that appear in your strings (I am not bothering with one letter substrings because for large dataset they'll get include the whole alphabet). Then you iterate again to find all 3-letters substrings, and so on ...

Comments

5

To compare all strings in a collection to each other to find duplicates, it's most efficient to use a Dictionary:

string[] strings = { "Zaphod", "Trillian", "Zaphod", "Ford", "Arthur" };

var count = new Dictionary<string, int>();
foreach (string s in strings) {
  if (count.ContainsKey(s)) {
    count[s]++;
  } else {
    count.Add(s, 1);
  }
}
foreach (var item in count) {
  Console.WriteLine("{0} : {1}", item.Key, item.Value);
}

Output:

Zaphod : 2
Trillian : 1
Ford : 1
Arthur : 1

You can also do it using LINQ methods:

var count =
  strings
  .GroupBy(s => s)
  .Select(
    g => new { Key = g.First(), Value = g.Count() }
  );

2 Comments

Nice code, but it doesn't answer the question that the op intended
@Yoni: If you know the exact intention of the OP, please post it somewhere in this question.
0

If you want to compare, use String.Compare.
If you to find a string in a list, use the Contains/Select method equivalent of the list type.

Comments

0

I like to use the String.Compare() static method as it let's you make everything explicit. This is important as string comparisons can be notorious for subtle bugs.

For example:

// Populate with your strings
List<string> manyStrings = new List<string>();

string oneString="target string";

foreach(string current in manyStrings)
{
    // For a culture aware, safe comparison
    int compareResult=String.Compare(current,oneString,
                       StringComparison.CurrentCulture);
    // OR
    // For a higher performance comparison
    int compareResult=String.Compare(current,oneString,
                       StringComparison.Ordinal);

    if (compareResult==0) 
    {
        // Strings are equal 

    }
}

If you actually want to just know if a string is a substring of another larger string, in the above loop you can use:

int indexPos=current.IndexOf(oneString,StringComparison.Ordinal); 

if (indexPos>=0)
{
    // oneString was found in current
}

Note that IndexOf accepts the same useful StringComparison enumeration.

Comments

0

To find the strings in your list, which are in the list for multiple times, you could start putting those strings into a HashSet, and check for each one, whether it is already in this set.

For example, you could:

HashSet<string> hashSet = new HashSet<string>();

foreach (string item in myList)
{
    if (hashSet.Contains(item)) 
    {
        // already in the list
        ...
    }
    else
    {
        // not seen yet, putting it into the hash set
        hashSet.Add(item);
    }
}

Comments

0

Starting with a C# 9 version you can use logical patterns:

if (s is ("string1" or "string2" or "string3"))
{
    // do things
}

Comments

-1
 string[] comparisonList = {"a", "b" "c"};
 from s in comparisonList where comparisonList.Contains("b") select s;

2 Comments

I don't think LINQ is really necessary here.
@musicfreak: meh. @ash: it's been 4 years since LINQ released.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.