2013-03-18 32 views
-3

這些天我正在嘗試爲我的學校的板球比賽制作記分板小部件。我已經創建了一個從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) 
+0

哦,你可以給我按摩?我的背痛。 ':P'(消息的固定拼寫) – Doorknob 2013-03-18 13:08:58

+0

您的模式已修復? – 2013-03-18 13:12:46

+0

是的..它更像是一個堅實的形式 – AACaN 2013-03-18 13:15:10

回答

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) 
+1

thanx Achintya。這應該工作。 – AACaN 2013-03-20 13:05:03

+0

@AACaN不客氣。 – 2013-03-20 13:20:05

1

使用Java?看看

String.split() 

如果你的模式是固定的,你應該能夠按位置拆分和訪問塊。 如果圖案更加動感,你將不得不使用

String.subString()/String.indexOf() 
+0

請你能解釋一下,用我的例子。 – AACaN 2013-03-18 13:21:26

相關問題