これはばかげているように聞こえるかもしれませんが、文字列がこのように見える場合、文字列内の最後の文字インデックスを見つけるにはどうすればよいですか。"string with white-space after last character "
一貫していて 1 つだけであれば問題ありませんが、2 つまたは 3 つの空白がある場合もあります。
編集: 最後の文字のインデックスが正しくないため、現在の文字列を新しい文字列にトリムできません。文字列をそのままにしておきたい
これが理由であり、私が得たものです
string description = "Lorem Ipsum is simply dummy text of the printing and typesetting indu. Lorem Ipsum has ben indusry s tandard dummy text ever since the 1500s.";
description = Regex.Replace(description, @"(?></?\w+)(?>(?:[^>'""]+|'[^']*'|""[^""]*"")*)>", String.Empty);
if (description.Count() > 101)
{
description = description.Substring(0, 101);
if (description.GetLast() != " ")
{
description = description.Substring(0, description.LastIndexOf(" ", 101)) + "...";
}
else
{
//here is should find the last character no mather how many whitespaces
description = description.Substring(0, description.Length - 1) + "...";
}
}