3

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?

Robaticus
  • 22,857
  • 5
  • 54
  • 63
markzzz
  • 47,390
  • 120
  • 299
  • 507
  • 1
    You should have given it a go yourself. This is the type of situation that can be good fun to solve. – Lloyd Powell Apr 27 '12 at 12:41
  • possible duplicate of [How do I replace the *first instance* of a string in .NET?](http://stackoverflow.com/questions/141045/how-do-i-replace-the-first-instance-of-a-string-in-net) – Greg Bacon Apr 28 '12 at 02:27

6 Answers6

11
 public static class MyExtensions
 {

   public static string ReplaceFirstOccurrance(this string original, string oldValue, string newValue)
    {
     if (String.IsNullOrEmpty(original))
        return String.Empty;
     if (String.IsNullOrEmpty(oldValue))
        return original;
     if (String.IsNullOrEmpty(newValue))
        newValue = String.Empty;
     int loc = original.IndexOf(oldValue);
     return original.Remove(loc, oldValue.Length).Insert(loc, newValue);
    }
}

and use it like:

string str="Hello my name is Marco";  
str.ReplaceFirstOccurrance("Hello", "<br/>");
str.ReplaceFirstOccurrance("my", "<br/>");
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
  • 1
    You should make this an extension method, then it'd be `"This is what is right".ReplaceFirstOccurance("is","
    ")`
    – Jamiec Apr 27 '12 at 12:41
  • 1
    I know extension methods but y should this to be Extension Method, this can be used as it is. but yes if you want you can convert it to Extension Method. – Zaheer Ahmed Apr 27 '12 at 12:47
  • 1
    Because this method is specific to a string, so as a general rule should be extended onto an instance of a string, rather than `SomeUtilityClass.ReplaceFirstOccurance`. Its exactly what extension methods are for! – Jamiec Apr 27 '12 at 12:49
5

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.

Marshal
  • 6,551
  • 13
  • 55
  • 91
  • I like the compacted version. But if the string don't have any space, I can't use it...) – markzzz Apr 27 '12 at 12:47
  • @markzzz If you are not sure if space will occur, some validations must come into place. I have edit the answer accordingly. – Marshal Apr 27 '12 at 12:51
  • 8
    This doesn't replace the string but just inserts a new inputted string after the first occurrence of that string, So at the end x would be `Hello
    my name is Marco` but it should be this `Hello
    my name is Marco`.
    – yogi Dec 04 '12 at 11:47
  • -1, what @yogi said, there's a couple of working answers here: http://stackoverflow.com/questions/8809354/replace-first-occurrence-of-pattern-in-a-string – Joe Jul 31 '13 at 06:28
  • @Joe I have mentioned an edit also, so he can use switch case. I think my answer is not incorrect or out-of-the-context to get a down vote. Surprise ! – Marshal Jul 31 '13 at 11:19
  • 2
    The question is titled 'how do you *replace* the first occurance in a string', your solution is answering 'how do you *insert* an extra string at the first occurance'. E.g if he wanted to replace the word 'Marco' with another name, your answer will show the inserted name + Marco. Your edit doesn't solve what is essentially using the wrong function. – Joe Jul 31 '13 at 23:25
3
string tmp = "Hello my name is Marco";
int index = tmp.IndexOf(" ");
tmp = tmp.Substring(0, index) + "<br />" + tmp.Substring(index + 1);
Steve Danner
  • 21,818
  • 7
  • 41
  • 51
1

Here you go, this would work:

var s = "Hello my name is Marco";
var firstSpace = s.IndexOf(" ");
var replaced = s.Substring(0,firstSpace) + "<br/>" + s.Substring(firstSpace+1);

You could make this into an extension method:

public static string ReplaceFirst(this string input, string find, string replace){
  var first= s.IndexOf(find);
  return s.Substring(0,first) + replace + s.Substring(first+find.Length);
}

And then the usage would be

var s = "Hello my name is Marco";
var replaced = s.ReplaceFirst(" ","<br/>");
Jamiec
  • 133,658
  • 13
  • 134
  • 193
0
string[] str = "Hello my name is Marco".Split(' ');
string newstr = str[0] + "<br /> " + string.Join(" ", str.Skip(1).ToArray());
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
0

Simply use

Replace("Hello my name is Marco", " ", "<br />", 1, 1)
knoami
  • 75
  • 1
  • 2
  • 7