kartal
kartal

Reputation: 18086

Split path by "\\" in C#

How can I split a path by "\\"? It gives me a syntax error if I use

path.split("\\");

Upvotes: 5

Views: 27213

Answers (8)

Nick
Nick

Reputation: 471

Better just use the existing class System.IO.Path, so you don't need to care for any system specifications.

It provides methods to access any part of a file path like GetFileName(string path) etc.

Upvotes: 0

cskwg
cskwg

Reputation: 849

A complete solution could look like this:

//
private static readonly char[] pathSeps = new char[] {
    Path.DirectorySeparatorChar,
    Path.AltDirectorySeparatorChar,
    Path.VolumeSeparatorChar,
};
//
///<summary>Split a path according to the file system rules.</summary>
public static string[] SplitPath( string path ) {
    if ( null == path ) return null;
    return path.Split( pathSeps, StringSplitOptions.RemoveEmptyEntries );
}

Some of the other proposed solutions in this article use the syntax: path.Split(new char[] {'/', '\'});

Although this will work, it has various disadvantages:

  1. It does not allow your application to adapt to various target platforms. Currently, our applications are basically running on UNIX and Windows OSs (Win, macOS, iOS, linux variations). So there is a fixed set of path characters. But this might change when dotNET were ported to other operating systems. So it is best to use the predefined constants.
  2. Performance of the inline syntax is worse. This might not be of interest for a handful of files, but when working with millions of files there are noticeable differences. The managed memory will go up until next GC. When looking at the generated assembly code you will find "call CORINFO_HELP_NEWARR_1_VC" for each of the 'new' statements, even in Release mode. This happens whenever you new-up any array, because arrays are not immutable. My proposed solution prevents this by declaring the array as readonly and static.
  3. Reusability of the inline syntax also is worse, because you might want to use the path separators array in other contexts.
  4. StringSplitOptions.RemoveEmptyEntries should be used to account for UNC paths and possible typos within the incoming path. The operating systems do not allow duplicate path separators, but there might be a typo from the user or a duplicate concatenation of path separator characters, for example when concatenating the path and filename.

Upvotes: -1

Alexander Stepaniuk
Alexander Stepaniuk

Reputation: 6408

To be on the safe side, you could use:

path.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar });

Upvotes: 7

Jack Culhane
Jack Culhane

Reputation: 781

On windows, using forward slashes is also accepted, in C# Path functions and on the command line, in Windows 7/XP at least.

e.g.: Both of these produce the same results for me:

dir "C:/Python33/Lib/xml"
dir "C:\Python33\Lib\xml"
(In C:)
dir "Python33/Lib/xml"
dir "Python33\Lib\xml"

On windows, neither '/' or '\' are valid chars for filename. On Linux, '\' is ok in filenames, so you should be aware of this if parsing for both.

So if you wanted to support paths in both forms (like I do) you could do:

path.Split(new char[] {'/', '\\'});

On Linux it would probably be safer to use Path.DirectorySeparatorChar.

Upvotes: 1

DeveloperX
DeveloperX

Reputation: 4683

Path.Split(new char[] { '\\\' });

Upvotes: 0

Mat
Mat

Reputation: 206689

You should be using

path.Split(Path.DirectorySeparatorChar);

if you're trying to split a file path based on the native path separator.

Upvotes: 37

Jon Skeet
Jon Skeet

Reputation: 1500385

There's no string.Split overload which takes a string. (Also, C# is case-sensitive, so you need Split rather than split). However, you can use:

string bits = path.Split('\\');

which will use the overload taking a params char[] parameter. It's equivalent to:

string bits = path.Split(new char[] { '\\' });

That's assuming you definitely want to split by backslashes. You may want to split by the directory separator for the operating system you're running on, in which case Path.DirectorySeparatorChar would probably be the right approach... it will be / on Unix and \ on Windows. On the other hand, that wouldn't help you if you were trying to parse a Windows file system path in an ASP.NET page running on Unix. In other words, it depends on your context :)

Another alternative is to use the methods on Path and DirectoryInfo to get information about paths in more file-system-sensitive ways.

Upvotes: 7

Eben Roux
Eben Roux

Reputation: 13246

Try path.Split('\\') --- so single quote (for character)

To use a string this works:

path.Split(new[] {"\\"}, StringSplitOptions.None)

To use a string you have to specify an array of strings. I never did get why :)

Upvotes: 9

Related Questions