51

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#

forsvarir
  • 10,749
  • 6
  • 46
  • 77
Janet
  • 573
  • 2
  • 5
  • 5

10 Answers10

93

Use String.Contains:

if (stringValue.Contains(anotherStringValue))
{  
    // Do Something // 
}
forsvarir
  • 10,749
  • 6
  • 46
  • 77
Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
  • 1
    see my answer below for case-insensitive variation – Pellet Sep 06 '17 at 02:24
  • case-insensitivity can be achieved using StringComparison param, no need to convert anything ToUpper(): `string1.Contains(string2, StringComparison.OrdinalIgnoreCase)` – Phil B May 05 '22 at 14:48
13

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

w.b
  • 11,026
  • 5
  • 30
  • 49
liron
  • 375
  • 2
  • 12
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

Robert
  • 5,278
  • 43
  • 65
  • 115
user3221427
  • 51
  • 1
  • 1
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.

Bastardo
  • 4,144
  • 9
  • 41
  • 60
4
if (stringValue.ToUpper().Contains("FIND_THIS"))
{  
    // Do Something // 
} 

Is another good variation for case-insensitive searches.

Pellet
  • 2,254
  • 1
  • 28
  • 20
4

Refer This.

String.Contains(...)
itzmebibin
  • 9,199
  • 8
  • 48
  • 62
miguel
  • 2,961
  • 4
  • 26
  • 34
3

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

user579674
  • 2,159
  • 6
  • 30
  • 40
  • 7
    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. – VoidKing Dec 21 '12 at 21:57
2

Use can use String.Contains for this.

if (test.Contains("abc"))
{  
    // Your Code Here
}
itzmebibin
  • 9,199
  • 8
  • 48
  • 62
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
Alexandre Neukirchen
  • 2,713
  • 7
  • 26
  • 36
0

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

if(string1.ToUpper().Contains(string2.ToUpper())
{
    //Your code goes here
}
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44