How do I trim leading and trailing whitespace in Common Lisp?
Asked Answered
C

1

12

How do I trim leading and trailing whitespace in Common Lisp?

Cleptomania answered 7/1, 2014 at 3:19 Comment(9)
Why is part of your question being given as an answer?Detachment
Because SO insisted on at least 30 characters in the body of the question. Ok, maybe that was a little unclear.Cleptomania
I'm not sure I understand. Wouldn't including your code in the question push you even further above 30?Detachment
I believed my answer to be correct, but didn't accept it immediately because it is possible that someone else has a better way.Cleptomania
Please note that "Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results." If you've already got a working solution, and didn't have any problems in coming up with (i.e., it's not all that difficult or unobvious), then it might not be a great StackOverflow question. If it took you a few iterations, then showing the problematic earlier ones in the question and a final one in an answer makes sense. If it's easy to write a working solution, this might be better on Code Review.Plight
@Joshua - I spent a while searching for the bits and pieces. I posted it here so that next time it would be one search away for both myself and for others.Cleptomania
@Cleptomania Well, there's not a clear line, but it might be worth mentioning that in the question. The downvote button tooltip says "This question does not show any research effort", which as written this question does. But as to research, the first Google hit for "trim string common lisp" is a link to the Trimming Blanks from the Ends of a String entry in the Common Lisp Cookbook which uses string-trim, and the second hit is the HyperSpec entry to string-trim. It's not clear, to me anyway, that this question needs answering on SO.Plight
@Joshua My search - 'common lisp trim whitespace' - found string-trim right away. What I needed was a list of all the whitespace characters. Neither of the links that you mention nor the results of my first search provided it. That was my rationale. As you can see, I'm new to SO. I'll accept your call.Cleptomania
possible duplicate of Trim curly braces from stringPardue
C
17
CL-USER> (string-trim 
      '(#\Space #\Newline #\Backspace #\Tab 
        #\Linefeed #\Page #\Return #\Rubout)
      "  A string   ")
"A string"

string-left-trim and string-right-trim for leading and trailing whitespace, respectively.

Cleptomania answered 7/1, 2014 at 3:19 Comment(1)
The second argument to string-trim is a character bag which can be a sequence. You might consider using a vector instead of a a list for better access. Additionally, are you sure that you got all the whitespace characters? Many implementations will include functions along the lines of whitespace-char-p. It might be worthwhile to iterate through character codes and collect all the characters satisfying it ahead of time, and use that as the sequence of white space characters.Plight

© 2022 - 2024 — McMap. All rights reserved.