2011-04-04 300 views
2
String startTag = "<sessionid>"; 
String endTag = "</sessionid>";          
if (startTag.equalsIgnoreCase("<sessionid>") && 
    endTag.equalsIgnoreCase("</sessionid>")) 
{ 
    int startLocation = strResponse.indexOf(startTag); 
    int endLocation = strResponse.indexOf(endTag); 
    Log.i("StartLocation", ""+startLocation); 
    Log.i("EndLocation", ""+endLocation); 
    String session_id = strResponse.substring(startLocation, endLocation); 
    ConstantData.session_id =session_id; 
    Log.i("SessionId", ""+session_id); 
} 

我得到session_id = <sessionid>32423jhoijhoijh;所以我想刪除<sessionid>。任何幫助將不勝感激。從字符串中刪除字符串

+1

嘗試String session_id = strResponse.substring(startLocation + 11,endLocation);這裏是11的長度是 mudit 2011-04-04 12:44:18

+0

確切的重複[如何刪除一部分字符串?](http://stackoverflow.com/questions/5538271/how-can-i-remove-a-part-of-a -string) – 2011-04-04 12:52:43

+0

請將答案標誌授予爲您提供合適解決方案的人員。 – JJD 2012-11-11 16:00:54

回答

2

int startLocation = strResponse.indexOf(startTag) + string length of startTag

0

"<sessionid>"長度爲您的startIndex代替的indexOf。

1

只是刪除字符串中的第11個字母或字符:

String startTag = "<sessionid>"; 
String endTag = "</sessionid>";          
if (startTag.equalsIgnoreCase("<sessionid>") && 
    endTag.equalsIgnoreCase("</sessionid>")) 
{ 
    int startLocation = strResponse.indexOf(startTag); 
    int endLocation = strResponse.indexOf(endTag); 
    Log.i("StartLocation", ""+startLocation); 
    Log.i("EndLocation", ""+endLocation); 
    String session_id = strResponse.substring(startLocation, endLocation); 
    session_id = session_id.substring(11, session_id.length()); 
    ConstantData.session_id =session_id; 
    Log.i("SessionId", ""+session_id); 
} 
0

一個可以嘗試的正則表達式太;

String str = "<sessionid>ABCDEFGH</sessionid>"; 
    str = str.replaceFirst("<sessionid>(\\S+)</sessionid>", "$1");