2016-06-21 5 views
-3
class temp 
{ 
    public static void main(String []args) 
    { 
     String [] arp = new String [] {"Sarah is a good girl","Sarah is a bad girl"} ;// I only want to print "good" from this array and not the entire string in the below println statement. 

     System.out.println(arp[0]);//this will print the entire string on element[0] while i only want to print "good" from that string. 
    } 
} 

Wenn dieses Problem gelöst ist, wird meine vorherige Frage ebenfalls gelöst.Wie drucke ich nur bestimmte Details aus dem String-Array?

+1

Blick in 'substring' – Idos

+0

' System.out.println ("gut") '? Scheint, du musst in deiner Frage spezifischer sein. Willst du vielleicht das X in "Sarah ist ein X Mädchen" oder sogar "Y ist ein X {Mädchen/Junge}" identifizieren? –

+0

@Samon Fischer Danke für deine Antwort, aber diese Saite ist nur ein Beispiel, da könnte alles an diesem Ort nicht nur gut sein. Ich möchte eine Technik oder eine Methode, bei der ich nur den vierten Teil drucken kann, wo "gut" steht. "Dies ist ein schönes Haus" Ich möchte nur "Haus", das ist das fünfte Wort oder "gut", das vierte Wort ist. Ist das möglich, was ich frage? – user25142514

Antwort

0

Verwenden Sie einfach eine if-Anweisung und String.contains

String[] strs = ...; 
for (int i = 0; i < strs.length; i++) { 
    if (strs[i].contains("good")) System.out.println("good"); 
} 
+0

Dank ... Gute handliche Methode ... Es wird meine Probleme lösen ... – user25142514

0

Sie können auch String split() Funktion verwenden.

Beispiel:

String [] arp = new String [] {"Sarah is a good girl","Sarah is a bad girl"} ; 

    for (int i = 0; i < arp.length; i++) { 
    String data = arp[i]; 
    String[] ex = data.split(" "); // You trim to remove leading and trailing spaces here 
    System.out.println(ex[0]); //prints Sarah 
    System.out.println(ex[1]); //prints is 
    System.out.println(ex[2]); //prints a 
    System.out.println(ex[3]); //prints good 
    System.out.println(ex[4]); //prints girl 
} 
0

Es wird funktionieren.

class temp{ 
    public static void main(String []args){ 
     String arp[] ="Sarah is a good girl","Sarah is a bad girl" ; 
     System.out.println(arp[3]); 
    } 
} 

https://ideone.com/5mDnZS

+0

Wow ... Beeindruckend. Vielen Dank – user25142514

Verwandte Themen