2013-06-29 39 views
1

我有一個這樣的字符串:解析字符串並獲得一定的價值

_id:2的thread_id:189 地址:0292 m_size:空的人:0日期:1372494272447 DATE_SENT:0協議:0閱讀:1 status:-1 type:1 reply_path_present:0 subject:null body:好的。回覆消息。 service_center:051108 locked:0 sim_id:0 error_code:0 seen:1 _id:1 thread_id:189 address:292 m_size:null person:0 date:1372493695831 date_sent:0 protocol:null read:1 status:-1 type :2 reply_path_present:空主題:空體:測試消息 service_center:空鎖定:0 SIM_ID:0 ERROR_CODE:0看出:0

我想取出這個字符串的唯一部件,例如地址:0292正文:xyz來自整個字符串。我想從一個非常大的字符串中獲得這兩個實例(以上只是一個示例)。假設它有20000多個字符。

我該如何做到這一點?

+0

使用StringTokenizer。大約20K字符的數量並不是那麼大,仍然低於100K。即使標記它仍然不到1/4兆內存。 –

+0

示例代碼pliss:p我正在考慮使用split,然後搜索數組,但這聽起來不像是最好的方法。 – Asim

回答

0

貌似每address之後是m_size,所以使用string.split()功能,分裂在關鍵字address然後選擇string.substring()(從所得陣列中的每個字符串),直到達到關鍵字m_size。並重復整個事件的關鍵字bodyservice_center。我想不出任何其他的方式。

0

你是對的,它看起來不漂亮。但它的作品:)

String[] splitString = string.split(" "); 
for (int i = 0; i < splitString.length; i++) { 
if (splitString[i].startsWith("body") || splitString[i].startsWith("address")) 
    Log.i(TAG, "Found: " + splitString[i]); 
    // Do whatever you need to do 
}