C
C#ā€¢2y ago
GIGA BRAIN

Getting a string to only accept 0's and 1's

I don't understand what is wrong with this line of code in particular: bool result = userInput.All(c=>Char.IsLetter(c) && c=='1' && c=='0');
29 Replies
Jester
Jesterā€¢2y ago
it cant be '1' and '0' at the same time
GIGA BRAIN
GIGA BRAINā€¢2y ago
oh i see should i make two bool results then? or would that work
mtreit
mtreitā€¢2y ago
Use an or Not an and
Jester
Jesterā€¢2y ago
char is letter and (1 or 0)
GIGA BRAIN
GIGA BRAINā€¢2y ago
oh i'll try that rq now that I think about it, does this strong not work in my case?
GIGA BRAIN
GIGA BRAINā€¢2y ago
GIGA BRAIN
GIGA BRAINā€¢2y ago
i originally had the if statement set to false, but even if I put in 0's and 1's it would still think that I put something else is there a way to modify the bool result to only have c=='1' or c=='0'? i think the char.isLetter is the problem
Jester
Jesterā€¢2y ago
i think 1 and 0 arent letters you should remove the is lettee
GIGA BRAIN
GIGA BRAINā€¢2y ago
would I replace the IsLetter with IsDigit in that case? oh perfect that was it
Jester
Jesterā€¢2y ago
just is 0 or 1 is enough
GIGA BRAIN
GIGA BRAINā€¢2y ago
lemme try that
GIGA BRAIN
GIGA BRAINā€¢2y ago
GIGA BRAIN
GIGA BRAINā€¢2y ago
what do you mean by this? something is definitely going over my head
Jester
Jesterā€¢2y ago
c => nothin
GIGA BRAIN
GIGA BRAINā€¢2y ago
oooooh is that a pointer?
Jester
Jesterā€¢2y ago
no you arent using pointers
GIGA BRAIN
GIGA BRAINā€¢2y ago
wait no so what does c => mean? im guessing is character c 1 or 0?
mtreit
mtreitā€¢2y ago
All takes a function as input. You need to give it a function. => is a way to define a function (called a lambda) inline...
Jester
Jesterā€¢2y ago
like Func<int, bool> isChar0or1 = c => c is '0' or '1';
mtreit
mtreitā€¢2y ago
Func<char, bool> šŸ™‚
Jester
Jesterā€¢2y ago
ok thats too complex
GIGA BRAIN
GIGA BRAINā€¢2y ago
and just real quick, a function is also a method right
Jester
Jesterā€¢2y ago
yeah i realised sorry its 1 am i cant sleep void Foo() { ... } or Action foo = () => { ... } ate nearly the same thing but the last one is a delegate refering to a lambda instead of an ordenary method
GIGA BRAIN
GIGA BRAINā€¢2y ago
oh so its a difference regarding format?
Jester
Jesterā€¢2y ago
ill try go sleep
GIGA BRAIN
GIGA BRAINā€¢2y ago
for sure thanks for the help!
Jester
Jesterā€¢2y ago
it has some other differences
GIGA BRAIN
GIGA BRAINā€¢2y ago
gotcha, def got a lot to learn sleep well šŸ™‚
Jester
Jesterā€¢2y ago
lambdas are useful but can be tricky thanks
Want results from more Discord servers?
Add your server
More Posts
āœ… āœ… Generate migrations with EF on sqlserver image docker taking too much timeHello , it's my first time making questions here , so if i forgot to provide some information , pleaā” Read Stream to End of Json ObjectI am trying to read a stream from the beginning of a json object until the end of a json object, witā” Identity and Authentication Advice or SuggestionsI'm building a Hosted Blazor WASM website with .Net 6 Backend API and I'm struggling to make a decisā” Confused about Assembly.Load (Hotloading CSharp Assemblies)I want to be able to load/unload user made C# libraries during runtime. However, these user librarieā” Recording hotkeysHow do I write down user hotkeys to save? For example, you want to use the combination CTRL+G, how dā” I have a pop-up form in _Layout section which will be take Contact us form dataI have a pop-up form in _Layout section which will be take Contact us form data so where can i set tā” how do I select a specific text from the file and delete it and it's corresponding contents in C``` static void Cancel() { StreamReader customercancel =ā” slide bug in csharp script in unityim making a game in unity and i have a bug in my script where my character glides over the ground whā” System.Net.Http truncates one of the response attributesThe access_token is always 632 characters long, and when I test it in Postman it works fine Postmanā” Logging Sudden Application ShutdownHey guys, I would like to ask if there is a possibility to log the event of an unexpected applicatio