开发者

How to remove or replace "\" in C#

Does anyone knows how to remove/replace开发者_如何学C from string "/" and "\" in C#?

I have string which looks like this:

string myString= @"d:\Folder1\folder name2\file.ext";

I need either get rid of "\" or just get file name without all this garbage of its location.


The Path class

Path.GetFileName(str_myString);
Path.GetFileNameWithoutExtension(str_myString);

Whenever you need to do something modifying a path, the Path class is a go-to solution. It's platform-specific, and at least on windows, it supports both / and \ (via Path.DirectorySeparatorChar and Path.AltDirectorySeparatorChar).


Hope bellow syntax will be help full for you

string inputString = @"hello world]\ ";

            StringBuilder sb = new StringBuilder();
            string[] parts = inputString.Split(new char[] { ' ', '\n', '\t', '\r', '\f', '\v','\\' }, StringSplitOptions.RemoveEmptyEntries);
            int size = parts.Length;
            for (int i = 0; i < size; i++)
                sb.AppendFormat("{0} ", parts[i]);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜