使用LINQ替换特定位置的特定子字符串

时间:2018-05-30 17:49:13

标签: c# string linq replace

我有一个缩写和位置列表,我想要替换特定的缩写而不是所有的缩写。

public class Abbreviation
{
    public int Pos { get; set; }
    public string ShortName { get; set; }
    public string LongName { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        List<Abbreviation> abbreviations = new List<Abbreviation>();
        abbreviations.Add(new Abbreviation() { Pos = 28, ShortName = "exp.", LongName = "expression" });
        abbreviations.Add(new Abbreviation() { Pos = 38, ShortName = "para.", LongName = "paragraph" });
        abbreviations.Add(new Abbreviation() { Pos = 51, ShortName = "ans.", LongName = "answer" });

        string test = "What is exp.? This is a test exp. in a para. contains ans. for a question";
        abbreviations.ForEach(x => ...); // Need a LINQ expression
        Console.WriteLine(test);
    }
}

如何在LINQ中执行此操作?

2 个答案:

答案 0 :(得分:1)

好的,这个怎么样:

  public static void Main()
    {
        //var search = new Regex(@"spell\ correction", RegexOptions.IgnoreCase);
        //var data = "this is a teit that i see that";

        List<Abbreviation> abbreviations = new List<Abbreviation>();
        abbreviations.Add(new Abbreviation() { Pos = 29, ShortName = "exp.", LongName = "expression" });
        abbreviations.Add(new Abbreviation() { Pos = 39, ShortName = "para.", LongName = "paragraph" });
        abbreviations.Add(new Abbreviation() { Pos = 54, ShortName = "ans.", LongName = "answer" });

        string test = "What is exp.? This is a test exp. in a para. contains ans. for a question";
        var offset = 0;
        abbreviations.ForEach(x => {

            if(test.Substring(x.Pos+ offset, x.ShortName.Length)==x.ShortName)
            {

                test = test.Remove(x.Pos + offset, x.ShortName.Length);
                test= test.Insert(x.Pos  + offset, x.LongName);
                offset += x.LongName.Length - x.ShortName.Length ;
            }

        }); // Need a LINQ expression
        Console.WriteLine(test);
    }

答案 1 :(得分:0)

请记住,如果您直接在源处替换缩写,则您替换的下一个缩写的位置索引将与您在开头声明的缩写的位置索引不同。所以我看待你的方式需要一些辅助变量才能做到。

此外,您的起始索引有点偏离标记。

编辑 - 通过常规ForEach替换LINQ foreach,以便您不会使用它不支持的方法发生任何冲突,例如String.IsNullOrEmpty()

public class Abbreviation
{
    public int Pos { get; set; }
    public string ShortName { get; set; }
    public string LongName { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        List<Abbreviation> abbreviations = new List<Abbreviation>();
        abbreviations.Add(new Abbreviation() { Pos = 29, ShortName = "exp.", LongName = "expression" });
        abbreviations.Add(new Abbreviation() { Pos = 39, ShortName = "para.", LongName = "paragraph" });
        abbreviations.Add(new Abbreviation() { Pos = 54, ShortName = "ans.", LongName = "answer" });

        string test = "What is exp.? This is a test exp. in a para. contains ans. for a question";

        string temp = "";
        int tempPos = 0;

        foreach (Abbreviation x in abbreviations)
        {
            if (!String.IsNullOrEmpty(test) && (test.Length >= x.Pos + x.ShortName.Length) && test.Substring(x.Pos, x.ShortName.Length) == x.ShortName)
            {
                temp += test.Substring(tempPos, x.Pos) + x.LongName;
            }
            tempPos = x.Pos + x.ShortName.Length;
        }
        Console.WriteLine(temp);
    }
}