77

I have two strings in scala and I want to find out, if the bigger string (needle) contains a smaller string (haystack).

What I found is doing it with regexps and matches like this (from this question):

needle.r.pattern.matcher(haystack).matches

which is (1) grossly overcomplicated for such a simple problem, but more importantly, (2) doesn't work for me, because

"needle".r.pattern.matcher("Finding needle in haystack").matches

returns

Boolean = false

Mario Galic
  • 47,285
  • 6
  • 56
  • 98
Karel Bílek
  • 36,467
  • 31
  • 94
  • 149

2 Answers2

118

If you want to do it with maximum efficiency, you may have to write it yourself (or find a good substring searching algorithm somewhere). If you just want it to work at all, then in Scala:

scala> "Finding needle in haystack" contains "needle"
res0: Boolean = true

scala> "Finding needle in haystack" indexOf "needle"
res1: Int = 8

These are not regex searches. You aren't using the regex match correctly either (edit: because that code asks for an exact match to the whole string, not to find a matching substring), but that's a different issue. If you want a count of the number of matches, you can do something like

scala> "needle".r.findAllIn("Finding needle in haystack").length
res2: Int = 1
Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
  • 11
    If anyone's wondering, `contains` and `indexOf` are just methods on `java.lang.String` and there is no Scala magic going on here. I haven't looked at the source but I expect they're well-optimized methods. – Luigi Plinge Apr 12 '12 at 19:07
  • 1
    And what's so slow or unoptimized about them? – matanster Sep 09 '14 at 07:03
19

Although answered I thought I would also offer this regex style

scala> "I have a needle in my haystack" matches ".*needle.*"
res10: Boolean = true
Neil Chambers
  • 393
  • 1
  • 2
  • 8
  • 1
    This is slow, and only works if there are no line terminators. – Ed Staub Apr 12 '12 at 17:58
  • 1
    Compared to the other answer. indexOf() (which contains() uses) is well-optimized both at Java level and within the JVM. – Ed Staub Apr 12 '12 at 21:20
  • 4
    @EdStaub it's not the subject of the question, but this answer is still relevant if regex is necessary. indexOf and contains don't seem to work with regex. – User Oct 01 '13 at 21:24