开发者

How to remove or replace "\" in C#

开发者 https://www.devze.com 2023-04-04 01:31 出处:网络
Does anyone knows how to remove/replace开发者_如何学C from string \"/\" and\"\\\" in C#? I have string which looks like this:

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

精彩评论

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