title: Get the line number of a string from body of text
body: <p>Let's say I have a file with two lines and I get the <code>indexOf</code> a substring in the file. It returns with <code>18</code> the character that the substring is found at. How can I find the line with this information?</p>
  
  <pre><code>var file = [
    'var foo = "hello"',
    'console.log(foo)',
  ].join('\n')
  
  var char = file.indexOf('console') // =&gt; 18
  var line = lineOfChar(file, char) // =&gt; 2
  </code></pre>