Replace the first occurrence in a string
.net, c#, replace
Solution
No need to add substrings, following will find the first space instance only. From MSDN:
Reports the zero-based index of the first occurrence of the specified string in this instance.
string x = "Hello my name is Marco";
int index = x.IndexOf(" ");
if (index >= 0)
{
x=x.Remove(index,1);
x = x.Insert(index, @"<br />");
}
Edit: If you are not sure if space will occur, some validations must come into place. I have edit the answer accordingly.
Problem
I have this string : ``` Hello my name is Marco ``` and I'd like to replace the first space (between `Hello` and `my`) with `<br />`. Only the first. What's the best way to do it on C#/.NET 3.5?