开发者

Remove string from 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);
} 

I am getting session_id = <sessionid>32423jhoijhoijh; so I want to remove <sessionid>. Any help will be appreciated.


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


Just remove the first 11 letters or characters from the 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);
   session_id = session_id.substring(11, session_id.length());
   ConstantData.session_id =session_id;
   Log.i("SessionId", ""+session_id);
}


Take the length of "<sessionid>" as your startIndex instead of indexOf.


One might try regular expressions too;

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜