0

I am stumped. I wanted to know if any combination in sequence order of the string is available. like for example my string to match is

string somevar = "rat";

then if an another string contains "rat", "at", "ra" or "r", "a", "t" it should return false.

I can only think of making condition for every single sequence i can manually find. but I am sure there could be some trick to find it easily. I had a look on Implement a function that prints all possible combinations of the characters in a string but it doesnt do what I want.

I am sorry if this is not very clear but I want to check a condition where

 if(somevar == "rat" || somevar == "at" || somevar == "ra" || somevar == "r" || somevar == "a" || somevar == "t")
 {
      \\do something
 }
4
  • 1
    Last edit invalidate previous requirement. You said "contains" earlier and now you directly compare with set of values. Commented Dec 14, 2016 at 9:12
  • @Sinatr: Yeah that was creating the confusion so i removed it. I mean to say. is my last condition should return false. Sorry for all the trouble Commented Dec 14, 2016 at 9:15
  • 1
    @mybirthname "drastically" - i just had to change a few characters Commented Dec 14, 2016 at 9:39
  • @fubo yes but few characters can change the question a lot. From contains to equal the difference is really big in my opinion. Commented Dec 14, 2016 at 9:40

3 Answers 3

4

You don't need to check all substrings if it's sufficient that a single char is contained:

bool containsAnyChar = somevar.Intersect("rat").Any();

That works because a string implements IEnumerable<char>(is a collection of characters).

Sign up to request clarification or add additional context in comments.

2 Comments

@mybirthname: Any is less efficient if the string is large. It's just a loop so O(n), Intersect uses a set based aproach. If the string isn't large it doesn't matter anyway
@TimSchmelter yes I test it manually with 2 strings with 10k characters(1-10k numbers and 5k -15k numbers) and the difference is really big. I didn't know it, thanks for mention it like I said. I already upvoted you
3

You need a method that determines all substrings

public static IEnumerable<string> GetAllSubStrings(string input, int length)
{
    for (var i = 0; i < input.Length - length + 1; i++)
    {
        yield return input.Substring(i, length);
    }
}

then you can just create a list with all combintaions

string somevar = "rat";
List<string> subStrings = new List<string>();
for (int i = 0; i < somevar.Length; i++)
{
    subStrings.AddRange(GetAllSubStrings(somevar, i + 1));
}
// subStrings = {"rat", "at", "ra", "r", "a", "t"}

and finally check your other string against that list.

UPDATE to your updated Question:

//check "ra" 
string testItem = "ra";
bool contains = subStrings.Any(x => testItem == x);

Comments

-1
string somevar = "rat";
string anotherString = "rat is there";
bool result = somevar.ToCharArray().Any(ch => anotherString.Contains(ch));

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.