2017-11-17 3 views
1

ich vor alle Zeichen löschen muß und nach einem bestimmten Zeichen Java-Programmierung zum Beispiel mit:Zeichen vor und nach dem Charakter in Java entfernen

Eingang:ab*cdAusgang:ad

Eingang :ab**cdAusgabe:ad

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<n;i++) 
     { 
      if(str1.charAt(i)=='*') 
      { 
       res=a.delete(i-1, i+2).toString(); 
      } 
     } 
     System.out.println("The final string is"+res); 
    } 

} 

Ich erhalte erwartete Ausgabe für case 1 aber case 2 falschen Ausgang mit ac als output.I weiß nicht, wo ich wrong.Someone werde bitte helfen me.Thanks im Voraus :)

+1

Warum ein regex ersetzen nicht tun? d. h. ersetzen Sie das Muster '. \ * +.' durch eine leere Zeichenfolge? – Vasan

+0

Ich bin neu in Java und String so sagen Sie mir im Detail. Haben wir Funktion namens Regex ersetzen – HariPriya

Antwort

0

Verwenden regex ersetzen mehr als eine * mit einzelnen *. Ihr Code wird wie unten sein:

input:ab*cd output:ad 
input ab**cd ouput:ad 
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; 
     str1 = str1.replaceAll("[*]+", "*");// add this line 
     StringBuffer a = new StringBuffer(str1); 
     n=str1.length()-1; 
     for(i=0;i<n;i++) 
     { 
      if(str1.charAt(i)=='*') 
      { 
       res=a.delete(i-1, i+2).toString(); 
      } 
     } 
     System.out.println("The final string is"+res); 
    } 

} 
+0

Vielen Dank, dass dies funktioniert :) – HariPriya

+0

Gern geschehen. – Sattyaki

0

Sie können Regex verwenden, um Ersetzen Sie die Zeichengruppe in Ihrer Zeichenfolge. So entfernen Sie * und 1 Zeichen vor & nachdem es:

Pattern pt = Pattern.compile(".\\*+."); 
Matcher match = pt.matcher(input); 
String output = match.replaceAll(""); 
//System.out.println(output); 
0

Sie es wie folgt tun:

String str1; 
    int n; 

    Scanner sc=new Scanner(System.in); 
    System.out.println("Enter the string"); 
    str1=sc.next(); 

    sc.close(); 

    StringBuffer a = new StringBuffer(str1); 
    n=a.length(); 
    for(int i=0;i<n;i++) 
    { 
     if(str1.charAt(i)=='*') 
     { 
      int aa = i-1; 
      int bb = i+1; 
      for(;bb<n; bb++){ 
       if(str1.charAt(bb)!='*'){ 
        break; 
       } 
      } 
      a.delete(aa, bb+1); 
      i = bb; 

     } 
    } 
    String res = a.toString(); 
    System.out.println("The final string is"+res); 

Aber eine bessere Java regex Weg ist:

Nach Wert von Scanner zu lesen -

String res2 = str1.replaceAll(".[\\*]+.", ""); 
    System.out.println("The final string is"+res2); 
0

Mit @Vasan Regex .. unten ist der Code.

public static void main(String[] args) { 
     String str1; 
     String res=""; 
     int n; 

     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; 

     String[] splitVal = str1.split(".\\*+."); 
     String newString =""; 
     for(int i=0; i<splitVal.length; i++){ 
      newString = newString + splitVal[i]; 
     } 

     System.out.println("The final string is "+newString); 
    } 

Ich hoffe, Sie verstehen.

0

Try this:

public static void main(String[] args) { 
    System.out.println("Enter the string"); 
    Scanner sc = new Scanner(System.in); 
    String str1 = sc.nextLine(); 
    String[] separated = str1.split(".\\*+."); 
    String result = ""; 
    for(String str : separated) { 
     result += str; 
    } 
    System.out.println(result); 
} 
0

was ist, wenn Eingabe: * abc * oder ab * c * de

+0

* abc * Testfall kann ignoriert werden – HariPriya

Verwandte Themen