226

Possible Duplicate:
How do I replace the first instance of a string in .NET?

Let's say I have the string:

string s = "Hello world.";

how can I replace the first o in the word Hello for let's say Foo?

In other words I want to end up with:

"HellFoo world."

I know how to replace all the o's but I want to replace just the first one

1
  • 2
    Voted to re-open this old question, which as explicitly about pattern and regular expression. While it an be simplified in this case to literals strings, it is not technically same as the actual ask. Unfortunately the duplicate is also tagged with 'regex', although it does not include 'pattern' anywhere. Commented Sep 18, 2020 at 21:53

3 Answers 3

324

I think you can use the overload of Regex.Replace to specify the maximum number of times to replace...

var regex = new Regex(Regex.Escape("o"));
var newText = regex.Replace("Hello World", "Foo", 1);
Sign up to request clarification or add additional context in comments.

3 Comments

good option to replace
Will this short circuit the search after first occurrence find?
Yes. The third parameter on the .Replace() call is the maximum number of matches. As noted in other answers below, first replacing just a single instance on simple strings, this solution is not the very efficient. But for other use-cases it may be appropriate.
289
public string ReplaceFirst(string text, string search, string replace)
{
  int pos = text.IndexOf(search);
  if (pos < 0)
  {
    return text;
  }
  return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
}

here is an Extension Method that could also work as well per VoidKing request

public static class StringExtensionMethods
{
    public static string ReplaceFirst(this string text, string search, string replace)
    {
      int pos = text.IndexOf(search);
      if (pos < 0)
      {
        return text;
      }
      return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
    }
}

4 Comments

much better than the regex imho, this is what the regex will eventually do anyway, after skipping overhead. In very long strings with the result very far at the end, the regex impl might be better as it supports streaming the char array, but for small strings, this seems a lot more efficient...
Never, ever do this. Use .Net. (ie, use the regex call made for this purpose.)
@Fattie Care to explain why?
public static string ReplaceFirst(this string text, string search, string replace) { int pos = text.IndexOf(search); if (pos < 0) { return text; } string result = text.Remove(pos, search.Length).Insert(pos, replace); return result; }
19

There are a number of ways that you could do this, but the fastest might be to use IndexOf to find the index position of the letter you want to replace and then substring out the text before and after what you want to replace.

if (s.Contains("o"))
{
    s = s.Remove(s.IndexOf('o')) + "Foo" + s.Substring(s.IndexOf('o') + 1);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.