Tokenize a string: Difference between revisions

added ocaml
(added ocaml)
Line 215:
splitBy f list = first : splitBy f (dropWhile f rest) where
(first, rest) = break f list
splitRegex :: Regex -> String -> [String]
joinWith :: [a] -> <nowiki>[[a]]</nowiki> -> [a]
joinWith d xs = concat $ List.intersperse d xs
-- "concat $ intersperse" can be replaced with "intercalate" from the Data.List in GHC 6.8 and later
 
putStrLn $ joinWith "." $ splitBy (== ',') $ "Hello,How,Are,You,Today"
-- using regular expression to split:
import Text.Regex
putStrLn $ joinWith "." $ splitRegex (mkRegex ',') $ "Hello,How,Are,You,Today"
 
=={{header|Groovy}}==
Line 267 ⟶ 274:
)
format "%\n" output
 
=={{header|OCaml}}==
To split on a single-character separator:
<ocaml>let rec split_char sep str =
try
let i = String.index str sep in
String.sub str 0 i ::
split_char sep (String.sub str (i+1) (String.length str - i - 1))
with Not_found ->
[str]</ocaml>
 
Splitting on a string separator using the regular expressions library:
<ocaml>#load "str.cma";;
let split_str sep str =
Str.split (Str.regexp_string sep) str</ocaml>
 
Joining is a simple folding recursion:
<ocaml>let rec join_with d = function
[] -> ""
| [x] -> x
| x :: xs -> x ^ d ^ join_with d xs</ocaml>
 
=={{header|Perl}}==
Anonymous user