OCAML - strings and substrings

ocaml, string, substring

Solution

With `String` module:

let contains s1 s2 =
  try
    let len = String.length s2 in
    for i = 0 to String.length s1 - len do
      if String.sub s1 i len = s2 then raise Exit
    done;
    false
  with Exit -> true

With `Str` module, like @barti_ddu said check this topic:

let contains s1 s2 =
    let re = Str.regexp_string s2 in
    try 
       ignore (Str.search_forward re s1 0); 
       true
    with Not_found -> false

Problem

Could someone help me to write a function that checks if a string is a substring of another string? (there can be more than only 2 strings) Thanks

Original source

Related problems