How to Replace the *First Instance* of a String in .Net

Replace first occurrence of pattern in a string

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);

Replace the first occurrence in a string

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.

How do I replace the first occurrence of a Substring in .NET?


// Option 1
var result = "%" + pctSign.Remove(0, 2);

// Option 2
var result = "%" + pctSign.Substring(2);

// Option 3
var regex = new Regex("^\\0");
var result = regex.Replace(pctSign, "%");

If you absolutely want to user String.Replace() then you can write your own extension method:

public static class StringExtension
{
public static String Replace(this string self,
string oldString, string newString,
bool firstOccurrenceOnly = false)
{
if ( !firstOccurrenceOnly )
return self.Replace(oldString, newString);

int pos = self.IndexOf(oldString);
if ( pos < 0 )
return self;

return self.Substring(0, pos) + newString
+ self.Substring(pos + oldString.Length);
}
}

// Usage:
var result = pctSign.Replace("/0", "%", true);

How to use replace only the first occurence of www

As Ally suggested. You are much better off using System.Uri. This also replaces the leading www as you wish.

private string FilterUrl(string url)
{
Uri uri = new UriBuilder(url).Uri; // defaults to http:// if missing
return Regex.Replace(uri.Host, "^www.", "") + uri.PathAndQuery;
}

Edit: The trailing slash is because of the PathAndQuery property. If there was no path you are left with the slash only. Just add another regex replace or string replace. Here's the regex way.

return Regex.Replace(uri.Host, "^www.", "") + Regex.Replace(uri.PathAndQuery, "/$", "");

How to Regex search/replace only first occurrence in a string in .NET?

From MSDN:

Replace(String, String, Int32)   

Within a specified input string, replaces a specified maximum number of strings that
match a regular expression pattern with a specified replacement string.

Isn't this what you want?

How to replace first occurrence of whitespace with comma?

First of all, String.Replace method1 returns your string as plmca60,,,,,5 because from documentation;

Returns a new string in which all occurrences of a specified string in
the current instance are replaced with another specified string.

You don't need regex for that.

You can use String.IndexOf(string) to get first index of a white space and a little bit Remove and Insert methods combination.

Reports the zero-based index of the first occurrence of the specified
string in this instance

string sku = "plmca60     5";
int index = sku.IndexOf(" ");
sku = sku.Remove(index, 1).Insert(index, ",");
Console.WriteLine(sku);

Result will be;

plmca60,    5

Here a demonstration.

1: Remember, it is Replace, not replace. C# is case sensitive language.

Replace first time a combination of chars occur in a string VB.NET

Enigmativity's is a cool 1 liner. You can also try this..

    if(result.StartsWith("00"))
result= result.Replace(result.Substring(0, 2), "+");


Related Topics



Leave a reply



Submit