On Wednesday, 4 December 2013 at 22:33:06 UTC, seany wrote:
that works, but wont the immutability imply that the above solution shall not work?

No. Essentially stripLeft and stripRight work by shrinking a Range by calling popFront and popBack until all of the characters in question are stripped. Think of it as changing what part of the string is "pointed to" vs changing the actual data itself. Since it doesn't modify the data inside, it's fine.

There is a bug in the Ali's suggestion. It's not equivalent to what you posted.

Consider: ((a) ... what your version implies is returning "(a" ... but Ali's version would return "a"

Here's a version that does what (I think) you want:

----------
string stripPar(string S)
{
        while(S[0] == '(' && S[S.length-1] == ')')
        {
        
              S = S[1 .. $ - 1];
        }

        return S;
}
----------

So, what this does is just changes the view of S, not the data of S. A subtle but important distinction.

Reply via email to