4b0
4b0

Reputation: 22323

Split and replace string

I have a string in a format:

string path="/fm/Templates/testTemplate/css/article.jpg";

I want to split this with second '/' and replace with '/' to '\\' want a result like this.

string newPath="\\Templates\\testTemplate\\css\\article.jpg";

My path is dynamic created so folder hierarchy is not fixed. What is the best way to do this.May I split first string path with / and go to loop to re-concate this and repalce with '/' to '\\' or there any easy way I am missing.

Upvotes: 0

Views: 15404

Answers (8)

Habib
Habib

Reputation: 223282

You may create a new string after removing the first item from the path. The following will give you the desired result. It may be optimized.

You can try:

  • Split the string on char / and remove empty entries.
  • Get a new array after disregarding the first item from the split array
  • Use string.Join to create the new string with \\ as separator.

Here is the code:

string path = "/fm/Templates/testTemplate/css/article.jpg";
string[] temp = path.Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
var newTemp = temp.AsEnumerable().Where((r, index) => index > 0);
string newPath2 = string.Join("\\", newTemp);

Upvotes: 0

Vishal Suthar
Vishal Suthar

Reputation: 17194

Try with this:

public static int nthOccurrence(String str, char c, int n)
{
    int pos = str.IndexOf(c, 0);
    while (n-- > 0 && pos != -1)
        pos = str.IndexOf(c, pos + 1);
    return pos;
}    

string path = "/fm/Templates/testTemplate/css/article.jpg";

int index = nthOccurrence(path, '/', 1);
string newpath = path.Substring(index).Replace("/", "\\");

OUTPUT: "\\Templates\\testTemplate\\css\\article.jpg"

Upvotes: 0

verdesmarald
verdesmarald

Reputation: 11866

Is your string manipulation really so time critical that you need to do everything at once? It's much easier just to break it down into parts:

// First remove the leading /../ section:
path = Regex.Replace("^/[^/]*/", "", path);

// Then replace any remaining /s with \s:
path = path.Replace("/", "\\");

Upvotes: 0

Azodious
Azodious

Reputation: 13882

string path = "/fm/Templates/testTemplate/css/article.jpg";
path = path.Substring(path.IndexOf('/', 1)).Replace("/", "\\\\");

Upvotes: 1

John Woo
John Woo

Reputation: 263803

Try this,

string path = "/fm/Templates/testTemplate/css/article.jpg";
string[] oPath = path.Split('/');
string newPath = @"\\" + string.Join(@"\\", oPath, 2, oPath.Length - 2);
Console.WriteLine(newPath);

Upvotes: 0

Nitesh Kumar
Nitesh Kumar

Reputation: 1774

You can use the following code.

string path="/fm/Templates/testTemplate/css/article.jpg"; 
string newpath = path.Replace("/fm", string.Empty).Replace("/","\\"); 

Thanks

Upvotes: 0

Sepster
Sepster

Reputation: 4849

If you're working with individual segments of your path (and not just replacing the direction of the slashes), the System.Uri class will help (.Segments property).

Eg from MSDN

Uri uriAddress1 = new Uri("http://www.contoso.com/title/index.htm");
Console.WriteLine("The parts are {0}, {1}, {2}", uriAddress1.Segments[0], uriAddress1.Segments[1], uriAddress1.Segments[2]);

Upvotes: 0

Shoban
Shoban

Reputation: 23016

Can't you just use String.Replace?

Upvotes: 2

Related Questions