DEV Community

loading...

Discussion on: Daily Challenge #1 - String Peeler

Collapse
praneetnadkar profile image
Praneet Nadkar • Edited

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" ;
Collapse
andreasjakof profile image
Andreas Jakof

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.