Zulip Chat Archive

Stream: Is there code for X?

Topic: String.isInfixOf


Joachim Breitner (Aug 03 2023 at 11:43):

Is there a function String → String → bool that tells me whether s1 is a substring of s2?

Adam Topaz (Aug 03 2023 at 12:37):

see https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/String.2EcontainsSubstring/near/371008174

Adam Topaz (Aug 03 2023 at 12:38):

the shortest implementation seems to be s1.toList <:+: s2.toList

Adam Topaz (Aug 03 2023 at 12:39):

(that's a prop though)

Joachim Breitner (Aug 03 2023 at 12:49):

Thanks. I just wrote

def String.isInfixOf (needle : String) (hey : String) := Id.run do
  let mut i := hey.mkIterator
  while not i.atEnd do
    if needle.isPrefixOf i.remainingToString
    then return true
    else i := i.next
  return false

to be unblocked for now, and will maybe use https://github.com/leanprover/std4/pull/178 when it lands.

Joachim Breitner (Aug 03 2023 at 12:50):

(This thread is resolved for me…)


Last updated: Dec 20 2023 at 11:08 UTC