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

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.

This question is related to c#

The answer is


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.


How about:

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

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

myString.Any(Char.IsWhiteSpace)

This functions should help you...

bool isThereSpace(String s){
    return s.Contains(" ");
}

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

Trim() will only remove leading or trailing spaces.

Try .Contains() to check if a string contains white space

"sossjjs sskkk".Contains(" ") // returns true