3

I have a situation where i don't want to compare total string length to other string .

Example:

string MainString = "Deanna Ecker";

string SearchString = "Ecker Designs";

int value = MainString.IndexOf(SearchString);

here it is searching with whole string. but i need to find any word in MainString. not with whole string..

Let me know how is this possible.

2
  • 1
    Split string by space and then search by each part of splitted string Commented Mar 14, 2014 at 12:10
  • given answers solve your problem. if you are also interested in matching words like "apple" with "apples" or "aple", en.wikipedia.org/wiki/Levenshtein_distance Commented Mar 14, 2014 at 12:16

3 Answers 3

8

If case-sensitivity is not an issue, you could split both strings by the space, then intersect the two lists to see if there are any matches:

var foundWords = MainString.Split(' ').Intersect(SearchString.Split(' '));

Or if you only want to know if a word was found:

var isMatch = MainString.Split(' ').Intersect(SearchString.Split(' ')).Any();
Sign up to request clarification or add additional context in comments.

Comments

1

you can convert your string to a char array then search each character via looping from all characters such that

public bool MatchString(string first,string second)
{
  char[] ch1=first.ToCharArray();
  char[] ch2=second.ToCharArray();
  bool match=false;
  for(int i=0 ; i<ch1.length ; i++)
   {
      for(int j=0 ; j<ch2.length ; j++)
       {
             if(ch2[j]==ch[i])
              {
                match=true;
                break;
              } 
       }
   }
 return match;
}

Comments

0

Try: var wordMatch = MainString.Split(' ').Intersect(SearchString.Split(' ')).Any();

Comments

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.