How to replace a string without using REPLACE function using C#?

Hemanthlaxmi
Posted by Hemanthlaxmi under C# category on | Points: 40 | Views : 5737
This code snippet is used to replace the string without using the replace function using C#.Most of the times we get a requirement to replace the String with a certain string.
class StringReplaceWithOutUsingFunction
{
public static void Main(string[] args)
{
string sentence = "Welcome to English Club Grammar for English learners.";
string[] parts = sentence.Split(' ');
string matchString = "English";
string replaceString = "engLISh";
for (int i = 0; i < parts.Count(); i++)
{
parts[i] = parts[i].Length == 7 == true && parts[i] == matchString == true ? parts[i] = replaceString : parts[i];
}
foreach (string item in parts)
{
Console.Write(item + " ");
}
Console.ReadKey();
}

}

Comments or Responses

Posted by: Ndebata on: 8/1/2011 Level:Starter | Status: [Member] | Points: 10
Hi Hemanth,

Your solution will not work properly. Pls check it.
Posted by: T.saravanan on: 8/2/2011 Level:Silver | Status: [Member] [MVP] | Points: 10
Hi Hemanth,

In our website square brace of 'i' is consider about italic format.So kindly change integer variable in your code.

Login to post response