Search String in C#

we can perform search String in C#.Search string allow us to search String at specified number of character or a sub string,we can perform Search string method in any part of given string in a program or search string in text this code we will perform String using String Method

String Search

In c# programming language we can perform String search function in program.TWo methods are used for String search:
1. Search String using String Method.
2. Search String using Regular Expressions.

In this article we use String search Method
The string type is alias for the System.String class of C# provide us number of useful Method for searching contents of the string.

Given bellow code show you various Method such as IndexOf() and
LastIndex() for string search.

using System;
using System.Collections.Generic;
using System.Text;

namespace CsharpSearch

class Program
static void Main(string[]args)
string str="This is for show how to use search string.";


bool bool1=str.StartssWith("This is");

System.Console.WriteLine("starts with'this is'? {0} boll);

bool bool2=str.StartssWith("using to",System.StringComparison.OrdinalIgnoreCase);

System.Console.WriteLine("starts with'for show'? {0} (ignoring case)',bool2);

bool bool3=str=str.EndWith(".");

System.Console.WriteLine.WriteLine("ends with'.'? {0}",bool3);

int first=str.NidexOf("is");
int Last=str.LastIndexOf("search");
string str2=str.SubString(first,last=first);
System.Console.WriteLine('between is ad search words:'{0}'",str2);

Output of this code is:

This is for show how to use search string.
starts 'with 'this is'? True
starts with 'for show'? False
ends with '.'? True
between is and search words :'is is for show how to use'
press any key to continue.....


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: