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

前端 未结 12 1388
感情败类
感情败类 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 08:58

    If you have serious text processing needs then use the text package from hackage:

    > :set -XOverloadedStrings
    > import Data.Text
    > strip "  abc   "
    "abc"
    

    If you're too stubborn to use text and don't like the inefficiency of the reverse method then perhaps (and I mean MAYBE) something like the below will be more efficient:

    import Data.Char
    
    trim xs = dropSpaceTail "" $ dropWhile isSpace xs
    
    dropSpaceTail maybeStuff "" = ""
    dropSpaceTail maybeStuff (x:xs)
            | isSpace x = dropSpaceTail (x:maybeStuff) xs
            | null maybeStuff = x : dropSpaceTail "" xs
            | otherwise       = reverse maybeStuff ++ x : dropSpaceTail "" xs
    
    
    > trim "  hello this \t should trim ok.. .I  think  ..  \t "
    "hello this \t should trim ok.. .I  think  .."
    

    I wrote this on the assumption that the length of spaces would be minimal, so your O(n) of ++ and reverse is of little concern. But once again I feel the need to say that if you actually are concerned about the performance then you shouldn't be using String at all - move to Text.

    EDIT making my point, a quick Criterion benchmark tells me that (for a particularly long string of words with spaces and ~200 pre and post spaces) my trim takes 1.6 ms, the trim using reverse takes 3.5ms, and Data.Text.strip takes 0.0016 ms...

提交回复
热议问题