57

Hope somebody can help me. I am just learning C# and I have a simple question.

I have a variable and I would like to check if that exists in another string. Something like

if ( test contains "abc" ) {

}

Is there an easy way to do this in C#

2
  • msdn.microsoft.com/en-us/library/dy85x1sa.aspx Commented May 1, 2011 at 12:31
  • 2
    Geepers... You almost answered that one in your question. Use the API Documentation Luke... the force is just sooo 90's Commented May 1, 2011 at 12:36

10 Answers 10

100

Use String.Contains:

if (stringValue.Contains(anotherStringValue))
{  
    // Do Something // 
}
Sign up to request clarification or add additional context in comments.

2 Comments

see my answer below for case-insensitive variation
case-insensitivity can be achieved using StringComparison param, no need to convert anything ToUpper(): string1.Contains(string2, StringComparison.OrdinalIgnoreCase)
14

IndexOf() function will do the work...
It will return -1 if the string does not exist

Comments

5
string MainString = "String Manipulation"; 
string SearchString = "pul"; 
int FirstChr = MainString.IndexOf(SearchString); 

This code shows how to search within a string for a sub string and either returns an index position of the start or a -1 which indicates the string has not been found.

you can also use Contains(), Contains is an instance method on the string type, which means you can call it on a specific string in your program. It has a bool result, which is true if the parameter is found, and false if it is not found.

using System;

class Program
{
    static void Main()
    {
    Test("Dot Net Perls");
    Test("dot net perls");
    }

    static void Test(string input)
    {
    Console.Write("--- ");
    Console.Write(input);
    Console.WriteLine(" ---");
    //
    // See if the string contains 'Net'
    //
    bool contains = input.Contains("Net");
    //
    // Write the result
    //
    Console.Write("Contains 'Net': ");
    Console.WriteLine(contains);
    //
    // See if the string contains 'perls' lowercase
    //
    if (input.Contains("perls"))
    {
        Console.WriteLine("Contains 'perls'");
    }
    //
    // See if the string contains 'Dot'
    //
    if (!input.Contains("Dot"))
    {
        Console.WriteLine("Doesn't Contain 'Dot'");
    }
    }
}

check C# String Functions and Manipulation for anything about strings.

Comments

5

using String.Contains(...) may not be a good idea.

String.Contains(...) does an ordinal case-sensitive comparison. So, be careful about case matching.

ofcourse you can use ToLower() or ToUpper() before you check

Comments

5
if (stringValue.ToUpper().Contains("FIND_THIS"))
{  
    // Do Something // 
} 

Is another good variation for case-insensitive searches.

Comments

4

Refer This.

String.Contains(...)

Comments

3

You have to use Regular Expressions. For example Regex.IsMatch(test, "abc"). This will return true if test contains abc.

1 Comment

As you can see above, you do not "need" to use regular expressions. In fact, that way may be the most obtuse, slowest, and hardest to understand ways of them all.
2

Use can use String.Contains for this.

if (test.Contains("abc"))
{  
    // Your Code Here
}

Comments

1

There are some methods that can do this validation like CompareTo, Contains, Compare, IndexOfAny and IndexOf.

Check the list of methods of the String class.

string s1 = "ani\u00ADmal";
string s2 = "animal";
string s3 = "abc";
string s4 = "abc";
string s5 = "ABC";

bool b1 = s1.CompareTo(s2) > -1; // return true, exists
bool b2 = s3.CompareTo(s4) > -1; // return true, exists
bool b3 = s3.CompareTo(s5) > -1; // return false, no case sensitive, no exists

bool b4 = s1.Contains(s2); // return false, no exists
bool b5 = s3.Contains(s4); // return true, exists
bool b6 = s3.Contains(s5); // return false, no case sensitive, no exists

string s6 = "MACHINE";
string s7 = "machine";
string s8 = "nature";

int a = String.Compare(s6, 0, s7, 0, s6.Length, true);  // return 0, contain and is less 
int b = String.Compare(s6, 0, s7, 0, s6.Length, false); // return 1, contain and is greater
int c = String.Compare(s6, 0, s8, 0, s6.Length, true);  // return -1, no contain
int d = String.Compare(s6, 0, s8, 0, s6.Length, false);  // return -1, no contain

int e = s1.IndexOfAny(s2.ToCharArray()); // return 0, exists
int f = s3.IndexOfAny(s4.ToCharArray()); // return 0, exists
int g = s3.IndexOfAny(s5.ToCharArray()); // return -1, no case sensitive, no exists

int h = s1.IndexOf(s2); // return 0, exists
int i = s3.IndexOf(s4); // return 0, exists
int j = s3.IndexOf(s5); // return -1, no exists

Comments

0

You can use .Contains(), but, also use .ToUpper() as .Contains() is case sensitive.

if(string1.ToUpper().Contains(string2.ToUpper())
{
    //Your code goes here
}

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.