【问题标题】:C# common substring list/extractionC#公共子串列表/提取
【发布时间】:2013-01-22 12:49:29
【问题描述】:

我的目标是遍历字符串数据库并在每次出现每个子字符串时获取计数。换句话说,我需要从字符串中提取所有可能的单词组合。

例如,输入可能是"this is the first string"

我想提取"this is""is the""the first""first string""this is the""is the first""the first string""this is the first""is the first string"

我只需要从左到右,总是按顺序。

我不确定从哪里开始。我已经有了读取数据库并保存到列表中的代码,只需要知道如何根据空格字符提取所有可能的子字符串。

【问题讨论】:

  • 在任何三个或更多单词的子串都必须包含两个单词的子串的前提下,只需要实际搜索包含两个单词的子串即可。
  • shure 组合“这是第一个字符串”不是允许的组合? ...无论如何,这看起来像一个递归算法
  • 刚刚回答了一个类似的问题,但是只需要 exactly n 单词的短语,使用正则表达式,我很好奇是否有正则表达式可以解决这个问题。

标签: c# .net regex string substring


【解决方案1】:

以下方法构建字符串中所有空格的索引列表(加上名义开始和结束空格),然后返回每个有序索引对之间的子字符串:

static IEnumerable<string> SpaceDelimitedSubstrings(string input)
{
    List<int> indices = new List<int> { -1 };
    int current = -1;
    while ((current = input.IndexOf(' ', current + 1)) > -1)
    {
        indices.Add(current);
    }
    indices.Add(input.Length);

    int minLength = 1;
    for (int i = 0; i < indices.Count - minLength; i++)
        for (int j = i + minLength; j < indices.Count; j++)
            yield return input.Substring(indices[i] + 1, indices[j] - indices[i] - 1);
}

如下调用

string input = "this is the first string";
foreach (var s in SpaceDelimitedSubstrings(input))
{
    Console.WriteLine(s);
}

它给了

minLength 更改为2 将删除单字返回。

【讨论】:

    【解决方案2】:
        List<string> WordCombinations(string phrase)
        {
            List<string> combinations = new List<string>();
    
            string[] words = phrase.Split();
    
            // We want all 2 word combinations, then 3, then 4, ...
            for (int take = 2; take < words.Length; take++)
            {
                // Start with the first word, then second, then ...
                for (int skip = 0; skip + take <= words.Length; skip++)
                {
                    combinations.Add(string.Join(" ", words.Skip(skip).Take(take).ToArray()));
                }
            }
    
            return combinations;
        }
    

    【讨论】:

    • @Beachwalker: 有一个string.Join 方法采用IEnumerable&lt;T&gt;: msdn.microsoft.com/en-us/library/dd992421.aspx;原始代码是有效的。
    • @MichaeEdenfield 啊,尝试使用 3.5 框架。 IEnumerable 的重载适用于 4.0 及更高版本。
    【解决方案3】:

    一种方法可能是:

    myString.Split()
    

    如果您不提供任何参数,它将忽略空格字符(制表符、换行符(s.a. Environment.NewLine)等)分割字符串。

    当您拥有所有子字符串时,您可以轻松地通过它们连接。 请记住,这可能会很慢,因为您每次都必须遍历字符串才能提取子字符串。

    【讨论】:

      【解决方案4】:

      您可以使用String.Split() 将字符串解析为标记。然后,您可以组合这些标记以创建您想要的组合。

      【讨论】:

        【解决方案5】:

        如果使用String.Split()? 那么你有所有单个单词,只需要另外的可能组合。

        执行上述操作的简单示例:

                string input = "this is the first string";
        
                var items = input.Split(' ');
                var result = new List<string>();
        
                // this gets only 2-word-combinations
                for (var i = 0; i < items.Count() - 1; i++)
                {
                    result.Add(items[i] + " " + items[i + 1]);
                }
        
                // from this point: search the 3-words etc. or put this in recursion
        

        【讨论】:

          猜你喜欢
          • 2022-08-15
          • 1970-01-01
          • 2021-05-19
          • 1970-01-01
          • 1970-01-01
          • 2020-11-21
          • 1970-01-01
          • 1970-01-01
          • 2018-10-21
          相关资源
          最近更新 更多