在Haskell中,如何从字符串的开始和结尾修剪空白字符? [英] In Haskell, how do you trim whitespace from the beginning and end of a string?
问题描述
trimabc
=>
abc
编辑:
好吧,让我更清楚一点。我不明白字符串文字与字符串的处理方式差别很大。
我想这样做:
输入限定的Data.Text作为T
在T.strip中输入s :: String =abc
Haskell可能吗?我使用的是-XOverloadedStrings,但只出现在文字上。
如果您有严重的文本处理需求, code> text hackage包:
> :set -XOverloadedStrings
>导入Data.Text
> stripabc
abc
如果你太固执,不能使用 text
并且不喜欢反向方法的低效率,那么或许(并且我的意思是MAYBE)如下所示会更有效率:
import Data.Char
trim xs = dropSpaceTail$ dropWhile isSpace xs
dropSpaceTail maybeStuff=
dropSpaceTail maybeStuff(x:xs)
| isSpace x = dropSpaceTail(x:maybeStuff)xs
| null maybeStuff = x:dropSpaceTailxs
|否则=反向MaybeStuff ++ x:dropSpaceTailxs
>修剪你好这\\ \\ t应该修剪好..我想.. \ t
你好这\ t应该修剪好..我想..
我写了这个假设,空间的长度是最小的,所以你的O(n)+ +
和reverse
是很不重要的。但是我再次觉得有必要说,如果你真的关心性能,那么你根本不应该使用String
- 移动到文本
。
编辑让我的观点,快速的Criterion基准测试告诉我(对于一个特别长的字符串,和空格)我的修剪需要1.6毫秒,使用反向的修剪需要3.5ms,并且
Data.Text.strip
需要0.0016毫秒...How do you trim whitespace from the start and end of a string?
trim " abc " => "abc"
Edit:
Ok, let me be a little clearer. I did not understand that string literals were treated so differently from Strings.
I would like to do this:
import qualified Data.Text as T let s :: String = " abc " in T.strip s
Is this possible in Haskell? I am using -XOverloadedStrings but that appears only to work for literals.
解决方案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
++
andreverse
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 usingString
at all - move toText
.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...这篇关于在Haskell中,如何从字符串的开始和结尾修剪空白字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!