2

在某个计数之后,我用.Substring(0, 100). 我不想在单词中间打断,如果是这样的话,我想进入单词之前的第一个空格。

我想我检查下一个字符是否不在" "单词中间。我最大的问题是如何退后一步来获取" "(空白)的索引。

这就是我到目前为止所得到的

        string description = "a long string";            

        description = Regex.Replace(description, @"(?></?\w+)(?>(?:[^>'""]+|'[^']*'|""[^""]*"")*)>", String.Empty);
        var newString = (description.Count() > 101) ? description.Substring(0, 101) : description;

        //i tried something like this
        var whatIsNext = newString.IndexOf(" ", 100, -20);
4

3 回答 3

3

我想你正在寻找 String.LastIndexOf

报告此实例中指定 Unicode 字符最后一次出现的从零开始的索引位置。搜索从指定的字符位置开始,然后向后向字符串的开头进行。

你想要这样的东西:

int index = s.LastIndexOf(' ', 100);
于 2012-09-06T12:42:01.693 回答
1

使用 LastIndexOf 并将其应用于您的子字符串

// The string we are searching.
string value = "Dot Net Perls";
//
// Find the last occurrence of ' '.
int index1 = value.LastIndexOf(' ');

链接信息

于 2012-09-06T12:43:45.260 回答
0

var whatIsNext = newString.Substring(0, newString.LastIndexOf(' '));

于 2012-09-06T12:42:52.863 回答