MODIFY STRING IN C#

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
 

MODIFY STRING 
String modification is a technique in which we modify our string to a new string object by various methods and operations. As we know, a string is an alias of System.String and they are immutable (whose state cannot be changed once it is created).

In many cases, you can assign the new object to the variable that hold the original string.

Let's see this in action.

using System;

namespace AspnetSolutions

{
    class ModifyString
    {
        public static void Main()
        {
            string wonder = "The world famous Taj Mahal is build by the mughal emperor Shah Jahan.";
            Console.WriteLine(wonder);
            wonder = wonder.Replace("mughal", "Mughal");
            Console.WriteLine(wonder);
        }
    }
}

Output



Here, we have modified the old string using the Replace method. Also, we have assigned the new object to the variable that held the original string (i.e. wonder). We have already talked about Replace method in our previous Blogs.

Drawback of using Replace() method:

1. This method is case sensitive. Means if we pass "The" it may check character for character and if it doesn't find any string, it return False. Either string like "the" or "THE" are present.

Practical Implementation:

using System;

namespace AspnetSolutions

{
    class ModifyString
    {
        public static void Main()
        {
            string wonder = "THE world famous Taj Mahal is build by THe mughal emperor Shah Jahan.";
            Console.WriteLine(wonder);
            wonder = wonder.Replace("The", "the");
            Console.WriteLine(wonder);
        }
    }
}


Click imaginationhunt.blogspot to see latest Blogs

Output






 
There is another method that work in a similar manner.

Regex.Replace() method:

Regex.Replace() is RegularExpression method. This method work in a similar manner to replace string. All the types of RegularExpression method are found in System.Text.RegularExpressions namespace. It has 6 overloaded constructors. We'll use one of the overload Regex.Replace() below.


 Click imaginationhunt.blogspot to see latest Blogs
 
Practical Implementation:

using System;
using System.Text.RegularExpressions;

namespace AspnetSolutions
 {
    class ModifyString
    {
        public static void Main()
        {
            string wonder = "THE world famous Taj Mahal is build by THe mughal emperor Shah Jahan.";
            Console.WriteLine(wonder);
            wonder = Regex.Replace(wonder, "The", "the", RegexOptions.IgnoreCase);
            Console.WriteLine(wonder);
        }
    }
}


Output:
Hence, Regex.Replace() method replaces all the occurrences of substring with the match string from the specified string.




Related Questions:


Q-1 How many sub part of specified string are modified in this code using Regex.Replace()?

using System;
using System.Text.RegularExpressions;

namespace AspnetSolutions
 {
    class ModifyString
    {
        public static void Main()
        {
            Console.WriteLine("Original string");
            string tonguetwister = "She sells seashells by the seashore.\n" +
                "The shells she sells are surely seashells.\n" +
                "So if she sells shells on the seashore,\n" +
                "I'm sure she sells seashore shells.\n";

            Console.WriteLine("Modify string using Regex()");
            tonguetwister = Regex.Replace(tonguetwister, "She", "cee", RegexOptions.IgnoreCase);
            Console.WriteLine(tonguetwister);
        }
    }
}


A) 4
B) 8
C) 9
D) 7
Ans- Option(C).

Q-2 Which of the following method replace string?
A) Compare
B) Regex.Replace
C) Equals
D) Regex.Match
Ans- Option(B).

Click imaginationhunt.blogspot to see latest Blogs

Keep learning and sharing...

Comments

Popular posts from this blog

SSC CGL 2016 Tier-1 Marks / Result

DATA TYPES IN C#

Pattern printing program

IS STRING MUTABLE OR IMMUTABLE IN .NET?

Top Mutual Funds

SSC CGL 2017 TIER 2 RESULT OUT

JUST IN TIME (JIT)

Can we have multiple Main methods in one class

TOP 15 VISUAL STUDIO SHORTCUT KEYS

Top 10 Interview Tips and Questions