63 821
Assignments Done
99,4%
Successfully Done
In August 2018

Answer to Question #4862 in C# for Olivia Weizman

Question #4862
Hi,
I have a homework to do in C Sharp. My teacher want us to replace a string x by a string y in a string p WITHOUT using the System.Replace thing. Please help me and know that we didnt learn lot. Help me using very basic method. Thnxxxxxx
Expert's answer
static public string ReplaceString(string str, string oldValue, string newValue, StringComparison comparison)
{
StringBuilder sb = new StringBuilder();

int previousIndex = 0;
int index = str.IndexOf(oldValue, comparison);
while (index != -1)
{
sb.Append(str.Substring(previousIndex, index - previousIndex));
sb.Append(newValue);
index += oldValue.Length;

previousIndex = index;
index = str.IndexOf(oldValue, index,
comparison);
}
sb.Append(str.Substring(previousIndex));

return sb.ToString();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions