Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a string has only blank spaces or zeros with C#

Tags:

c#

regex

I want to detect if an IBAN account has all digits at zero 0, for example

00 0000 000 00 0000000000.

So I build this regex pattern to check it:

^(0\s){0,25}$

The argument would be: Check if the input string has only blank spaces or zeros.

But this does not work. Any help? Thank you.

like image 311
anmarti Avatar asked Jan 27 '26 05:01

anmarti


1 Answers

Here is a non-regex example.

string input = "00 0000 000 00 0000000000";

bool isNotAllZeros = input
    .Where(x => char.IsDigit(x))
    .Any(x => x != '0');

It says: "get me all the digits from the string, and see if there are any that are not '0'."

Another method (to avoid double-negatives), using All, which might make more sense when reading:

bool isAllZeros = input
    .Where(x => char.IsDigit(x))
    .All(x => x == '0');

This says "are all the digits in the input '0'?"

like image 51
gunr2171 Avatar answered Jan 29 '26 18:01

gunr2171