Solved

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

Posted on 2009-05-10
4
935 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?
0
Comment
Question by:dev_qasource
4 Comments
 
LVL 29

Expert Comment

by:anarki_jimbel
Comment Utility
You may use
int index = htmlString.IndexOf(substring);

if index>=0 - substring found
0
 
LVL 12

Expert Comment

by:geowrian
Comment Utility
use String.indexOf(). See here: http://dotnetperls.com/Content/IndexOf.aspx
0
 
LVL 9

Accepted Solution

by:
tetorvik earned 50 total points
Comment Utility
or alternatively yourString.Contains(substring);
this will return true if substring exists in your string.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now