title: Find String in Char iterator
body: <p>I have a use case where I need to return a String up to a delimiter String (if found) from an iterator of Char.</p>
  
  <p>The contract:</p>
  
  <ul>
  <li>if iterator is exhausted (only at the begin), return None</li>
  <li>if the delimiter String is found, return all characters before it (empty String is fine), delimiter will be dropped</li>
  <li>else return the remaining characters</li>
  <li><strong>do not eagerly exhaust the iterator!</strong></li>
  </ul>
  
  <p>I do have this working solution, but it feels like Java (which is where I'm coming from)</p>
  
  <pre><code>class MyClass(str: String) {
    def nextString(iterator: Iterator[Char]): Option[String] = {
      val sb = new StringBuilder
      if(!iterator.hasNext) return None
      while (iterator.hasNext) {
        sb.append(iterator.next())
        if (sb.endsWith(str)) return Some(sb.stripSuffix(str))
      }
      Some(sb.toString())
    }
  }
  </code></pre>
  
  <p>Is there a way I can do this in a more functional way (ideally without changing the method signature)?</p>
  
  <p>Update: Here is how I test this</p>
  
  <pre><code>val desmurfer = new MyClass("_smurf_")
  val iterator: Iterator[Char] = "Scala_smurf_is_smurf_great_smurf__smurf_".iterator
  println(desmurfer.nextString(iterator))
  println(desmurfer.nextString(iterator))
  println(desmurfer.nextString(iterator))
  println(desmurfer.nextString(iterator))
  println(desmurfer.nextString(iterator))
  println
  println(desmurfer.nextString("FooBarBaz".iterator))
  println(desmurfer.nextString("".iterator))
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Some(Scala)
  Some(is)
  Some(great)
  Some()
  None
  
  Some(FooBarBaz)
  None
  </code></pre>