Pregunta

These days I'm trying to make a scoreboard widget for my school's cricket match. And I Already created a widget which get's messages from twitter. now i need to separate the message into pieces and display in the widget.

For Example-> when a tweet received by the widget as

          Home scored 50 for 8 wickets and guest scored 60 for 5 wickets and batting.

I want my widget to display

          Home = 60 wickets= 8
          Guest = 50 wickets=5 (Batting)
¿Fue útil?

Solución 2

public static void main(String[] args) {
    String s = "Home scored 50 for 8 wickets and guest scored 60 for 5 wickets and batting.";
    String result = "";
    int homeScored = s.indexOf("scored");
    int guestScored = s.lastIndexOf("scored");
    int homeWicket = s.indexOf("for");
    int guestWicket = s.lastIndexOf("for");

    result = "home =" + s.substring(homeScored + 6, homeWicket)
            + "wicket ="
            + s.substring(homeWicket + 3, s.indexOf("wickets")) + "\n"
            + "guest = " + s.substring(guestScored + 6, guestWicket)
            + "wicket ="
            + s.substring(guestWicket + 3, s.lastIndexOf("wickets"))
            + "(batting)";
    System.out.println(result);
}

Output:

home = 50 wicket = 8 
guest =  60 wicket = 5 (batting)

Otros consejos

Using Java? Take a look at

String.split()

If your pattern is fixed you should be able to split and access the pieces by position. If the pattern is more dynamic, you will have to use combinations of

String.subString() / String.indexOf() 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top