【问题标题】:How can I get the substring after a specifc word and on another word in a string如何在特定单词之后和字符串中的另一个单词上获取子字符串
【发布时间】:2018-09-30 15:40:04
【问题描述】:

假设我有字符串“Old Macdonald has a farm and on”。 我想得到子字符串“有一个农场”。 我想在工作“麦克唐纳”之后得到任何东西,直到“农场”这个词

所以字符串中的常量是:

“Macdonald” - 我不想包含在子字符串中

"farm" - 我想要包含在子字符串中的结束词

我一直在尝试合并 indexof 等功能,但似乎无法使其工作

【问题讨论】:

  • 你可以使用正则表达式。 link ...或者您可以将整个字符串拆分为由空格分隔的子字符串..并在“MacDonald”之后获取字符串直到“farm”并将它们连接起来。

标签: c# .net substring indexof between


【解决方案1】:

当然,Linq 不能从解决方案列表中排除:

string SearchString = "Old Macdonald had a farm and on";
string fromPattern = "Macdonald";
string toPattern = "farm";

string result = string.Join(" ", SearchString
                      .Split((char)32)
                      .SkipWhile(s => s != fromPattern)
                      .Skip(1)
                      .TakeWhile(s => s != toPattern)
                      .Append(toPattern));

【讨论】:

    【解决方案2】:

    正如我在评论中提到的,我建议使用 Regex(TheGeneral 建议的方式)。但是还有另一种方法。

    将其添加为解决方法

            string input = "Old Macdonald had a farm and on";
            List<string> words = input.Split(" ".ToCharArray()).ToList();
            string finalString = "";
    
            int indexOfMac = words.IndexOf("Macdonald");
            int indexOfFarm = words.IndexOf("farm");
    
    
            if (indexOfFarm != -1 && indexOfMac != -1 &&  //if word is not there in string, index will be '-1' 
                indexOfMac < indexOfFarm)  //checking if 'macdonald' comes before 'farm' or not
    
            {
                //looping from Macdonald + 1 to farm, and make final string
                for(int i = indexOfMac + 1; i <= indexOfFarm; i++)
                {
                    finalString += words[i] + " ";
                }
            }
            else
            {
                finalString = "No farms for you";
            }
    
            Console.WriteLine(finalString);
    

    【讨论】:

    • 最好将这些字符串的索引保存在 int 变量中,而不是每次都使用魔术字符串
    【解决方案3】:

    您可以使用 IndexOf() 和 Substring() 作为

    string input = "Old Macdonald had a farm and on";
    int pos=input.IndexOf("Macdonald");
    if(pos > -1){
      string s2=input.Substring(pos);
      string second="farm";
      int secLen=second.Length;
      int pos2=s2.IndexOf(second);
      if(pos2 > -1){
        Console.WriteLine("Substring: {0}", s2.Substring(0,pos2+secLen));
      }
    }
    

    【讨论】:

      【解决方案4】:

      您可以将RegEx(?&lt;=Macdonald\s).*(?=\sand) 一起使用

      说明

      • 正面观察 (?&lt;=Macdonald\s)
        • Macdonald 匹配字符 Macdonald 字面意思
        • \s 匹配任何空白字符
      • .* 匹配任何字符(行终止符除外)

        • * Quantifier — 匹配 0 次到无限次,尽可能多次,按需回馈(贪婪)
      • 正向预测 (?=\sand)

        • \s 匹配任何空白字符并匹配字符 and 字面意思

      示例

      var input = "Old Macdonald had a farm and on";
      var regex = new Regex(@"(?<=Macdonald\s).*(?=\sand)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
      var match = regex.Match(input);
      if (match.Success)
      {
          Console.WriteLine(match.Value);
      }
      else
      {
          Console.WriteLine("No farms for you");
      }
      

      输出

      had a farm
      

      Full Demo Here

      【讨论】:

      • 我会赞成这个答案,但我最近赞成你的几个答案,现在我的投票有可能被视为“有针对性的投票”:D
      • @Amit Hah,目标远离 :)
      • 我正要写同样的答案。无论如何+1。您能否解释一下后向正向和前向正向,以便 OP 以更好的方式理解正则表达式。
      • 不幸的是,我正在使用的应用程序不允许我使用正则表达式或控制台,因此无法对此进行测试。我已经能够以常规方式做到这一点了。
      • @AusGamer 看看我的回答
      猜你喜欢
      • 2014-10-03
      • 2022-08-02
      • 2015-06-22
      • 1970-01-01
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      • 2018-08-14
      • 1970-01-01
      相关资源
      最近更新 更多