Вопрос

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)
Это было полезно?

Решение 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)

Другие советы

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() 
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top