Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a string in a string except first occurrence

Tags:

c#

How to replace a string in a string except first occurrence?

e.g. C:\\Test-Processed\1-Processed\2-Processed should output

C:\\Test-Processed\1\2

like image 944
user1941944 Avatar asked Apr 16 '13 05:04

user1941944


1 Answers

Something like following:

string originalStr = "C:\\Test-Processed\\1-Processed\\2-Processed";
string temp = "-Processed";
string str = originalStr.Substring(0, originalStr.IndexOf(temp) + temp.Length);
originalStr = str + originalStr.Substring(str.Length).Replace(temp, "");

originalStr will be:

originalStr = "C:\\Test-Processed\\1\\2"
like image 104
Habib Avatar answered Sep 20 '22 06:09

Habib