Solved

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

Posted on 2009-05-10
4
945 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 30

Expert Comment

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

if index>=0 - substring found
0
 
LVL 12

Expert Comment

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

Accepted Solution

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

Expert Comment

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 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