C
C#2y ago
AriieSiiS

❔ Help with strings

I put it here because I think it is a quick and very basic question, I have a string with a specific series of letters and I want to check that another string that the user writes by console can ONLY have letters of that string. How can I do it? I mean this is what I have so far but it doesn't work. In the first case I want to be able to enter only those numbers and then I want to be able to enter only one of those letters but it is not well done.
11 Replies
Angius
Angius2y ago
Loops Loop over one string Inside loop over the other Basically, compare each character of one string with each character of the other If the first letter of the string you want to check isn't any of the characters you want to check for, return false from the method If it is, check the second letter If you can go over all the letters, return true
AriieSiiS
AriieSiiSOP2y ago
ok, thanks, I thought it would be easier without having to do that but i guess is not happenning I'll see if I can fix it and post it here in a little while, but thanks.
Angius
Angius2y ago
I mean, you can use LINQ .All() with .Contains() should do the trick
AriieSiiS
AriieSiiSOP2y ago
I cant I have to do it in a "rudimentary" way, is the aim of the exercise.
Angius
Angius2y ago
Loops it is, then
MODiX
MODiX2y ago
Diddy#2634
REPL Result: Success
var allowed = "ABCDE";

Console.WriteLine(
"ABCDE".Split(allowed, StringSplitOptions.RemoveEmptyEntries).Length > 0
);


Console.WriteLine(
"ABCDEFG".Split(allowed, StringSplitOptions.RemoveEmptyEntries).Length > 0
);
var allowed = "ABCDE";

Console.WriteLine(
"ABCDE".Split(allowed, StringSplitOptions.RemoveEmptyEntries).Length > 0
);


Console.WriteLine(
"ABCDEFG".Split(allowed, StringSplitOptions.RemoveEmptyEntries).Length > 0
);
Console Output
False
True
False
True
Compile: 557.327ms | Execution: 73.035ms | React with ❌ to remove this embed.
Aaron
Aaron2y ago
not like that it doesn't
MODiX
MODiX2y ago
Windows10CE#8553
REPL Result: Success
var allowed = "ABCDE";

Console.WriteLine(
"A".Split(allowed, StringSplitOptions.RemoveEmptyEntries).Length > 0
);
var allowed = "ABCDE";

Console.WriteLine(
"A".Split(allowed, StringSplitOptions.RemoveEmptyEntries).Length > 0
);
Console Output
True
True
Compile: 602.280ms | Execution: 34.077ms | React with ❌ to remove this embed.
Aaron
Aaron2y ago
pretty sure you wanted the char[] version
MODiX
MODiX2y ago
Windows10CE#8553
REPL Result: Success
char[] allowed = "ABCDE".ToCharArray();

Console.WriteLine(
"A".Split(allowed, StringSplitOptions.RemoveEmptyEntries).Length > 0
);
char[] allowed = "ABCDE".ToCharArray();

Console.WriteLine(
"A".Split(allowed, StringSplitOptions.RemoveEmptyEntries).Length > 0
);
Console Output
False
False
Compile: 540.183ms | Execution: 73.077ms | React with ❌ to remove this embed.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server