All Blond
All Blond

Reputation: 1253

How to remove or replace "\" in C#

Does anyone knows how to remove/replace 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.

Upvotes: 1

Views: 284

Answers (2)

Christopher Currens
Christopher Currens

Reputation: 30695

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).

Upvotes: 16

shamim
shamim

Reputation: 6770

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]);

Upvotes: 0

Related Questions