title: Java equivalent of Php substr()
body: <p>I am a Java developer but I need a code to be converted from PHP to Java.
  I need a function which gives equivalent result in Java as of PHP fuction substr (as Java doesn't take negative values whereas php takes).</p>
  
  <p>For example:</p>
  
  <pre><code>   $string = "I am not lonely man";
     $sub = $substr($string,1,-3);
  </code></pre>
  
  <p><strong>EDIT: PROPER ANSWER:-</strong></p>
  
  <pre><code>public  String substr(String string,int from,int to){
      if(from &lt; 0 &amp;&amp; to &lt; 0){
          if(Math.abs(from) &gt; Math.abs(to)){
              String s = string.substring(string.length()-Math.abs(from));
              return s.substring(s.length()-Math.abs(to));
          }else{
              return "";
          }
     }else if(from &gt;= 0 &amp;&amp; to &lt; 0){
          String s = string.substring(from);
         if(Math.abs(to) &gt;= s.length()){
             return "";
         }else{
             return s.substring(0,s.length()-Math.abs(to));
         }
  
  
      }else if(from &lt; 0 &amp;&amp; to &gt;= 0){
          String s = string.substring(string.length()-Math.abs(from));
          if(to &gt;= s.length()){
          return s;   
          }
          return s.substring(0,to);
      }else{
          String s = string.substring(Math.abs(from));
          if(to &gt;= s.length()){
              return s;
          }else{
              return s.substring(0, Math.abs(to));
          }
  
      }
  
  }
  </code></pre>
  
  <p>Output: am not lonely </p>