【问题标题】:Split string based on count and words using C#使用 C# 根据计数和单词拆分字符串
【发布时间】:2016-02-06 13:01:38
【问题描述】:

我需要在单词上拆分字符串,每行应该有 25 个字符。例如:

字符串 ORIGINAL_TEXT = "请编写一个程序,将这段文本分成小块。每个块的最大长度应为 25"

输出应该是:

“请写一个程序”,

“打破了这段文字”,

“成小夹头。每个”,

"块应该有一个",

"最大长度为 25"

我尝试使用 substring - 但它破坏了像

这样的词

请写一个程序” - 错误

请编写程序” - 正确

请编写一个程序 - 只有 23 个字符,可以多 2 个字符,但会破坏 that 这个词。

string[] splitSampArr = splitSamp.Split(',', '.', ';');
string[] myText = new string[splitSampArr.Length + 1];

int i = 0;
foreach (string splitSampArrVal in splitSampArr)
{
    if (splitSampArrVal.Length > 25)
    {
        myText[i] = splitSampArrVal.Substring(0, 25);
        i++;
    }
    myText[i] = splitSampArrVal;

    i++;
}

【问题讨论】:

  • 你用.(和,;)分割字符串,但是作为输出,你说你需要"into small chucks. Each"里面有一个点?

标签: c# regex string split substring


【解决方案1】:

您可以通过以下方式实现:

@"(\b.{1,25})(?:\s+|$)"

regex demo

这个正则表达式匹配并捕获除换行符(.)以外的任何字符并将其捕获到第 1 组中(因此,我们只开始匹配整个单词),出现 1 到 25 次(感谢限制量词{1,25}),然后匹配1个或多个空白字符(\s+)或字符串结尾($)。

code demo

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class Test
{
    public static void Main()
    {
        var str = "Please write a program that breaks this text into small chucks. Each chunk should have a maximum length of 25 ";
        var chunks = Regex.Matches(str, @"(\b.{1,25})(?:\s+|$)")
                 .Cast<Match>().Select(p => p.Groups[1].Value)
                 .ToList();
        Console.WriteLine(string.Join("\n", chunks));
    }
}

【讨论】:

  • 我发现这个正则表达式有问题。如果我给长度让我们说 5 那么它只匹配最多 5 个长度的单词。前任。 “这是我的发票”仅分为 2 行,但预期为 3 行。
  • @MukeshKumar 你get 3 lines,所以没有问题。
  • @WiktorStribiżew - 这在 RegEx 构建器中非常有效,但是当我在 C# 中使用更大的数字尝试它时,它给我的字符数不超过 127 个(我需要 32K 个字符,但它像即使在较小的数字(例如 320)上也是如此)。 var goodsChunks = Regex.Matches(application.Goods ?? "", @"(\b.{1,32000})(?:\s+|$)") .Cast().Select(p => p.Groups[1].Value) .ToList();有什么想法吗?
  • @Mike 我不知道您输入的字符串是什么样的以及您期望得到什么。
  • 道歉 - 我认为问题在于我的文本中有换行符。
【解决方案2】:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            var sentence = "Please write a program that breaks this text into small chucks. Each chunk should have a maximum length of 25 ";
            StringBuilder sb = new StringBuilder();
            int count = 0;
            var words = sentence.Split(' ');
            foreach (var word in words)
            {
                if (count + word.Length > 25)
                {
                    sb.Append(Environment.NewLine);
                    count = 0;
                }
                sb.Append(word + " ");
                count += word.Length + 1;
            }
            Console.WriteLine(sb.ToString());
            Console.ReadKey();
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-26
    • 2018-06-07
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    • 2011-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多