开发者

Selecting a specific part of a string C#

I am trying to create a new string, which removes certain characters from an existing string e.g.

string path = "C:\test.txt"

so string "pathminus" wil开发者_如何学运维l take out the "C:\" e.g.

string pathminus = "test.txt"


Use Path.GetFileName

Eg:

string pathminus = Path.GetFileName(path);


There's so many ways that you can remove the a certain part of a string. These are a couple of ways to do it:

var path = @"C:\test.txt";
var root = @"C:\";

Using string.Remove()

var pathWithoutRoot = path.Remove(0, root.Length);
Console.WriteLine(pathWithoutRoot);                // prints test.txt

Using string.Replace()

pathWithoutRoot = path.Replace(root, "");
Console.WriteLine(pathWithoutRoot);                // prints test.txt

Using string.Substring()

pathWithoutRoot = path.Substring(root.Length);
Console.WriteLine(pathWithoutRoot);                // prints test.txt

Using Path.GetFileName()

pathWithoutRoot = Path.GetFileName(path);
Console.WriteLine(pathWithoutRoot);                // prints test.txt

You can also use Regular Expression to find and replace parts of a string, this is a little bit harder though. You can read on MSDN on how to use Regular Expressions in C#.

Here's a complete sample on how to use string.Remove(), string.Replace(), string.Substring(), Path.GetFileName() and Regex.Replace():

using System;
using System.IO;
using System.Text.RegularExpressions;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            var path = @"C:\test.txt";
            var root = @"C:\";

            var pathWithoutRoot = path.Remove(0, root.Length);
            Console.WriteLine(pathWithoutRoot);

            pathWithoutRoot = Path.GetFileName(path);
            Console.WriteLine(pathWithoutRoot);

            pathWithoutRoot = path.Replace(root, "");
            Console.WriteLine(pathWithoutRoot);

            pathWithoutRoot = path.Substring(root.Length);
            Console.WriteLine(pathWithoutRoot);

            var pattern = "C:\\\\";
            var regex = new Regex(pattern);

            pathWithoutRoot = regex.Replace(path, "");
            Console.WriteLine(pathWithoutRoot);
        }
    }
}

It all depends on what you want to do with the string, in this case you might want to remove just C:\ but next time you might want to do other types of operations with the string, maybe remove the tail or such, then the above different methods might help you out.


The string class offers all sorts of ways to do this.

If you want to change "C:\test.txt" to "test.txt" by removing the first three characters:

path.Substring(3);

If you want to remove "C:\" from anywhere in the string:

path.Replace("C:\", "");

Or if you specifically want the filename, regardless of how long the path is:

Path.GetFileName(path);

Depending on your intentions, there are many ways to do it. I prefer using the static class Path.


For this specific example, I would look into the Path Class. For your example, you can just call:

string pathminus = Path.GetFileName(path);


Have you looked at the Substring method?


If the string is actually a file path, use the Path.GetFileName method to get the file name part of it.


path.SubString(path.IndexOf('\'))


You want System.Text.RegularExpressions.Regex but exactly what are you trying do here?

In its simplest form:

    [TestMethod]
    public void RemoveDriveFromPath()
    {
        string path = @"C:\test.txt";

        Assert.AreEqual("test.txt", System.Text.RegularExpressions.Regex.Replace(path, @"^[A-Z]\:\\", string.Empty));
    }

Are you just trying to get the file name of a file without the path?

If so do this instead:

    [TestMethod]
    public void GetJustFileName()
    {
        string path = @"C:\test.txt";

        var fileInfo = new FileInfo(path);

        Assert.AreEqual("test.txt", fileInfo.Name);
    }


For more general strings, use the string.Split(inputChar), which takes a character as a parameter and splits the string into a string[] wherever that inputChar is found.

string[] stringArr = path.Split('\\'); // need double \ because \ is an escape character
// you can now concatenate any part of the string array to get what you want. 
// in this case, it's just the last piece
string pathminus = stringArr[stringArr.Length-1];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜