We're a place where coders share, stay up-to-date and grow their careers.
In C#, I would use in built string function Trim() In my opinion we don't need Substring() here. Just call :
readedInput.Trim(readedInput[0], readedInput[readedInput.Length - 1])
With 2 length validation:
var trimmed = readedInput.Length > 2 ? readedInput.Trim(readedInput[0], readedInput[readedInput.Length - 1]) : "Invalid" ;
But what if the input would be "aaajldflbbb"?
When using Trim(char[]), all appearances of the characters given to the method at the beginning and at the end, will be removed, leaving "jldfl".
Removes all leading and trailing occurrences of a set of characters specified in an array from the current String object.
In C#, I would use in built string function Trim()
In my opinion we don't need Substring() here.
Just call :
With 2 length validation:
But what if the input would be "aaajldflbbb"?
When using Trim(char[]), all appearances of the characters given to the method at the beginning and at the end, will be removed, leaving "jldfl".