Ersel Bozkartal
Ersel Bozkartal

Reputation: 73

string to replace specific characters

For example => iddaa///news/haber05112013.jpg

Result=> iddaa/news/haber05112013.jpg

I want to replace this spesific characters ("/"); But sometimes double characters ('///') , sometimes ('//') can ı replace this? Thank you

Upvotes: 4

Views: 158

Answers (4)

Alex Filipovici
Alex Filipovici

Reputation: 32571

You may use a regular expression. Try this:

var strTargetString = @"iddaa///news/haber05112013.jpg";
var strRegex = @"/+";
RegexOptions myRegexOptions = RegexOptions.Multiline;
Regex myRegex = new Regex(strRegex, myRegexOptions);
var strReplace = @"/";
var result =  myRegex.Replace(strTargetString, strReplace);

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727027

If you don't know how many repetitions there are, use Regex.Replace:

var res = Regex.Replace(orig, "/+", "/");

"/+" is a regular expression that matches one or more forward slash.

Upvotes: 9

Mohammad Arshad Alam
Mohammad Arshad Alam

Reputation: 9862

Simplest Way is :

string str = "iddaa///news/haber05112013.jpg";
str = str.Replace("///", "/").Replace("//","/");

Upvotes: 0

Avi Turner
Avi Turner

Reputation: 10456

This code will handle any length of / sequence:

string a = //some string
while(a.Contains(@"//"))
{
    a = a.Replace(@"//",@"/");
}

@dasblinkenlight answer is better. Regex is the right way to go.

Upvotes: 0

Related Questions