0

これはばかげているように聞こえるかもしれませんが、文字列がこのように見える場合、文字列内の最後の文字インデックスを見つけるにはどうすればよいですか。"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) + "...";
    }
}
4

10 に答える 10

2

完全を期すために、正規表現を使用するソリューションを次に示します (他の提案されたソリューションよりも優れていると主張しているわけではありません)。

var text = "string with whitespace after last character  ";
var regex = new Regex(@"\s*$");
var match = regex.Match(text);
var lastIndex = match.Index - 1;

文字列が空の場合はlastIndex-1 になり、コードでこれを処理する必要があることに注意してください。

于 2012-09-07T07:08:25.577 に答える
1

ここでのすべての答えは、文字列をトリムするため、インデックスがシフトされた新しい文字列を作成するため、最終結果は元の文字列の間違ったインデックスになります。

代わりに何が行われるかは、ただ

"string with whitespace after last character ".ToCharArray().
          Select((x,i)=> new {x,i}).Where(ch=>ch.x != ' ').Last();

戻り値:

x :    'r'
index: 42
于 2012-09-07T07:02:28.560 に答える
1

これを試して:

        string s = "string with whitespace after last character    ";
        int index = s.Length - s
            .ToCharArray()
            .Reverse()
            .TakeWhile(c => c == ' ')
            .Count();
于 2012-09-07T07:14:59.907 に答える
0

文字列の最初の文字の前に空白がない場合は、これyourString.Trim().Length -1を実行する必要があります。

于 2012-09-07T06:55:45.550 に答える
0

トリム関数を使用して、その文字列の最後のインデックスを取得します。

于 2012-09-07T06:55:52.610 に答える
0

文字列をトリミングしてみませんか

string abc = "string with whitespace after last character ";
abc = abc.trim();

それが役に立てば幸い

于 2012-09-07T06:57:28.877 に答える
0

たぶんこれ?

String s = "string with whitespace after last character  ";
int lastCharIndex = s.Length - (s.TrimEnd().Length);
于 2012-09-07T06:59:12.170 に答える
0

これだけ。Linq や Regex は必要ありません。しかしTrimEnd()、そうではなくTrim()、文字列の先頭に空白があるかどうかを考える必要はありません。

string s = "string with whitespace after last character  ";
int lastCharIndex = s.TrimEnd().Length - 1;

OPが本当にそのために正規表現を使用したい場合、ここに私の即興があります:

        string text = "string with whitespace after last character  ";
        Match m = Regex.Match(text, @"^(.*)(\w{1})\s*$");
        if (m.Success)
        {
            int index = m.Groups[1].Length;
            Console.WriteLine(text[index]);
            Console.WriteLine(index);
        }

        Console.ReadLine();
于 2012-09-07T07:07:25.297 に答える
0

Array.FindLastIndex空白と同等でない最後の文字を検索するために使用します。

Array.FindLastIndex(str.ToCharArray(), ch => !Char.IsWhiteSpace(ch))

于 2014-07-15T08:41:40.330 に答える
-1

最後にある他の空白文字(タブや改行など)も無視されることは問題ではないという仮定の下で、単にトリムを使用します。

String s = "string with whitespace after last character  ";
int lastChar = s.TrimEnd().Length-1;

元の文字列sはそれによって変更されないことに注意してください(TrimEnd()のドキュメントを参照)。

于 2012-09-07T06:56:03.560 に答える