简体   繁体   English

删除字符串的特定部分

[英]Remove a specific part of a string

I want to go through a text file and remove specific parts of the string. 我想浏览一个文本文件并删除字符串的特定部分。 In this case I want to remove the path: 在这种情况下,我要删除路径:

PERFORMER "Charles Lloyd"

TITLE "Mirror"

FILE "Charles Lloyd\[2010] Mirror\01. I Fall In Love Too Easily.wav" WAVE

TRACK 01 AUDIO

FILE "Charles Lloyd\[2010] Mirror\02. Go Down Moses.wav" WAVE

to

PERFORMER "Charles Lloyd"

TITLE "Mirror"

FILE "01. I Fall In Love Too Easily.wav" WAVE //here are the changes

TRACK 01 AUDIO

FILE "02. Go Down Moses.wav" WAVE //here are the changes

I tried out things like: (given the string s which contains the whole text) 我尝试了以下操作:(给定包含整个文本的字符串s)

s = s.Remove(s.IndexOf("FILE") + 5, (s.IndexOf("\\") + 1) - s.IndexOf("FILE") - 5);

and repeat this function to remove the part between "FILE " " and the following backslash 并重复此功能以删除“ FILE”和以下反斜杠之间的部分

It removes the part correctly, but I would have to manually adjust the number of times it has to run this function (run once for every backslash per line). 它可以正确删除该零件,但是我必须手动调整它必须运行此功能的次数(每行每个反斜杠运行一次)。 But this algorithm lacks flexibility and I don't know how to make it approach the next line that starts with "FILE" and begin the procedure again... 但是此算法缺乏灵活性,我不知道如何使它接近以“ FILE”开头的下一行并重新开始该过程。

If all your text is one string variable, you could first split it, and than do replacements for all strings and than join again (assume your text is variable lines ): 如果所有文本都是一个字符串变量,则可以先将其拆分,然后替换所有字符串,然后再进行合并(假设文本是可变lines ):

var strings = lines.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
var replacedStrings = new List<string>();
foreach (var s in strings)
{       
    string replaced;
    if (s.StartsWith("FILE"))
    {
        var sWithoutFile = s.Substring(5);
        replaced = s.Substring(0, 6) +
                    sWithoutFile.Substring(sWithoutFile.LastIndexOf("\\") + 1);
    }
    else
    {
        replaced = s;
    }
    replacedStrings.Add(replaced);
}   
var result = string.Join(Environment.NewLine, replacedStrings);

Assuming that your line always start with FILE " and EndsWith " WAVE , you can use System.Io.Path.GetFilename() Function to achieve this: 假设您的行始终以FILE "和EndsWith " WAVE开头,则可以使用System.Io.Path.GetFilename()函数来实现此目的:

If str.StartsWith("File"){

string strResult = "FILE """ + IO.Path.GetFileName(str.Substring(6,str.Length - 12)) + """ WAVE";

}

Example: 例:

FILE "Charles Lloyd\[2010] Mirror\01. I Fall In Love Too Easily.wav" WAVE

Result: 结果:

FILE "01. I Fall In Love Too Easily.wav" WAVE

You can read more about this Function in this MSDN article 您可以在此MSDN文章中阅读有关此功能的更多信息。

Split the array using character \\ and store the last element in the array back to the string. 使用字符\\拆分数组,并将数组中的最后一个元素存储回字符串。

For Example something like this: 例如下面的例子:

array = file.split('\')

file = array[array.size - 1];

What about Regular Expressions. 那正则表达式呢。

using System;
using System.Text.RegularExpressions;

class RemovePaths
{
    static void Main()
    {
        string input = @"
PERFORMER ""Charles Lloyd""

TITLE ""Mirror""

FILE ""Charles Lloyd\[2010] Mirror\01. I Fall In Love Too Easily.wav"" WAVE

TRACK 01 AUDIO

FILE ""Charles Lloyd\[2010] Mirror\02. Go Down Moses.wav"" WAVE";

        string test = @"
PERFORMER ""Charles Lloyd""

TITLE ""Mirror""

FILE ""01. I Fall In Love Too Easily.wav"" WAVE

TRACK 01 AUDIO

FILE ""02. Go Down Moses.wav"" WAVE";

        Regex rgx = new Regex(@"(?<=\"").*\\(?=.+\"")");
        string result = rgx.Replace(input, "");
        Console.WriteLine(result == test ? "Pass" : "Fail");
    }
}

Breakdown of the RegEx... RegEx的细分...

(?<=\\"") <--- must start with a double-quote but be excluded using (?<=...) (?<= \\“”)<---必须以双引号开头,但使用(?<= ...)排除在外

. \\ <--- match any text up to and including a "\\". \\ <---匹配任何文本,最多包括“ \\”。 note: . 注意: 。 matches anything 匹配任何东西

(?=.+\\"") <--- skip at least one character(.+) and it must end with a double-quote(\\"). (?=。+ \\“”)<---至少跳过一个字符(。+),并且必须以双引号(\\“)结尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM