I have string.like str..now it contains value test.Hoe can i find that this srting contains 's' or not??
For example,
string str="test" ;
now i want to check that str contains 's' or not?How can I find that???
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Jul 9, 2015, 10:52 PM EDT
And no, to check if a boolean variable is true, you don't need
== trueThe comparison operator itself returns a boolean value.
EDIT Since the
Containsmethod is an extension method, my solution appeared to be confusing to some. Here are two versions that doesn't require you to addusing System.Linq;:Posted Jul 9, 2015, 10:54 PM EDT
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Jul 9, 2015, 10:57 PM EDT
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string abc = "test";
var doesContainS = abc.ToLowerInvariant().Contains('s');
if (doesContainS == true)
Console.WriteLine("its present");
else
Console.WriteLine("its not present");
Console.ReadLine();
}
}
}
Manoj BhoirRank #599 · 4K reputationPosted Jul 9, 2015, 10:59 PM EDT
MessageBox.Show(str.Contains("s").ToString());
Pranay RanaRank #735 · 3.2K reputationPosted Jul 9, 2015, 11:06 PM EDT
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted Jul 9, 2015, 11:11 PM EDT
string search;
Console.WriteLine("Enter Your String");
str = Console.ReadLine();
Console.WriteLine("Enter Search string");
search = Console.ReadLine();
if(str.Contains(search))
{
Console.WriteLine("Strint {0} contain {1}",str,search);
}
else
{
Console.WriteLine("Strint {0} doesn't contain {1}",str,search);
}
Console.ReadKey();