In Haskell, how do you trim whitespace from the beginning and end of a string?

前端 未结 12 1264
感情败类
感情败类 2021-01-01 08:45

How do you trim whitespace from the start and end of a string?

trim \"  abc \" 

=>

\"abc\"

Edit:

Ok, let me be a little cleare

12条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-01 09:00

    Along the lines of what other people have suggested, you can avoid having to reverse your string by using:

    import Data.Char (isSpace)
    
    dropFromTailWhile _ [] = []
    dropFromTailWhile p item
      | p (last items) = dropFromTailWhile p $ init items
      | otherwise      = items
    
    trim :: String -> String
    trim = dropFromTailWhile isSpace . dropWhile isSpace
    

提交回复
热议问题