劈开两个标准的String [英] Splitting a String with two criteria

查看:173
本文介绍了劈开两个标准的String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下所列的字符串。




字符串样本=class0 .calss1 .class2 .class3.class4 .class5 CLASS6。 class7;




我需要从这个样本字符串创建一个单词列表。



一个WORD是一个周期开始,以一个字符串:




  1. 空格或

  2. 另一段或

  3. 字符串的结尾



注意:这里的关键点是 - 分离是基于两个标准 - 一个句点和一个空格



我有下面的程序。它工作正常。然而,有一个简单/更高效/简洁的使用方法 LINQ 正则表达式



代码

 列表<串GT; wordsCollection =新的List<串GT;(); 
串样本=class0 .calss1 .class2 .class3.class4 .class5 CLASS6 .class7

串词= NULL;

INT stringLength = sample.Length;
INT CURRENTCOUNT = 0;

如果(stringLength大于0)
{
的foreach(夏亚℃的样品)
{

CURRENTCOUNT ++;
如果(String.IsNullOrEmpty(字))
{
如果(C =='')
{
字= Convert.ToString(C);
}
}
,否则
{

如果(C =='')
{
// END标准达成
字=字+ Convert.ToString(C);
wordsCollection.Add(字);
字=的String.Empty;
}
,否则如果('。'C ==)
{
// END标准达成
wordsCollection.Add(字);
字= Convert.ToString(C);
}
,否则
{
字=字+ Convert.ToString(C);
如果(stringLength == CURRENTCOUNT)
{
wordsCollection.Add(字);
}
}
}

}
}

结果

 的foreach(在wordsCollection串wordItem)
{
控制台.WriteLine(wordItem);

}





参考:




  1. 分裂的字符串,基于谓词

  2. 有没有更好的办法让每个项目一个谓词匹配子序列?

  3. Linq基于通用候补谓词< T> ;?


解决方案

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



代码

 正则表达式的regex ​​=新的正则表达式(@\。[^。] + ); 
VAR匹配= regex.Matches(样品);
的String []结果= matches.Cast<比赛方式>()选择(X => x.Value).ToArray();

请参阅它的在线工作:的 ideone



结果

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

正则表达式的说明

 
\。匹配一个点
[^。 ] +负值字符类 - 从太空或点(至少一个)

东西除了

相关




I have a string as listed below.

string sample = " class0 .calss1 .class2 .class3.class4 .class5 class6 .class7";

I need to create a list of WORDS from this sample string.

A WORD is a string that starts with a period and ends with:

  1. a space or
  2. another period or
  3. end of string

Note: The key point here is - the splitting is based on two criteria - a period and a blank space

I have following program. It works fine. However, is there a simpler/more efficient/concise approach using LINQ or Regular Expressions?

CODE

        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);
                        }
                    }
                }

            }
        }

RESULT

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

        }

Reference:

  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>?

解决方案

You can do this with a regular expression.

Code

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

See it working online: ideone

Result

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

Explanation of Regular Expression

\.      Match a dot
[^. ]+  Negative character class - anything apart from space or dot (at least one)

Related

这篇关于劈开两个标准的String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆