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);
} 

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

3
  • 1
    try String session_id = strResponse.substring(startLocation+11, endLocation); here 11 is length of <sessionid> Commented Apr 4, 2011 at 12:44
  • exact duplicate of How can I remove a part of a string? Commented Apr 4, 2011 at 12:52
  • Please grant the answer flag to the person who provided a suitable solution for you. Commented Nov 11, 2012 at 16:00

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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);
}

Comments

0

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

Comments

0

One might try regular expressions too;

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.