【问题标题】:Replace a specific sub-string at specific position using LINQ使用 LINQ 替换特定位置的特定子字符串
【发布时间】:2018-11-09 15:58:56
【问题描述】:

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

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 中做到这一点?

【问题讨论】:

  • 那么Pos对应字符串中的位置?这意味着如果exp. 不在第 30 位,它应该被忽略(即不被替换)?
  • 是的,没错
  • 你当前的代码有效吗?
  • 是第一个“exp”。第 8 位?
  • 你应该提供一个清晰的列表,你已经尝试过什么,以及一个清晰的输入、输出。我建议扩展您的问题,以使人们更容易理解这个问题。仅供参考。通常,问题越漂亮,答案就越漂亮。一半的问题=一半的答案。

标签: c# string linq replace


【解决方案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);
    }

【讨论】:

  • 不要使用.ForEach(),只使用foreach循环。
  • 还有。他没有具体说明你需要占抵消吗?你简单推断的是谁。但他没有提供足够的背景来警告它。除此之外,还可以完成这项工作。但是,为什么不简单地解决最初的问题,即他提供了不正确的起始位置,也只是从它们中减去一个呢?假设您为知道自己在做什么的人编程,并且他们按照您的预期从零开始计数。这个答案会搞砸他们的代码。
  • 有一个服务会返回缩写列表,其中包括单词应该被替换的顺序,因此颠倒列表可以避免重新调整位置。
  • 我的问题已经得到解答,谢谢。最初我想知道使用 LINQ 表达式进行字符串操作的任何超级有效的方法。
【解决方案2】:

请记住,如果您直接在源中替换缩写词,您要替换的下一个缩写词的位置索引将与您在开始时声明的位置索引不同。所以我认为你需要一些辅助变量来做到这一点。

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

编辑 - 将 LINQ ForEach 替换为常规的 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);
    }
}

【讨论】:

  • 如果 LINQ 不喜欢内部逻辑,因为不支持从 System.String 派生的汇编方法,我们总是可以使用标准的 foreach 代替。
  • 您应该只使用常规的foreach 循环。 List.ForEach() 总是一个坏主意
  • @maccettura 是的,我仍然不清楚为什么 LINQ 不能访问/不引用其他基本汇编方法。
猜你喜欢
  • 1970-01-01
  • 2017-04-10
  • 2014-08-19
  • 2015-10-18
  • 2011-01-15
  • 2017-09-17
  • 2021-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多