0

How can I check a specific string to see if it contains a series of substrings? Specifically something like this:

public GetByValue(string testString) {

    // if testString contains these substrings I want to throw back that string was invalid
    // string cannot contain "the " or any part of the words "College" or "University"

   ...
}
2
  • Depends on how you see this evolving in the future. A regular expression might be a good start. Have you tried anything? Commented Nov 29, 2011 at 13:38
  • Not really sure where to start... my data access layer is using the string entered to do a search... if the string contains "the " or any part of the words "college" or "university" - example: "col" or "uni" - then there are too many rows returned, too generic. As far as growth, just those 3 words are all I care about. I'm using Linq to SQL in the DAL Commented Nov 29, 2011 at 13:54

5 Answers 5

1

If performance is a concern, you may want to consider using the RegexStringValidator class.

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

Comments

0

You can use string.Contains() method

http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx

// This example demonstrates the String.Contains() method
using System;

class Sample 
{
public static void Main() 
{
string s1 = "The quick brown fox jumps over the lazy dog";
string s2 = "fox";
bool b;
b = s1.Contains(s2);
Console.WriteLine("Is the string, s2, in the string, s1?: {0}", b);
}

} /* This example produces the following results:

Is the string, s2, in the string, s1?: True */

Comments

0

This an interesting question. As @Jon mentioned, a regular expression might be a good start because it will allow you to evaluate multiple negative matches at once (potentially). A naive loop would be much less efficient in comparison.

Comments

0

You can check it as follow....

   class Program
{



public static bool checkstr(string str1,string str2)
{
 bool c1=str1.Contains(str2);
 return c1;

}

public static void Main()
{
string st = "I am a boy";
string st1 = "boy";

bool c1=checkstr(st,st1);
//if st1 is in st then it print true otherwise false
      System.Console.WriteLine(c1);
}
}

Comments

0

Decided against checking for strings to limit my data return, instead limited my return to a .Take(15) and if the return count is more than 65,536, just return null

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.