【问题标题】:Ways to break text after a certain number of words or characters in C#? [closed]在 C# 中一定数量的单词或字符后中断文本的方法? [关闭]
【发布时间】:2012-09-14 21:55:18
【问题描述】:

给定一个字符串

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

  1. 4 个字
  2. 40 个字符

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


更新/编辑:

正则表达式实现

广告 #2 - 在 40 个字符后拆分(参见 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();

这篇文章用作社区维基

【问题讨论】:

  • @O.R.Mapper 不是类型设置测试字符串 en.wikipedia.org/wiki/Lorem_ipsum 的语言
  • @Blam:是的,我知道。它不是英语(顺便说一句,也不是真正的拉丁语),因此我们可以推测 OP 的意思是“任何语言”而不是特定语言。
  • @O.R.Mapper:当有人想强调文本或语言无关紧要时,通常使用 Lore-ipsum。不多也不少。
  • 我喜欢O.R.Mapper 介绍的开放思想。让我们继续那个主题......正如Tim Schmelter所说:它“可能是任何文本”
  • 我只是好奇有些人是如何找到支持这个问题的理由的。它甚至不包括一个问题(唯一的? 标记在标题中)

标签: c# .net text newline line-breaks


【解决方案1】:

回答你的问题 #2:把它放在一个静态类中,你会得到一个很好的扩展方法,它可以在另一个字符串中以给定的间隔插入一个字符串

public static string InsertAtIntervals(this string s, int interval, string value)
{
    if (s == null || s.Length <= interval) {
        return s;
    }
    var sb = new StringBuilder(s);
    for (int i = interval * ((s.Length - 1) / interval); i > 0; i -= interval) {
        sb.Insert(i, value);
    }
    return sb.ToString();
}

【讨论】:

    【解决方案2】:

    4 个字

    正如 O. R. 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);
    

    40 个字符

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

    两者

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-04
      • 1970-01-01
      • 2014-10-16
      • 2012-10-17
      • 2023-01-30
      相关资源
      最近更新 更多