将推文分解为子字符串并在窗口小部件中显示

时间:2013-03-18 13:07:58

标签: java android twitter widget string-split

这些天我正试图为我学校的板球比赛制作一个记分牌小部件。而且我已经创建了一个小部件,可以从twitter获取消息。现在我需要将消息分成几部分并显示在小部件中。

例如 - >当小部件收到推文时

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

我希望我的小部件显示

          Home = 60 wickets= 8
          Guest = 50 wickets=5 (Batting)

2 个答案:

答案 0 :(得分:1)

使用Java?看看

String.split()

如果你的图案是固定的,你应该能够按位置拆分和访问这些图块。 如果模式更具动态性,则必须使用

的组合
String.subString() / String.indexOf() 

答案 1 :(得分:0)

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);
}

输出:

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