Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C#中,在一定数量的单词或字符后打断文本的方法?_C#_.net_Text_Newline_Line Breaks - Fatal编程技术网

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

在C#中,在一定数量的单词或字符后打断文本的方法?,c#,.net,text,newline,line-breaks,C#,.net,Text,Newline,Line Breaks,给定一个字符串: “Lorem ipsum door sit amet,concetetur adipiscing elit,sed do eiusmod tempor incidedunt ut”,在 4个单词 40个字符 使用最大语言版本的C#4(以便与Mono平台兼容) 更新/编辑: 正则表达式实现: ad#2-在40个字符后拆分(参见此) 这篇文章是一个社区wiki 四个字 正如O.R.Mapper在评论中所说,这实际上取决于您在给定字符串中定义“单词”的能力以及单词之间的分隔符。但

给定一个字符串

Lorem ipsum door sit amet,concetetur adipiscing elit,sed do eiusmod tempor incidedunt ut
”,在

  • 4个单词
  • 40个字符
  • 使用最大语言版本的
    C#4
    (以便与Mono平台兼容)


    更新/编辑:

    正则表达式实现

    ad#2-在40个字符后拆分(参见此)


    这篇文章是一个社区wiki

    四个字

    正如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个字符


    两者

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

    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);
    
    回答你的问题#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();
    }
    
    publicstaticstringinsertatintervals(这个字符串是s,int-interval,字符串值)
    {
    if(s==null | | s.长度0;i-=间隔){
    某人插入(i,价值);
    }
    使某人返回字符串();
    }
    
    @O.R.Mapper这不是类型集测试字符串的语言@Blam:是的,我知道。它不是英语(顺便说一句,也不是真正的拉丁语),因此我们可以推测OP的意思是“任何语言”,而不是特定的语言。@O.R.Mapper:Lore ipsum通常用于强调文本或语言不重要。没有更多,也没有更少。我喜欢引入的开放思想
    O.R.Mapper
    。让我们来谈谈这个主题。。。。正如蒂姆·施梅尔特(Tim Schmelter)所说:“它可能是任何文本”,我只是好奇一些人是如何找到理由对这个问题投赞成票的。它甚至不包括一个问题(唯一的
    标记在标题中)
    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);
    
    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();
    }