-5

Can anyone explain through sample code how to split string? And below I pasted the string

String str="000F33353238343830323038353239323300000133B5150A8C002E3C188007C4D950039300A1090000F0080301000200F001030900010A000018000002480000013DC70000000000";

Given string first 34 bits are IMEI number and next 8 bits timestamp and so on.

3
  • you mean bytes? use substring() with appropriate parameters. Commented Feb 11, 2012 at 10:23
  • 5
    "explain through sample code" SO is not a code factory. Commented Feb 11, 2012 at 10:25
  • Take a look at here, might help you. Commented Feb 11, 2012 at 10:27

2 Answers 2

3

Use String.substring(int,int).

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1.

Note the parts Returns, which means it does not alter the original String.

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

3 Comments

+1 for it does not alter the original String.
but my question is in your code only IMEI number only displayed.. and what about others and how can i split remaning string
You seem to have copy/pasted that comment from your reply to Jörg Beyer. But the response is the same for either. Use the method multiple times, with different ranges of int.
3

you can use the String method substring

substring(int beginIndex, int endIndex) 

beginIndex is inclusive while endIndex is exclusive. subString returns a copy of a part of the origrianlstring back. so:

String part = original.substring(beginIndex, endIndex);

or:

String part = "abcdefgh".substring(2,4) // part will be "cd"

in your case:

String imei = str.substring(0, 34);
String timestamp = str.substring(34,34+8);
... do that for any part you want to extract.

asuming you ment bytes where you wrote bits.

for further insights you might read the documentation on Java Strings, http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html

4 Comments

but my question is in your code only IMEI number only displayed.. and what about others and how can i split remaning string
@Manjula add subsequent substring function calls for the other data, like string timestamp = str.substring(34,42);
i tried but its getting error like Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 42
You will get the StringIndexOutOfBoundsException if the beginIndex or the endIndex is outside the string. In your case the original string (called "str" in your question) is shorter than 42 characters. I will guide you through, when you show the code that failed and the output. You can append that in your question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.