文字列の先頭・末尾から空白を取り除く
[*** Text 版]
先頭部分で連続している空白を取り除いたTextを得る関数
code: (haskell)
stripStart :: Text -> Text
末尾部分で連続している空白を取り除いたTextを得る関数
code: (haskell)
stripEnd :: Text -> Text
先頭部分および末尾部分で連続した空白を取り除いたTextを得る関数
code: (haskell)
strip :: Text -> Text
評価例
code:_
>> :set -XOverloadedStrings
>> import Data.Text
>> sample = " 3 spaces leading, 5 spaces trailing "
>> sample
" 3 spaces leading, 5 spaces trailing "
>> stripStart sample
"3 spaces leading, 5 spaces trailing "
>> stripEnd sample
" 3 spaces leading, 5 spaces trailing"
>> strip sample
"3 spaces leading, 5 spaces trailing"
[*** String 版]
Text版のstripStart,stripEnd,stripにあたるものは以下のように定義します.
code: (haskell)
import Data.Char (isSpace)
import qualified Data.List (dropWhileEnd) as L
stripStart' :: String -> String
stripStart' = dropWhile isSpace
stripEnd' :: String -> String
stripEnd' = L.dropWhileEnd isSpace
strip' :: String -> String
strip' = stripEnd' . stripStart'
評価例
code:_
>> sample' = " 3 spaces leading, 5 spaces trailing "
>> sample'
" 3 spaces leading, 5 spaces trailing "
>> stripStart' sample'
"3 spaces leading, 5 spaces trailing "
>> stripEnd' sample'
" 3 spaces leading, 5 spaces trailing"
>> strip' sample'
"3 spaces leading, 5 spaces trailing"