Today
in Engineer's World, we are discussing a very important topic of C# -String in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR +ASP.NET SOLUTIONS
Click imaginationhunt.blogspot to see latest Blogs
SEARCH IN STRING
Searching in the string means an act to search a specified string object within the string.
There are numerous ways of doing this:
1) Contains()
2) IndexOf()
3) LastIndexOf()
4) StartsWith()
5) EndsWith()
2) IndexOf()
3) LastIndexOf()
4) StartsWith()
5) EndsWith()
Contains() method:
Contains() return a specified string object within the string. This method is present in System.String Class. It returns boolean value, i.e. "true" if string object is found within string otherwise return "false".
IndexOf() method:
IndexOf() is used to search the position of the specified character from the given string. Its return type is Integer.
StartsWith()
method:
StartsWith() determines whether the start of this string matches the specified string when compared using the specified comparison option. This method is present in System.String Class. It returns boolean value, i.e. "true" if the string object starts with the string to seek otherwise return "false".
EndsWith() method:
EndsWith() determines whether the end of this string matches the specified string when compared using the specified comparison option. This method is present in System.String Class. It returns boolean value, i.e. "true" if the string object ends with the string to seek otherwise return "false".
Let's see all these method quickly by an example.
Example- C# program that uses all the above method.
Practical Implementation
using System;
namespace AspnetSolutions
{
class searchString
{
public static void Main()
{
string str = "A quick brown fox jump over a lazy dog.";
// Using Contains() method
bool value = str.Contains("fox");
Console.WriteLine("fox contain in string? {0}", value);
// Using IndexOf() method
int index = str.IndexOf("jump");
Console.WriteLine("index is {0}", index);
// Using LastIndexOf() method
int lastindexof = str.LastIndexOf("o");
Console.WriteLine("lastindexof is {0}", lastindexof);
// Using StartsWith() method
bool startsvalue = str.StartsWith("A quick");
Console.WriteLine("Starts with {0}", startsvalue);
// Using EndsWith() method
bool endsvalue = str.EndsWith("dog.");
Console.WriteLine("ends with {0}",endsvalue);
}
}
}
Click imaginationhunt.blogspot to see latest Blogs
Output
[Note]:
1. Contains() method is case-sensitive. Except Contains() all other four methods (IndexOf(), LastIndexOf(), StartsWith(), EndsWith()) are case-in-sensitive.
2. Contains(), StartsWith() and EndsWith() method return boolean value, whereas the other two LastIndexOf() and IndexOf() method return integer values.
To read STRING SEARCH USING REGEX METHOD IN C# - Click here.
Contains() return a specified string object within the string. This method is present in System.String Class. It returns boolean value, i.e. "true" if string object is found within string otherwise return "false".
IndexOf() method:
IndexOf() is used to search the position of the specified character from the given string. Its return type is Integer.
LastIndexOf()
method:
LastIndexOf() is used to search the last position of the specified character from the
given string. Its return type is Integer.
StartsWith() determines whether the start of this string matches the specified string when compared using the specified comparison option. This method is present in System.String Class. It returns boolean value, i.e. "true" if the string object starts with the string to seek otherwise return "false".
EndsWith() method:
EndsWith() determines whether the end of this string matches the specified string when compared using the specified comparison option. This method is present in System.String Class. It returns boolean value, i.e. "true" if the string object ends with the string to seek otherwise return "false".
Let's see all these method quickly by an example.
Example- C# program that uses all the above method.
Practical Implementation
using System;
namespace AspnetSolutions
{
class searchString
{
public static void Main()
{
string str = "A quick brown fox jump over a lazy dog.";
// Using Contains() method
bool value = str.Contains("fox");
Console.WriteLine("fox contain in string? {0}", value);
// Using IndexOf() method
int index = str.IndexOf("jump");
Console.WriteLine("index is {0}", index);
// Using LastIndexOf() method
int lastindexof = str.LastIndexOf("o");
Console.WriteLine("lastindexof is {0}", lastindexof);
// Using StartsWith() method
bool startsvalue = str.StartsWith("A quick");
Console.WriteLine("Starts with {0}", startsvalue);
// Using EndsWith() method
bool endsvalue = str.EndsWith("dog.");
Console.WriteLine("ends with {0}",endsvalue);
}
}
}
Click imaginationhunt.blogspot to see latest Blogs
Output
[Note]:
1. Contains() method is case-sensitive. Except Contains() all other four methods (IndexOf(), LastIndexOf(), StartsWith(), EndsWith()) are case-in-sensitive.
2. Contains(), StartsWith() and EndsWith() method return boolean value, whereas the other two LastIndexOf() and IndexOf() method return integer values.
To read STRING SEARCH USING REGEX METHOD IN C# - Click here.
Related Questions:
#newtoprgm try first program
Q-1 Write the code to calculate the age of John from the given string?
string dateOfBirth = "John was born on 25/05/1992 in Vashi. And today 25/05/2015, his age is?";
Q-1 Write the code to calculate the age of John from the given string?
string dateOfBirth = "John was born on 25/05/1992 in Vashi. And today 25/05/2015, his age is?";
Ans- The Solution code is-
using System;
namespace AspnetSolutions
{
class searchString
{
public static void Main()
{
string dateOfBirth = "John was born on 25/05/1992 in Vashi. And today 25/05/2015, his age is?";
string year1 = dateOfBirth.Substring(dateOfBirth.IndexOf("1992"), "1992".Length);
string year2 = dateOfBirth.Substring(dateOfBirth.IndexOf("2015"), "2015".Length);
int age = Int32.Parse(year2) - Int32.Parse(year1);
Console.WriteLine("Age is {0}", age);
}
}
}
Q-2 What will be the output of the code?
using System;
namespace AspnetSolutions
{
class searchString
{
public static void Main()
{
string websiteName = "www.imaginationhunt.blogspot.com";
int searchResult = websiteName.LastIndexOf("blogsot");
Console.WriteLine(searchResult);
}
}
}
A) -1
B) 20
C) Exception
Ans- Option(A).
Explanation- If specified string is not found in given string then LastIndexOf() would return -1.
Click imaginationhunt.blogspot to see latest Blogs
Keep learning and sharing...
No comments:
Post a Comment