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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Pankaj Kumar ChoudharyPosted Jul 10, 2015, 3:11 AM
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();
Pranay RanaPosted Jul 10, 2015, 3:06 AM
Manoj BhoirPosted Jul 10, 2015, 2:59 AM
MessageBox.Show(str.Contains("s").ToString());
Sanjeeb LenkaPosted Jul 10, 2015, 2:57 AM
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();
}
}
}
Posted Jul 10, 2015, 2:54 AM
Sanjeeb LenkaPosted Jul 10, 2015, 2:52 AM
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;: