设为字符串字母数字,我想在找到数字时拆分它。
remove xs = [ x | x <- xs, not (x `elem` ",.?!-:;\"\'0123456789")]
我从那些数字和somne标点符号开始但我怎么能分割字符串,所以我给字符串"I go 25abc tomorrow 100!"->["I","go","abc","tomorrow"]
!
答案 0 :(得分:2)
> import Data.List.Split
> split (dropDelims . dropBlanks $ oneOf " 0123456789") "I go 25abc tomorrow 100!"
["I","go","abc","tomorrow","!"]
使用words
两步法将起作用
> words $ map (\x -> if x `elem` ['a'..'z']++['A'..'Z'] then x else ' ') "I go 25abc tomorrow 100!"
["I","go","abc","tomorrow"]
这里我将过滤器的定义更改为只有alpha字符,如果不符合您的需要,您可以通过定义过滤掉的字符来反转if条件。
导入Data.Char(isAlpha)
即可更改
x `elem` ['a'..'z']++['A'..'Z']
到isAlpha x
答案 1 :(得分:1)
您可以定义这样的递归函数:
import Data.Char
split :: String -> [String]
split [] = []
split s =
let -- Remove non alphabetic characters from front of list
s' = dropWhile (not . isAlpha) s
-- Split the list at the point where the first non-alphabetic
-- character appears
(xs,ys) = span isAlpha s'
-- Recursively call split on the second list, while prepending
-- the first list.
in xs:(split ys)
以下是通过递归调用对测试字符串进行的操作:
s = "I go 25abc tomorrow 100!"
s' = "I go 25abc tomorrow 100!"
xs = "I"
ys = " go 25abc tomorrow 100!"
s = " go 25abc tomorrow 100!"
s' = "go 25abc tomorrow 100!"
xs = "go"
ys = " 25abc tomorrow 100!"
s = " 25abc tomorrow 100!"
s' = "abc tomorrow 100!"
xs = "abc"
ys = " tomorrow 100!"
s = " tomorrow 100!"
s' = "tomorrow 100!"
xs = "tomorrow"
ys = " 100!"
s = " 100!"
s' = ""
xs = ""
ys = ""
请注意,这会将单词"abc4def"
拆分为["abc", "def"]
。
答案 2 :(得分:0)
以下应该这样做。
splitWithoutNumbers :: String -> [String]
splitWithoutNumbers = map removeNumbers.words
where
removeNumbers "" = ""
removeNumbers (s:ss) | isNumber s = removeNumbers ss
| otherwise = s : removeNumbers ss
*Main> splitWithoutNumbers "I go 25abc tomorrow 100!"
["I","go","abc","tomorrow","!"]
答案 3 :(得分:0)
import Data.Char
import Control.Monad
remove = words . filter (liftM2 (||) isAlpha isSpace)
答案 4 :(得分:-1)
下面的代码行将在找到数字
时拆分字符串String [] strArr = str.split(“[0-9]”);