【问题标题】:How to separate paragraphs in a string?如何分隔字符串中的段落?
【发布时间】:2013-01-28 15:04:58
【问题描述】:

我试图获取一个由几段组成的多行字符串并将其拆分为几个单独的文本。

我意识到每当我跳过一行时,都会有一个 \n\r 序列。之后我认为每个新行都以 \n 开头并以 \r 结尾。为此,我编写了以下代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication15
{
   class Program
   {
    struct ParagraphInfo
    {
        public ParagraphInfo(string text)
        {
            int i;
            Text = text;
            i = text.IndexOf('.');
            FirstSentence = text.Substring(0, i);
        }

        public string Text, FirstSentence;
    }

    static void Main(string[] args)
    {
        int tmp = 0;
        int tmp1 = 0;
        string MultiParagraphString = @"AA.aa.

BB.bb.

CC.cc.

DD.dd.

EE.ee.";

        List<ParagraphInfo> Paragraphs = new List<ParagraphInfo>();

        Regex NewParagraphFinder = new Regex(@"[\n][\r]");
        MatchCollection NewParagraphMatches = NewParagraphFinder.Matches(MultiParagraphString);


        for (int i = 0; i < NewParagraphMatches.Count; i++)
        {
            if (i == 0)
            {
                Paragraphs.Add(new ParagraphInfo((MultiParagraphString.Substring(0, NewParagraphMatches[0].Index))));
            }
            else if (i == (NewParagraphMatches.Count - 1))
            {
                tmp = NewParagraphMatches[i].Index + 3;
                tmp1 = MultiParagraphString.Length - NewParagraphMatches[i].Index - 3;
                Paragraphs.Add(new ParagraphInfo(MultiParagraphString.Substring(tmp, tmp1)));
            }
            else
            {
                tmp = NewParagraphMatches[i].Index + 3;
                tmp1 = NewParagraphMatches[i + 1].Index - NewParagraphMatches[i].Index+3;
                Paragraphs.Add(new ParagraphInfo(MultiParagraphString.Substring(tmp, tmp1)));
            }
        }

        Console.WriteLine(MultiParagraphString);
        foreach (ParagraphInfo Paragraph in Paragraphs)
        {
            Console.WriteLine(Paragraph.Text);

        }


    }
}
}

当我将段落的每个成员一个接一个地打印在整个文本旁边时,出现了一些相当奇怪的东西。段落列表的输出是这样的:

AA.aa.


CC.cc.

DD。


DD.dd.

EE。


EE.ee.


我不明白为什么这种情况一直发生,而且我不明白为什么每次输出都如此不同。

很抱歉,如果它是一团糟,但我真的需要一些帮助。如果有人有更好的想法,请随时分享。

【问题讨论】:

  • 仅供参考,在 Windows 上,行终止符是 \r\n,而不是 \n\r。
  • 另外,两者都在行的end,而不是在beginning。 .NET 具有独立于平台的Environment.NewLine,用于跨平台处理换行/回车序列,这就是你应该做的替换,而且,正则表达式,.. 只是,为什么?
  • 听起来你假设行以 \n 开头并以 \r 结尾是错误的 - 不是 \r\n 只是 CRLF 意味着行应该以 \r\n 结尾- 这样就可以拆分var paragraphs = someString.Split(Environment.NewLine)?

标签: c# regex string escaping string-literals


【解决方案1】:

您可以尝试以下方法:

MultiParagraphString.Split(new [] {Environment.NewLine}, 
           StringSplitOptions.RemoveEmptyEntries);

这将返回一个IEnumerable&lt;String&gt;。如果您想将它们转换为您的结构,只需使用Select

MultiParagraphString.Split(new [] {Environment.NewLine}, 
           StringSplitOptions.RemoveEmptyEntries)
          .Select(s => new ParagraphInfo(s)).ToList();

【讨论】:

  • 你能解释一下新的 [] {Environment.NewLine 部分吗?我没明白,你想把什么传递给方法?我一般不太了解这个功能,这次 MSDN 也没有什么特别的用处..
  • Environment.NewLine 返回特定系统的行尾 (bit.ly/1267zb7)。 String.Split() 不接受单个字符串作为分隔符 - 只有数组 (bit.ly/1267KD5) 是 new []{} 的来源。
【解决方案2】:

我认为每个新行都以 \n 开头并以 \r 结尾

没有。 \r\n 是用于在 Windows(和其他非 Unix)系统中指示新行的两个字符序列。它并不表示段落的“开始”和“结束”。

要分成段落,可以使用string.Split()

string[] paragraphs = MultiParagraphString.Split(new string[]{"\r\n"},
                           StringSplitOptions.RemoveEmptyEntries);

【讨论】:

【解决方案3】:
 string text = richTextBox1.Text;

您可以使用以下命令忽略段落:

text = text.Replace((char)10, ' ');

您可以使用以下方法检测段落:

string[] words = s.split('');
foreach (string word in words)
{
if (word.Contains((char)10))
{
MessageBox.Show("A paragraph is here (with brillant English accent)");
}

注意:此代码仅在段落由文本上的 enter 键分隔时才有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    相关资源
    最近更新 更多