Dejan.S
Dejan.S

Reputation: 19138

Find the last character of a string that got whitespaces after it

This might sound stupid but how can i find the last characters index in a string if the string looks like this "string with white-space after last character ", if it was consistent and just 1 it would be no problem but sometimes it might be 2 or 3 white-spaces

EDIT: I cant trim my current string to a new string, because the index of the last character wont be right. I want to keep the string as is

This is why and what I got

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) + "...";
    }
}

Upvotes: 0

Views: 5395

Answers (10)

aavezel
aavezel

Reputation: 604

Use Array.FindLastIndex for search last char who not equialent as whitespace:

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

Upvotes: 0

horgh
horgh

Reputation: 18533

Just this. No necessity for Linq or Regex. But TrimEnd(), not Trim(), and you don't need to think whether you have whitespaces in the beginning of the string.

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

If OP really wants to use Regex for that, than here's my improvisation:

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

Upvotes: 0

codeling
codeling

Reputation: 11389

Under the assumption that it doesn't matter that other whitespace characters (e.g. tab or line feed) at the end are also ignored, simply use trim:

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

Note that the original string s remains unchanged by that (see TrimEnd() documentation).

Upvotes: -1

Ivan Golović
Ivan Golović

Reputation: 8832

Try this:

        string s = "string with whitespace after last character    ";
        int index = s.Length - s
            .ToCharArray()
            .Reverse()
            .TakeWhile(c => c == ' ')
            .Count();

Upvotes: 1

Martin Liversage
Martin Liversage

Reputation: 106826

For completeness here is a solution that uses regular expressions (I'm not claiming that it is any better than the other proposed solutions though):

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

Note that if the string is empty lastIndex will be -1 and you need to handle this in your code.

Upvotes: 2

Tigran
Tigran

Reputation: 62256

All answers here are trim the string so create a new string with shifted indices, so the final result would be wrong index in the original string.

What would be done instead, is just

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

returns:

x :    'r'
index: 42

Upvotes: 1

AndrewR
AndrewR

Reputation: 6748

Maybe this one?

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

Upvotes: 0

Apocalyp5e
Apocalyp5e

Reputation: 181

why not trim your string

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

Hope it helps

Upvotes: 0

Andromeda
Andromeda

Reputation: 12897

Use the trim function and get the last index of that string..

Upvotes: 0

Alex
Alex

Reputation: 23300

If the string doesn't have any whitespace before the first character, yourString.Trim().Length -1 should acomplish this.

Upvotes: 0

Related Questions