Today
in Engineer's World, we are discussing a very important topic of C# -String in very easy way. Posted By- +Manish Kumar Gautam +LIV WIRE +ASP.NET SOLUTIONS
Click imaginationhunt.blogspot to see latest Blogs
5. Replace()
Replace() method is used to replace the old specified character or string with the new specified character or string from the given string. It replaces all the occurrence of that character from that string. Its return type is string.
Practical Implementation:
{
class Substring
{
public static void Main()
{
string str = "Name=Tapan,Education=BCA,Mb=xxxxxxx,Email=Tap@tap.com";
class Substring
{
public static void Main()
{
string str = "Name=Tapan,Education=BCA,Mb=xxxxxxx,Email=Tap@tap.com";
Console.WriteLine("Replace()");
//Using Replace()
Console.WriteLine("Using String: " + str.Replace("Mb", "Mobile"));
Console.WriteLine("Using Substring: " + str.Replace(str.Substring(25, 2), "Mobile"));
//Using Replace()
Console.WriteLine("Using String: " + str.Replace("Mb", "Mobile"));
Console.WriteLine("Using Substring: " + str.Replace(str.Substring(25, 2), "Mobile"));
}
}
}
}
}
Click imaginationhunt.blogspot to see latest Blogs
Output
FIGURE 5 |
In the output window, we have two different outcomes. Let's understand each one of them.
a) Using String- In first we learn how to replace the old specified string 'Mb' with new specified string 'Mobile'.
b)
Using Substring- Second one shows how we can get a sub part of the given string using substring() method and replace it with new string. For ex- fetching out 'Mb' and replacing it with 'Mobile'.
6. Remove()
Remove() method is used to remove the specified character or string from the given string. It has two overloads, which take its parameter in integer format. Its return type is string.
Practical Implementation:
{
class Substring
{
public static void Main()
{
string str = "Name=Tapan,Education=BCA,Mb=xxxxxxx,Email=Tap@tap.com";
class Substring
{
public static void Main()
{
string str = "Name=Tapan,Education=BCA,Mb=xxxxxxx,Email=Tap@tap.com";
Console.WriteLine("Remove()");
//Using Remove()
Console.WriteLine("Using String: " + str.Remove(5, 5));
Console.WriteLine("Using Substring: " + str.Substring(0,24).Remove(5,5));
//Using Remove()
Console.WriteLine("Using String: " + str.Remove(5, 5));
Console.WriteLine("Using Substring: " + str.Substring(0,24).Remove(5,5));
}
}
}
}
}
Click imaginationhunt.blogspot to see latest Blogs
Output
FIGURE 6 |
In the output window, we have two different outcomes. Let's understand each one of them.
a) Using String- In first we learn how to remove the old specified string that is 'Tapan' which is name value. We have done this by using Remove(5,5) method. The first integer value '5' denote the starting index in the string "str" and the next '5' denotes the length up to which we want to remove.
b)
Using Substring- Second one shows how we can get a sub part of the given string
using substring() method and then remove the string. For ex-
fetching out 'Name=Tapan,Education=BCA' and then removing the string part by using Remove(5,5). Hence, name value 'Tapan' is removed. And we get 'Name=,Education=BCA' as output.
To read STRING.LENGTH PROPERTY IN C# - Click here.
Related Questions:
Q-1 Which method is used to change string in C#?
A) Trim()
B) Edit()
C) Change()
D) None of the Above
Ans- Option (D).
Explanation- Replace() method is used to change string.
Q-2 What is the output of the code?
Click imaginationhunt.blogspot to see latest Blogs
{
class StringSubstringTesting
{
public static void Main(string[] args)
{
string str = "Imaginationhunt.blogspot.com";
class StringSubstringTesting
{
public static void Main(string[] args)
{
string str = "Imaginationhunt.blogspot.com";
Console.WriteLine("Using String: " + str.Replace('o','t'));
}
}
}
}
}
}
A) Imaginatitnhunt.blogspot.com (Only first occurrence)
B) Imaginationhunt.blogspot.ctm (Only last occurrence)
C) Imaginatitnhunt.blogspot.ctm (Only first and last occurrence)
D) Imaginatitnhunt.bltgsptt.ctm (All occurrences)
Ans- Option (D).
Click imaginationhunt.blogspot to see latest Blogs
Keep learning and sharing...
No comments:
Post a Comment