Reputation: 633
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
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
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
Reputation: 2437
if (stringValue.ToUpper().Contains("FIND_THIS"))
{
// Do Something //
}
Is another good variation for case-insensitive searches.
Upvotes: 5
Reputation: 385
IndexOf()
function will do the work...
It will return -1 if the string does not exist
Upvotes: 14
Reputation: 9439
Use can use String.Contains
for this.
if (test.Contains("abc"))
{
// Your Code Here
}
Upvotes: 2
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
Reputation: 14781
Use String.Contains:
if (stringValue.Contains(anotherStringValue))
{
// Do Something //
}
Upvotes: 100
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
Reputation: 2179
You have to use Regular Expressions. For example Regex.IsMatch(test, "abc")
. This will return true if test contains abc.
Upvotes: 3