Solved

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

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
About delegates in c sharp 3 47
Coding C# in Linux 8 65
How useful is the free version of Selenium? 3 34
Winform Module - What is the ASP.Net equiv 2 19
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

776 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