Check if string has space in between (or anywhere)
C#C# Problem Overview
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.
C# Solutions
Solution 1 - C#
How about:
myString.Any(x => Char.IsWhiteSpace(x))
Or if you like using the "method group" syntax:
myString.Any(Char.IsWhiteSpace)
Solution 2 - C#
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.
Solution 3 - C#
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
Solution 4 - C#
Trim()
will only remove leading or trailing spaces.
Try .Contains()
to check if a string contains white space
"sossjjs sskkk".Contains(" ") // returns true
Solution 5 - C#
This functions should help you...
bool isThereSpace(String s){
return s.Contains(" ");
}