从字符串中删除字符串

时间:2011-04-04 12:35:17

标签: android string

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>。任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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);
}

答案 2 :(得分:0)

"<sessionid>"的长度作为startIndex而不是indexOf。

答案 3 :(得分:0)

也可以尝试使用正则表达式;

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