C尖锐分隔符

时间:2010-07-30 11:46:23

标签: c#-3.0

在给定的句子中,我想分成10个字符串。字符串中的最后一个字不应该是不完整的。应根据空格或,.

进行拆分

例如:  this is ram.he works at mcity.

现在10个字符的子串是, this is ra. 但输出应该是, this is. 最后一句话不应该是不完整的

2 个答案:

答案 0 :(得分:2)

您可以使用正则表达式来检查匹配后的字符是否为单词字符:

string input = "this is ram.he";

Match match = Regex.Match(input, @"^.{0,10}(?!\w)");
string result;
if (match.Success)
{
    result = match.Value;
}
else
{
    result = string.Empty;
}

结果:

this is

另一种方法是按令牌构建字符串向上令牌,直到添加另一个令牌超过字符限制:

StringBuilder sb = new StringBuilder();
foreach (Match match in Regex.Matches(input, @"\w+|\W+"))
{
    if (sb.Length + match.Value.Length > 10) { break; }
    sb.Append(match.Value);
}
string result = sb.ToString();

答案 1 :(得分:0)

不确定这是否是您正在寻找的东西。请注意,这可以做得更干净,但应该让你开始...(可能想使用StringBuilder而不是String)。

    char[] delimiterChars = { ',', '.',' ' };
    string s = "this is ram.he works at mcity.";

    string step1 = s.Substring(0, 10);   // Get first 10 chars

    string[] step2a = step1.Split(delimiterChars);    // Get words
    string[] step2b = s.Split(delimiterChars);        // Get words

    string sFinal = "";

    for (int i = 0; i < step2a.Count()-1; i++)     // copy count-1 words
    {
        if (i == 0)
        {
            sFinal = step2a[i];
        }
        else
        {
            sFinal = sFinal + " " + step2a[i];
        }
    }

    // Check if last word is a complete word.

    if (step2a[step2a.Count() - 1] == step2b[step2a.Count() - 1])
    {
        sFinal = sFinal + " " + step2b[step2a.Count() - 1] + ".";
    }
    else
    {
        sFinal = sFinal + ".";
    }