OCAML - 字符串和子字符串

pri*_*ima 2 string ocaml substring

有人能帮我写一个函数来检查一个字符串是否是另一个字符串的子字符串吗?

(可以有超过 2 个字符串)

谢谢

cag*_*ago 6

String模块:

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
Run Code Online (Sandbox Code Playgroud)

使用Str模块,就像@barti_ddu 说的检查这个话题

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