summaryrefslogtreecommitdiffstats
path: root/stdlib/string.ml
diff options
context:
space:
mode:
authorFabrice Le Fessant <Fabrice.Le_fessant@inria.fr>2013-06-05 13:27:05 +0000
committerFabrice Le Fessant <Fabrice.Le_fessant@inria.fr>2013-06-05 13:27:05 +0000
commit1f9f68a328b6c96188ee48430b09ffee754e7b67 (patch)
treecd24383b87f27aaaac7ec1a2e6e43c6ffa8290ae /stdlib/string.ml
parentd940182de56eed2503b2492fac1cab0b607a4877 (diff)
Add String.split and String.cut_at from Misc
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@13746 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
Diffstat (limited to 'stdlib/string.ml')
-rw-r--r--stdlib/string.ml20
1 files changed, 20 insertions, 0 deletions
diff --git a/stdlib/string.ml b/stdlib/string.ml
index fda40b527..9e4412b3b 100644
--- a/stdlib/string.ml
+++ b/stdlib/string.ml
@@ -203,3 +203,23 @@ let rcontains_from s i c =
type t = string
let compare (x: t) (y: t) = Pervasives.compare x y
+
+(* split a string [s] at every char [c], and return the list of sub-strings *)
+let split s c =
+ let len = length s in
+ let rec iter pos to_rev =
+ if pos = len then List.rev ("" :: to_rev) else
+ match try
+ Some ( index_from s pos c )
+ with Not_found -> None
+ with
+ Some pos2 ->
+ if pos2 = pos then iter (pos+1) ("" :: to_rev) else
+ iter (pos2+1) ((sub s pos (pos2-pos)) :: to_rev)
+ | None -> List.rev ( sub s pos (len-pos) :: to_rev )
+ in
+ iter 0 []
+
+let cut_at s c =
+ let pos = index s c in
+ sub s 0 pos, sub s (pos+1) (length s - pos - 1)