在Haskell中处理字符串通常使用String类型,也可以使用Text类型,具体取决于需求。
以下是处理字符串的一些常见操作:
str1 = "Hello" str2 = "World" concatenatedStr = str1 ++ ", " ++ str2 str = "Hello" strLength = length str str = "Hello World" containsStr = "World" `isInfixOf` str import Data.List.Split str = "Hello,World" splitStr = splitOn "," str import Data.Char str = "hello" upperStr = map toUpper str lowerStr = map toLower str import Data.List str = "Hello World" replacedStr = intercalate "-" (words str) str1 = "123" intVal = read str1 :: Int str2 = "3.14" floatVal = read str2 :: Float 这些是一些常见的字符串操作,还有许多其他函数和库可以帮助更复杂的字符串处理任务。