tags:

views:

101

answers:

4

Given a string that ends in a whitespace character return true.

I'm sure I should be able to do this with regular expressions but I'm not having any luck. MSDN reference for regular expressions tells me that \s should match on a whitespace, but I can't figure the rest out.

A: 

How about

.+\s$
Vadim
A: 

$ marks the end of the target on the pattern:

\s$

Jordão
A: 

Like this:

if (Regex.IsMatch(someString, @"\s+$"))
  • \s matches whitespace
  • + means one or more of the preceding expression
    (one or more whitespace characters)
  • $ means the end of the string
SLaks
+7  A: 

You certainly can use a regex for this, and I'm sure someone smarter than me will post exactly how to do it :), but you probably don't want to use a regex in this case. It will almost certainly be faster to simply ensure that the string is not null or empty, and then to return

Char.IsWhiteSpace(myString[length - 1])
mjd79
wow, didn't know about that, and it has been around since 2.0 http://msdn.microsoft.com/en-us/library/system.char.iswhitespace.aspx
TesterTurnedDeveloper