Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if string has space in between (or anywhere) [duplicate]

Tags:

c#

Is there a way to determine if a string has a space(s) in it?

sossjjs sskkk should return true, and sskskjsk should return false.

"sssss".Trim().Length does not seem to work.

like image 756
GurdeepS Avatar asked Jan 16 '12 23:01

GurdeepS


People also ask

How do I check if a string contains whitespace?

Use the test() method to check if a string contains whitespace, e.g. /\s/. test(str) . The test method will return true if the string contains at least one whitespace character and false otherwise.

How do you check if a string contains a space in C++?

The isspace() function in C++ checks if the given character is a whitespace character or not.

How do you show a space in a string?

However, " " , "\t" and "\n" are all strings containing a single character which is characterized as whitespace. If you just mean a space, use a space.

How check string is space or not in PHP?

PHP | ctype_space() Function A ctype_space() function in PHP is used to check whether each and every character of a string is whitespace character or not. It returns True if the all characters are white space, else returns False.


3 Answers

How about:

myString.Any(x => Char.IsWhiteSpace(x))

Or if you like using the "method group" syntax:

myString.Any(Char.IsWhiteSpace)
like image 85
Dave Avatar answered Sep 29 '22 19:09

Dave


If indeed the goal is to see if a string contains the actual space character (as described in the title), as opposed to any other sort of whitespace characters, you can use:

string s = "Hello There";
bool fHasSpace = s.Contains(" ");

If you're looking for ways to detect whitespace, there's several great options below.

like image 45
Mike Christensen Avatar answered Sep 29 '22 19:09

Mike Christensen


It's also possible to use a regular expression to achieve this when you want to test for any whitespace character and not just a space.

var text = "sossjj ssskkk";
var regex = new Regex(@"\s");
regex.IsMatch(text); // true
like image 32
David Clarke Avatar answered Sep 29 '22 21:09

David Clarke