在C#中使用一定数量的单词或字符后打破文本的方法?

Lor*_*uer 3 .net c# text newline line-breaks

给定一个字符串:

" Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut",之后打破它

  1. 4个字
  2. 40个字符

使用最大语言版本C# 4(为了与Mono平台兼容).


更新/编辑:

正则表达式实现:

广告#2 - 在40个字符后分割(请参阅此要点)

using System;
using System.Text.RegularExpressions;
Regex.Split(
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut"
, "(.{40})"
, RegexOptions.Multiline)
.Where(s => !string.IsNullOrEmpty(s))
.ToArray();
Run Code Online (Sandbox Code Playgroud)

这篇文章是一个社区维基.

Cha*_*ion 5

4个字

正如OR Mapper在他的评论中所说,这实际上取决于你在给定字符串中定义"单词"的能力以及单词之间的分隔符.但是,假设您可以将分隔符定义为空格,那么这应该工作:

using System.Text.RegularExpressions;

string delimiterPattern = @"\s+"; // I'm using whitespace as a delimiter here

// find all spaces between words
MatchCollection matches = Regex.Matches(text, delimiterPattern);

// if we found at least 4 delimiters, cut off the string at the 4th (index = 3)
// delimiter. Else, just keep the original string
string firstFourWords = (matches.Count >= 4)
    ? (text.Substring(0, matches[3].Index))
    : (text);
Run Code Online (Sandbox Code Playgroud)

40个字符

string firstFortyCharacters = text.Substring(0, Math.Min(text.Length, 40));
Run Code Online (Sandbox Code Playgroud)

结合两者,我们可以得到更短的一个:

using System.Text.RegularExpressions;

string delimiterPattern = @"\s+"; // I'm using whitespace as a delimiter here

// find all spaces between words
MatchCollection matches = Regex.Matches(text, delimiterPattern);

// if we found at least 4 delimiters, cut off the string at the 4th (index = 3)
// delimiter. Else, just keep the original string
string firstFourWords = (matches.Count >= 4)
    ? (text.Substring(0, matches[3].Index))
    : (text);

string firstFortyCharacters = text.Substring(0, Math.Min(text.Length, 40));

string result = (firstFourWords.Length > 40) ? (firstFortyCharacters) : (firstFourWords);
Run Code Online (Sandbox Code Playgroud)