2012-04-28 49 views
3

我創建使用J2ME,ASP和WML移動網站。子串和索引

我從DB讀取並創建一個字符串,如下面的,所以我以後可以稱呼它。

   string id = reader[0].ToString(); 
       string name = reader[1].ToString(); 
       Response.Write("#" + name + id); 
在J2ME中

我記得它作爲

stringItem3.setText("Welcome "+result.substring(result.indexOf("#")+1) 
    + "ID " + result.substring(result.indexOf("#")+2)); 

,結果並不像我想我試圖改變很多的語法,但沒有運氣。

結果始終顯示爲

enter image description here

而我真正想展示的是類似

Welcome (name) ID : (id) 
or 
Welcome Arin ID : 20111 

什麼是得到正確結果的最佳方式是什麼?

而且我後面能保存在內部J2me的一個字符串值的ID後使用它(是/否)?


我改變了字符串(解決)

string id = reader[0].ToString(); 
string name = reader[1].ToString(); 
Response.Write("*"+ id + "#" + name); 

和獲取信息

stringItem3.setText("Welcome "+result.substring(result.indexOf("#")+1) 
    + "ID " + result.substring(result.indexOf("*")+1,result.indexOf("#"))); 

result.substring(result.indexOf("*")+1,result.indexOf("#"))); 

這是當平均獲得之間的 「*」 的字符和 「#」

現在結果如下圖所示。

enter image description here

回答

2

你節省的樣子#Arin20111的字符串,所以indexOf("#")+2#後2 第二字符。

所以result.substring(result.indexOf("#")+1)#,這是名稱 ID後整個字符串。並且result.substring(result.indexOf("#")+1)與開始時的A除外是相同的字符串。

如果你知道這個名字將永遠不會包含#,你可能會在字符串存儲爲#Arin#20111。那麼這個名字就是在<sup>st</sup>and before the 2<sup>nd</sup>, and the ID is everything after the 2<sup>nd</sup>#`之後的所有東西。

+0

謝謝你的提示,我貼我的問題一樣。 – arin 2012-04-28 14:15:13

+1

不錯的工作,阿琳! – 2012-04-28 14:18:30