title: Java substring using lastindexOf return a specific length
body: <p>I'm using JAVA and I have a string called example that looks like;</p>
  
  <pre><code>example = " id":"abcd1234-efghi5678""tag":"abc" "
  </code></pre>
  
  <p>NOTE: I have't escaped the "'s using \ but you get the idea..</p>
  
  <p>...I want to just return;</p>
  
  <p>abcd1234</p>
  
  <p>...I've been trying using substring</p>
  
  <pre><code>example = (example.substring(example.lastIndexOf("id\":\"")+5));
  </code></pre>
  
  <p>(because this string could be anywhere in a HTML/JSON File) which kinda works all the lastIndexOf does is find it and then keep everything AFTER it - i.e it returns;  </p>
  
  <p>abcd1234-efghi5678""tag":"abc" </p>
  
  <p>Basically I need to find the lastIndexOf based on the string and limit it returns afterwards - I found that I could do it another substring command like this;</p>
  
  <pre><code>example = (example.substring(example.lastIndexOf("id\":\"")+5));
  example = example.substring(0,8);
  </code></pre>
  
  <p>...but it seems messy. Is there any way of using lastIndexOf and also setting a max length at the same time - it's probably something really simple that I can't see due to staring at it for so long.</p>
  
  <p>Many thanks in advance for your help!</p>