Solved

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

Posted on 2009-05-10
4
949 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
[X]
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
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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…
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.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

717 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