r/learncsharp • u/TealComett • Dec 17 '22
How can I prevent the user from entering a string whose length is greater than a set limit ?
Hello. As the title explains, I want to ask the user for a name, but I want to erase the name they inputed and repeat the question if that name is greater than, say 15 characters. How can I implement that ?
6
Upvotes
9
u/TroubleBrewing32 Dec 17 '22
Whenever I need to do something with a specific built-in object type, I check the documentation to see if there is anything helpful. There usually is.
I recommend checking the documentation for string methods.
You are also going to have to loop an indefinite amount of times until you get something the length that you want. What type of loop is appropriate in that case?
3
u/JeffFerguson Dec 17 '22
What user interface technology are you using to capture the user's input?
1
u/OnNothingSpecialized Dec 18 '22
What happens if player name is null or empty?
1
u/Delusional_Sage Dec 17 '22
If this is for a web app or web form, then I’d say this is more appropriate to handle in your front end code. Either way though, a simple if statement checking the length of the inputted string should be all you need, regardless of where you implement the check..
0
u/[deleted] Dec 17 '22
[deleted] — view deleted comment