【问题标题】:Splitting a String with two criteria使用两个条件拆分字符串
【发布时间】:2012-12-21 15:10:06
【问题描述】:

我有一个如下所列的字符串。

字符串示例 = "class0 .calss1 .class2 .class3.class4 .class5 class6 .class7";

我需要从这个示例字符串中创建一个 WORDS 列表。

一个 WORD 是一个以句点开头并以:结尾的字符串:

  1. 空格或
  2. 另一个时期或
  3. 字符串结束

注意:这里的重点是 - 拆分基于两个标准 - 句点和空格

我有以下程序。它工作正常。但是,有没有使用LINQRegular Expressions 的更简单/更高效/简洁的方法?

代码

        List<string> wordsCollection = new List<string>();
        string sample = " class0 .calss1 .class2 .class3.class4  .class5 class6 .class7";

        string word = null;

        int stringLength = sample.Length;
        int currentCount = 0;

        if (stringLength > 0)
        {
            foreach (Char c in sample)
            {

                currentCount++;
                if (String.IsNullOrEmpty(word))
                {
                    if (c == '.')
                    {
                        word = Convert.ToString(c);
                    }
                }
                else
                {

                    if (c == ' ')
                    {
                        //End Criteria Reached
                        word = word + Convert.ToString(c);
                        wordsCollection.Add(word);
                        word = String.Empty;
                    }
                    else if (c == '.')
                    {
                        //End Criteria Reached
                        wordsCollection.Add(word);
                        word = Convert.ToString(c);
                    }
                    else
                    {
                        word = word + Convert.ToString(c);
                        if (stringLength == currentCount)
                        {
                            wordsCollection.Add(word);
                        }
                    }
                }

            }
        }

结果

        foreach (string wordItem in wordsCollection)
        {
            Console.WriteLine(wordItem);

        }

参考:

  1. Splitting up a string, based on predicate
  2. Is there a better way to get sub-sequences where each item matches a predicate?
  3. Linq based generic alternate to Predicate<T>?

【问题讨论】:

标签: c# linq


【解决方案1】:

您可以使用正则表达式来做到这一点。

代码

Regex regex = new Regex(@"\.[^ .]+");
var matches = regex.Matches(sample);
string[] result = matches.Cast<Match>().Select(x => x.Value).ToArray();

在线查看:ideone

结果

.calss1
.class2
.class3
.class4
.class5
.class7

正则表达式解释

\。匹配一个点 [^. ]+ 否定字符类 - 除了空格或点之外的任何内容(至少一个)

相关

【讨论】:

  • +1 我通常不赞成使用正则表达式来拆分字符串,但这是最好的方法。
  • 谢谢。你能解释一下“尽可能少”是什么意思吗
  • @Lijo:正则表达式默认是贪婪的。 ? 修饰符使 * lazy
  • 也感谢您的参考regular-expressions.info/repeat.html#lazy。如果您可以使用这些详细信息和链接更新答案,那就太好了
  • @Lijo:考虑了一下,更新为更简单的正则表达式。
【解决方案2】:
string sample = " class0 .calss1 .class2 .class3.class4  .class5 class6 .class7";

string[] words = sample.Split(new char[] {'.'}).Skip(1).Select(x=> 
            "." + x.Split(new char[] {' '})[0].Trim()).ToArray();

EDIT 错过了列表部分:

List<string> words = sample.Split(new char[] {'.'}).Skip(1).Select(x=> 
            "." + x.Split(new char[] {' '})[0].Trim()).ToList();

【讨论】:

    【解决方案3】:

    您需要保留 .和空间?

    如果没有,你可以使用:

    sample.split(new char[]{" ", "."}).ToList();
    

    这会给你一个字符串列表。

    【讨论】:

      【解决方案4】:
      string sample = " class0 .calss1 .class2 .class3.class4 .class5 class6 .class7";
      sample = Regex.Replace(sample, " ", String.Empty);
      string[] arr = sample.Split(new char[] { '.' });
      

      【讨论】:

      • 你没有看到 class0 出现在结果中吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-30
      • 1970-01-01
      • 2018-01-06
      相关资源
      最近更新 更多