我想知道我的字符串是否以另一个字符串开头。例如:
startsWith "(" "(test string)" == True
有没有内置在Haskell中的这样一个函数?
我想知道我的字符串是否以另一个字符串开头。例如:
startsWith "(" "(test string)" == True
由于字符串是字符列表,我们可以导入 Data.List
并使用通用函数isPrefixOf
:
isPrefixOf :: Eq a => [a] -> [a] -> Bool
例子:
Prelude Data.List> isPrefixOf "abc" "abcxyz"
True
Data.Text
你可以使用isPrefixOf
检查一个字符串是否以另一个字符串开头:
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.Text as T
T.isPrefixOf "(" "(test string)"
要在您的项目中使用Data.Text
,请在您的 Cabal 文件中将text
添加为依赖项:
build-depends:
base >=4.7 && <5
, text