-3

This is what I have:

if (userVar == " ")
{
    Console.WriteLine("stop");
}

OK but that only handles it for one spacing. What if the user presses 2 spaces, 3 spaces, 100 spaces in the console, how do I make an if statement for all of those?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 7
    Please see [String.IsNullOrWhiteSpace](https://learn.microsoft.com/en-us/dotnet/api/system.string.isnullorwhitespace?view=netframework-4.7.2). – Andrew Morton Apr 06 '19 at 20:50
  • Possible duplicate of [Check if string is empty or all spaces in C#](https://stackoverflow.com/questions/7438957/check-if-string-is-empty-or-all-spaces-in-c-sharp) – Christian Gollhardt Apr 06 '19 at 23:52

5 Answers5

1

What you need is IsNullOrWhiteSpace function https://learn.microsoft.com/en-us/dotnet/api/system.string.isnullorwhitespace?view=netframework-4.7.2

Julian Solarte
  • 555
  • 6
  • 29
  • 1
    Looks like a comment, not an answer. ([same as Andrew's](https://stackoverflow.com/questions/55553352/how-do-i-prevent-a-user-from-entering-string-spaces#comment97807913_55553352), BTW). – Uwe Keim Apr 06 '19 at 20:54
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – 41686d6564 stands w. Palestine Apr 06 '19 at 20:56
0

In order to check if every character in the string is a space, you could do the following:

if(userVar.All(x => x == " "))
{
    Console.WriteLine("stop");
}

Alternatively, if you don't want to use lambdas, you could do this relatively easily with a function:

public static boolean IsAllSpaces(string input)
{
    if(input == null || input == String.Empty)
    {
        return false;
    }
    foreach(char c in input)
    {
        if(c != " ")
        {
            return false;
        }
    }
    return true;
}

Note that the first will throw an exception if the string is null, while the second will return false. Up to you which is preferable behavior, you could check for null in the first example before calling All if you want.

IllusiveBrian
  • 3,105
  • 2
  • 14
  • 17
  • 3
    Why reinvent the wheel? The framework already provides a `String.IsNullOrWhiteSpace` function. – 41686d6564 stands w. Palestine Apr 06 '19 at 21:07
  • 1
    @AhmedAbdelhameed IsNullOrWhiteSpace checks for any whitespace character, not just spaces. See https://learn.microsoft.com/en-us/dotnet/api/system.char.iswhitespace?view=netframework-4.7.2. It will also return true for empty and null strings, which is not per OP spec. – IllusiveBrian Apr 06 '19 at 21:37
0
if (userVar.length > 0 && String.IsNullOrEmpty(userVar.Trim())
{
    Console.WriteLine("stop");
}

It has caracters and they are only spaces...

0

Use it like this:

if (String.IsNullOrWhiteSpace(userVar))
{
    Console.WriteLine("stop");
}

According to documentation: Returns true if the value parameter is null or Empty, or if value consists exclusively of white-space characters.

Usman Afzal
  • 376
  • 3
  • 9
-1

Well, if you want NOT A SINGLE space. you can use string.Conatins() function.

if (userVar.Contains(" "))
{
    Console.WriteLine("stop");
}

NOTE: No matter user inputs 1 space or 100, it'll detect.

Zain Arshad
  • 1,885
  • 1
  • 11
  • 26
  • Please note that this will "stop" even if the string contains a space _and_ other characters as well. I can't tell for sure that that's not what the OP wanted but if that's what _you_ intended, I think you should make that clear in your answer. – 41686d6564 stands w. Palestine Apr 06 '19 at 21:10
  • I have mentioned that IT WILL DETECT ANY SPACE CHARACTER. Mentioned it not once bu two times in my answer. – Zain Arshad Apr 06 '19 at 21:13