We help IT Professionals succeed at work.

Looking for C# code to find a substring inside a string

dev_qasource
dev_qasource asked
on
1,022 Views
Last Modified: 2012-05-06
Hi,

I was looking for a solution in C# where I could confirm that the Sub-string exists inside a long string.

Suppose I want to find that sub-string 'save' exists inside the string - 'To save the current HTML file' or not.

Can anyone provide C# code for this?
Comment
Watch Question

Dmitry GSenior Developer
CERTIFIED EXPERT

Commented:
You may use
int index = htmlString.IndexOf(substring);

if index>=0 - substring found

Commented:
use String.indexOf(). See here: http://dotnetperls.com/Content/IndexOf.aspx
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
You could do something like the following if you merely want to confirm existence:
string test = "To save the current HTML file";
bool hasIt;
 
hasIt = test.Contains("save");

Open in new window

Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.