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

前端 未结 12 1386
感情败类
感情败类 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:59

    You can combine Data.Text's strip with it's un/packing functions to avoid having overloaded strings:

    import qualified Data.Text as T
    
    strip  = T.unpack . T.strip . T.pack
    lstrip = T.unpack . T.stripStart . T.pack
    rstrip = T.unpack . T.stripEnd . T.pack
    

    Testing it:

    > let s = "  hello  "
    > strip s
    "hello"
    > lstrip s
    "hello  "
    > rstrip s
    "  hello"
    

提交回复
热议问题