title: Java: simpler way of cutting off end of string
body: <p>Everytime I encounter this I ask myself the same question: Isn't there a simpler and less annoying way of cutting a string from the end by X characters.</p>
  
  <p>Let's say I got <code>"Helly there bla bla"</code> and - why ever - I need to cut off the last 2 characters, resulting in <code>"Helly there bla b"</code>.
  I now would do the following:</p>
  
  <pre><code>String result = text.substring(0, text.length() - 2);
  </code></pre>
  
  <p>I rather want to do something like:</p>
  
  <pre><code>String result = text.cutOffEnd(2);
  </code></pre>
  
  <p>I know there are many String libraries out there, but don't know many of them and I never saw something like that so I hoped someone of you might know better :)</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p><strong>Q:</strong> Why don't you just build your own util method / class?</p>
  
  <p><strong>A:</strong> I don't want to use an own util method. I don't write a util method for "null or empty" or other trivial things. I go with the opinion that there MUST BE something available already as I would say that tons of people need this kind of function pretty often in their lifetime.
  Plus: I work in many different projects and just want to rely on a simple library call like "Strings.nullToEmpty(str)" etc. I just don't build something like that on my own, although it's trivial.</p>
  
  <p><strong>Q:</strong> why is <code>text.substring(0, text.length() - 2);</code> not good enough?</p>
  
  <p><strong>A:</strong> It's very bulky if you compare it with my desired function. Also, think of that: If you determine the string, it becomes even unhandier:</p>
  
  <pre><code>String result = otherClass.getComplicatedCalculatedText(par1, par2).substring(0,
  otherClass.getComplicatedCalculatedText(par1, par2).length() - 2);
  </code></pre>
  
  <p>Obviously I'd need to use a local variable, which is so unnecessary at this point... As it could simply be:</p>
  
  <pre><code>String result = otherClass.getComplicatedCalculatedText(par1, par2).cutOffEnd(2);
  </code></pre>