title: remove characters before and after character in java
body: <p>I need to delete all characters before and after a specific character using java programming
  for example:</p>
  
  <p><strong>input:</strong> <code>ab*cd</code> <strong>output:</strong><code>ad</code></p>
  
  <p><strong>input:</strong> <code>ab**cd</code> <strong>output:</strong><code>ad</code></p>
  
  <pre><code>import java.util.Scanner;
  
  public class Starstring {
      public static void main(String[] args) {
          String str1;
          String res="";
          int n,i=0;
  
          Scanner sc=new Scanner(System.in);
          System.out.println("Enter the string");
          str1=sc.next();
          res=str1;
          StringBuffer a = new StringBuffer(str1);
          n=str1.length()-1;
          for(i=0;i&lt;n;i++)
          {
              if(str1.charAt(i)=='*')
              {
                  res=a.delete(i-1, i+2).toString();
              }
          }
          System.out.println("The final string is"+res);
      }
  
  }
  </code></pre>
  
  <p>I am getting expected output for <code>case 1</code> but <code>case 2</code> wrong output with ac as output.I don't know where I am going wrong.Someone please help me.Thanks in advance:)</p>