0

working with strings - help

I need help with this exercise which I try to do as it is in the code I shared but it generates an error. The problem mentions the following: In the RPG game you are playing, the player-character name must not exceed 10 characters in length. Write a program to take the player's character as input and abbreviate it from the 10th character if the name is longer than 10 characters. Generate the final result. Input example DragonBorn126 Output example DragonBorn Use the Remove () method to remove extra characters. Don't forget to reassign the formatted string to the name variable. This is my code: https://code.sololearn.com/ctfze852dNvD/?ref=app

5th Jul 2021, 10:57 PM
Alex Narváez
Alex Narváez - avatar
5 Respostas
+ 2
if (name.Length > 10) { name = name.Remove(10); } Console.WriteLine(name);
5th Jul 2021, 11:11 PM
visph
visph - avatar
+ 1
there's an error if the input string is less than 10 char length... you must test if input string length (name.Length) is greater than 10 and use Remove() only if true ;)
5th Jul 2021, 11:07 PM
visph
visph - avatar
+ 1
visph Excellent! only one condition was missing so that the code will execute correctly preventing exceptions. Thank you
5th Jul 2021, 11:14 PM
Alex Narváez
Alex Narváez - avatar
0
no error for me, and correct output for input example ^^
5th Jul 2021, 11:00 PM
visph
visph - avatar
0
visph. Exactly, in the exercise here there is no error but in the module the output does generate an error, and it is strange because that error should not appear if everything is fine here. I don't know what other solution can be implemented. I appreciate your suggestions.
5th Jul 2021, 11:09 PM
Alex Narváez
Alex Narváez - avatar