方式到一定数量的C#单词或字符的后断开文本? [英] Ways to break text after a certain number of words or characters in C#?

查看:291
本文介绍了方式到一定数量的C#单词或字符的后断开文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个字符串

Lorem存有悲坐阿梅德,consectetur adipisicing ELIT,sed的根本eiusmod tempor incididunt UT ,后

打破它
  1. 4字
  2. 40个字符

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


更新/编辑:

regex实现

广告#2 - 拆分后的40个字符(见本要点

 使用系统;
使用System.Text.RegularEx pressions;
Regex.Split(
Lorem存有悲坐阿梅德,consectetur adipisicing ELIT,中美战略经济对话做eiusmod tempor incididunt UT
({40})
,RegexOptions.Multiline)
。凡(S =>!string.IsNullOrEmpty(多个))
.ToArray();
 


此职位作为社区维基

解决方案
  

4字

由于OR映射在他的评论说,这真的取决于你给定的字符串,什么分隔符字之间定义一个字的能力。但是,假设你可以定义分隔符为空白,那么这应该工作:

 使用System.Text.RegularEx pressions;

字符串delimiterPattern = @\ S +; //我使用空格作为分隔符这里

//查找单词之间的所有空间
MatchCollection匹配= Regex.Matches(文字,delimiterPattern);

//如果我们发现了至少4分隔符,切断串在第4(指数= 3)
//分隔符。否则,只要保持原始字符串
字符串firstFourWords =(matches.Count> = 4)
    ? (text.Substring(0,匹配[3] .Index))
    : (文本);
 


  

40个字符

 字符串firstFortyCharacters = text.Substring(0,Math.Min(text.Length,40));
 


  

两个

结合两者,我们可以得到较短的:

 使用System.Text.RegularEx pressions;

字符串delimiterPattern = @\ S +; //我使用空格作为分隔符这里

//查找单词之间的所有空间
MatchCollection匹配= Regex.Matches(文字,delimiterPattern);

//如果我们发现了至少4分隔符,切断串在第4(指数= 3)
//分隔符。否则,只要保持原始字符串
字符串firstFourWords =(matches.Count> = 4)
    ? (text.Substring(0,匹配[3] .Index))
    : (文本);

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

字符串结果=(firstFourWords.Length→40)? (firstFortyCharacters):(firstFourWords);
 

Given a string:

"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut", break it after

  1. 4 words
  2. 40 characters

using a maximum language version of C# 4 (in order to be compatible with the Mono platform).


Update/Edit:

Regex Implementations:

ad #2 - split after 40 characters (see this gist)

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


This post serves as a community wiki.

解决方案

4 words

As O. R. Mapper said in his comment, this really depends on your ability to define a "word" in a given string and what the delimiters are between words. However, assuming you can define the delimiter as whitespace, then this should work:

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


40 characters

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


Both

Combining both, we can get the shorter one:

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

这篇关于方式到一定数量的C#单词或字符的后断开文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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