Janet
Janet

Reputation: 633

How can I check if a string exists in another string

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#

Upvotes: 57

Views: 135807

Answers (10)

user11961350
user11961350

Reputation: 9

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

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

Upvotes: 0

Alexandre Neukirchen
Alexandre Neukirchen

Reputation: 2783

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

Upvotes: 1

Pellet
Pellet

Reputation: 2437

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

Is another good variation for case-insensitive searches.

Upvotes: 5

liron
liron

Reputation: 385

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

Upvotes: 14

miguel
miguel

Reputation: 3009

Refer This.

String.Contains(...)

Upvotes: 4

itzmebibin
itzmebibin

Reputation: 9439

Use can use String.Contains for this.

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

Upvotes: 2

user3221427
user3221427

Reputation: 51

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

Upvotes: 5

Akram Shahda
Akram Shahda

Reputation: 14781

Use String.Contains:

if (stringValue.Contains(anotherStringValue))
{  
    // Do Something // 
}

Upvotes: 100

Bastardo
Bastardo

Reputation: 4152

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.

Upvotes: 5

user579674
user579674

Reputation: 2179

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

Upvotes: 3

Related Questions