Module StrExtras


module StrExtras: sig  end
String functions



String construction


val combine : string list -> string
Concatenates a list of strings with a blank seperator
val implode : char list -> string
Turn a list of characters into a string.
val of_array : char array -> string
Turn an array of characters into a string.
val map : (char -> char) -> string -> string
Like Array.map, for strings. Returns a newly allocated transformed string.
val mapi : (char -> int -> char) -> string -> string
Like map, but passes the index as well as the character.
val subpos : string -> int -> int -> string
subpos string startp endp returns the substring of string starting at position start and ending at position end. subpos "foo." 0 2 returns "foo", for example.
val repeat : string -> int -> string
repeat s n returns a new string made up of n copies of s.


Processing characters


val iteri : (char -> int -> unit) -> string -> unit
Like String.iter, but passes the index of the character as well.
val fold_left : ('a -> char -> 'a) -> 'a -> string -> 'a
fold_left f x s computes f (... (f (f x s.[0]) s.[1]) ...) s.[n-1] where n is the length of the string s.
val fold_right : ('a -> char -> 'a) -> string -> 'a -> 'a
fold_right f s x computes f s.(0) (f s.(1) ( ... (f s.(n-1) x) ...)), where n is the length of the string s.
val ensure : (char -> bool) -> string -> bool
ensure f s returns true if f is true for all characters in s. It stops after the first false result, making it more efficient than StrExtras.fold_left for validation.
val ensure_range : (char -> bool) -> string -> int -> int -> bool
ensure_range f s i len applies f to the len-gth characters in s starting at index i and returns true if f is true for all the characters, otherwise false.


String manipulation



Most of these functions return newly allocated strings that are modified versions of their arguments

val explode : string -> char list
Turn a string into a list of characters.
val to_array : string -> char array
Turn a string into an array of characters.
val center : ?pad:char -> ?trunc:bool -> string -> int -> string
center str length returns str centered in a length-character line. The default padding character is space. If trunc is true and the string is longer than length, the string is truncated.
val ljust : ?pad:char -> ?trunc:bool -> string -> int -> string
ljust str length adds padding characters to the end of str if needed so it is len characters long. The default padding character is a space. If trunc is true, and the string is longer than length characters, it is cut off. The default is to return a copy of string if it is longer than length.
val rjust : ?pad:char -> ?trunc:bool -> string -> int -> string
Just like rjust, but padding is added to the start of the string if needed.
type trim_style = [ `Both | `Left | `Right ] 
The direction that trim cuts characters from.
val trim : ?style:trim_style -> string -> char -> string
trim string character removes any leading and trailing occurances of character from string. style controls wether they're removed from just the front or back.
val map_inplace : (char -> char) -> string -> unit
Like map, but modifies the argument string.
val mapi_inplace : (char -> int -> char) -> string -> unit
Like map_inplace, but passes the index of the character as well.
val rev : string -> string
Returns a reversed version of the string


Trimming off bits


val first_word : string -> string
Returns the first word of a string
val cut_first_char : string -> string
Cuts off the first character of a string and returns the rest
val cut_first_n : string -> int -> string
Cuts off the first n characters of a string and returns the rest.
val cut_last_char : string -> string
Cuts off the last character of a string and returns the rest
val cut_last_n : string -> int -> string
Cuts off the last n characters of a string and returns the rest
val cut_first_word : string -> string
Cuts off the first word of a string and returns the rest
val split_at : str:string -> sep:char -> string
Returns everything after the character
val chomp : string -> string
Remove all trailing whitespace from a string
val right : string -> int -> string
Return the rightmost n characters of a string
val left : string -> int -> string
Return the leftmost n characters of a string


Capitalization


val uppercase : string -> string
Like String.uppercase, but locale-dependant
val lowercase : string -> string
Like String.lowercase, but locale-dependant
val capitalize : string -> string
Like String.capitalize, but locale-dependant
val uncapitalize : string -> string
Like String.uncapitalize, but locale-dependant
val titlecase : string -> string
Lowercases all but the first letter in each word in the string, which is uppercased.


Searching


val first_of : string -> string -> int
first_of needle haystack returns the position of the first occurance in haystack of a character in needle. Like C strcspn().
Raises Not_found if no characters in needle are in haystack
val first_of_from : string -> string -> int -> int
first_of_from needle haystack pos returns the position of the first occurance in haystack (Starting at pos) of a character in needle.
Raises Not_found if no characters in needle are in haystack
val last_of : string -> string -> int
last_of needle haystack returns the position of the last occurance in haystack of a character in needle.
Raises Not_found if no characters in needle are in haystack
val last_of_from : string -> string -> int -> int
last_of_from needle haystack pos returns the position of the last occurance in haystack (Starting to look at pos) of a character in needle.
Raises Not_found if no characters in needle are in haystack
val first_not_of : string -> string -> int
first_not_of needle haystack returns the position of the first occurance in haystack of a character that's not also in needle. Like C strspn().
Raises Not_found if all characters in needle are in haystack
val first_not_of_from : string -> string -> int -> int
first_not_of_from needle haystack pos returns the position of the first occurance in haystack (Starting at pos) of a character that's not also in needle.
Raises Not_found if all characters in needle are in haystack
val last_not_of : string -> string -> int
last_not_of needle haystack returns the position of the last occurance in haystack of a character that's not also in needle.
Raises Not_found if all characters in needle are in haystack
val last_not_of_from : string -> string -> int -> int
last_not_of_from needle haystack pos returns the position of the last occurance in haystack (Starting at pos) of a character that's not also in needle.
Raises Not_found if all characters in needle are in haystack
val prefix : string -> string -> bool
prefix pref str returns true if str starts with pref.
val suffix : string -> string -> bool
suffix suf str returns true if str ends with suf.
val index_substr : string -> string -> int
index_substr needle haystack returns the position in haystack where needle starts.
Raises Not_found if the substring isn't present.
val index_substr_from : string -> string -> int -> int
index_substr_from needle haystack pos returns the position in haystack where needle starts, starting looking at pos.
Raises
val match_substr : string -> string -> int -> bool
match_substr substr str pos returns true if str contains substr at position pos
Raises Invalid_argument if pos is out of range
module FastSearch: sig  end
The functions in this module are for fast searching for the same substring multiple times.
val common_prefix : string list -> string
Returns the longest prefix all the strings in the list have in common. For example, common_prefix ["foobar"; "foobaz"; "food"] returns "foo".


Replacing


val replace : string -> string -> string -> string
StrExtras.replace str old new replaces every instance of old with new in str and returns a new string with the changes.


Comparison


val collate : string -> string -> int
collate a b compares two strings like String.compare, but using the `LC_COLLATE locale via the C strcoll() function.
val compare_insensitive : string -> string -> int
compare_insensitive a b compares two strings in a case-insensitive manner, using the current `LC_CTYPE locale.


Modules for comparision

These modules can be used as the input to functor like Map.Make or Hashtbl.Make

module Collate: sig  end
Comparision using StrExtras.collate.
module CaseInsensitive: sig  end
Comparision using StrExtras.compare_insensitive.