Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 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