C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Next: The example program shows that Contains is case-sensitive. It shows how to test the result of Contains.
Test: We call the Test method 2 times. In that method, the output is printed the console.
And: The string "Net" is first tested with Contains. Contains is case-sensitive.
Info: Test() shows how to test Contains for true and false. We can store the result in a bool.
True, FalseIfC# program that uses Contains
using System;
class Program
{
static void Main()
{
Test("The Dev Codes");
Test("dot net Codex");
}
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 'Codex' lowercase
//
if (input.Contains("Codex"))
{
Console.WriteLine("Contains 'Codex'");
}
//
// See if the string contains 'Dot'
//
if (!input.Contains("Dot"))
{
Console.WriteLine("Doesn't Contain 'Dot'");
}
}
}
Output
--- The Dev Codes ---
Contains 'Net': True
--- dot net Codex ---
Contains 'Net': False
Contains 'Codex'
Doesn't Contain 'Dot'
Tip: In IL Disassembler, we see how Contains is implemented. A screenshot of Contains is provided.
IL DisassemblerAnd: With StringComparison.Ordinal, all language characters are treated the same—regardless of the system locale.