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#
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#
Use String.Contains:
if (stringValue.Contains(anotherStringValue))
{
// Do Something //
}
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.
You have to use Regular Expressions. For example Regex.IsMatch(test, "abc"). This will return true if test contains abc.
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