0

I know this has been asked before, but my code isn't working.

The senario is I need to check if a string ONLY contains letters, numbers and spaces. I need to fail if it contains any thing else.

I've tried the RegEx method, but I don't understand regular expressions, so I need to use a LINQ method for my assessment.

Here is my code:

if (!CSVItemArray[count].All(Char.IsLetterOrDigit) && !CSVItemArray[count].Contains(" "))
{
    return false;
}
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Smithy
  • 166
  • 1
  • 1
  • 13
  • Is `CSVItemArray` an array of strings? – ChrisF Jun 02 '16 at 12:45
  • @ChrisF Yes, it is. – Smithy Jun 02 '16 at 12:49
  • *so I need to use a LINQ method for my assessment*... What does LINQ have to do with checking for character types? – sstan Jun 02 '16 at 12:51
  • You should really try to consider regular expression here, which will be much more clear to you once you will be used to it. In this particular case "/^[a-zA-Z0-9 ]+$/" would be enough. You can have a look at http://regexr.com/ for example. You will find tutorial, help, samples and online test of your expressions ! – Seb T. Jun 02 '16 at 12:55

3 Answers3

3

Just combine the check for letter, digit, or whitespace in the All query:

if (!CSVItemArray[count].All(c => Char.IsLetterOrDigit(c) || Char.IsWhiteSpace(c)))
{
    return false;
}
D Stanley
  • 149,601
  • 11
  • 178
  • 240
2

Your logic is a little confused. The following returns true if the string in CSVItemArray[count] only contains letters, digits and white spaces:

return CSVItemArray[count].All(c => Char.IsLetterOrDigit(c) || Char.IsWhiteSpace(c));
René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • It is a boolean validation function, if it fails to meet my condition, it returns false. – Smithy Jun 02 '16 at 12:58
  • @Smithy yes that's what it does. I said it returns true if it contains _"only letters, digits and spaces"_, so it returns falls if there is something different than that. The code checks that _`All`_ characters inside the string are letters, digits or spaces. – René Vogt Jun 02 '16 at 13:01
1

Doing something (e.g. using Regex) because not understanding it is a bad thing - at least for developers. In particular if what you want to do can easily be achieved by using a regex.

Having said this you may simply use this:

Regex r = new Regex("^[A-Za-z0-9\\s]*$");
var valid = r.IsMatch(myString);

This will look for any number of upper- or lowercase characters, digits and whitespace-characters. The sequence itself is embraced by [], the following * sets the number of times the sequence can occur in the string (in your case none uo to infinite times). The ^ and $ are just for marking the start and end of your string repsectivly. This avoids that %asdfgh12345 // will match for instance.

EDIT: If you need Umlauts also (ä, ö, ü, ß, ...) you may have a look at this post which handles special characters also.

Community
  • 1
  • 1
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • Good point. Thoug your regex does not include äöüß and those letters. But OP didn't specify what makes a letter for him. – René Vogt Jun 02 '16 at 13:02
  • @HimBromBeere _"Doing something [...] because not understanding it is a bad thing..."_ I definitely agree with that. I will learn how Regex works, but if I use it in my assessment, I'll have to explain what it's doing, and my tutor hasn't taught me that, so it wouldn't go down well. – Smithy Jun 02 '16 at 13:09
  • @HimBromBeere I've realised what one of my issues is right now. I'm using the regex for now. My code fails when it checks a string with an apostrophe. How I include that in this? `"^[A-Za-z0-9\\s]*$"` – Smithy Jun 02 '16 at 13:16
  • Simply put it into the braces: `A-Za-z0-9´\\s`. If you need more special characters you should add those to your questions so that others also have the chance to edit their answers. – MakePeaceGreatAgain Jun 02 '16 at 13:17