-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.

digEmAll
  • 56,430
  • 9
  • 115
  • 140
Manjula
  • 61
  • 1
  • 1
  • 5

2 Answers2

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • +1 for `it does not alter the original String`. – RanRag Feb 11 '12 at 10:26
  • but my question is in your code only IMEI number only displayed.. and what about others and how can i split remaning string – Manjula Feb 11 '12 at 10:31
  • 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`. – Andrew Thompson Feb 11 '12 at 10:34
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

Jörg Beyer
  • 3,631
  • 21
  • 35
  • but my question is in your code only IMEI number only displayed.. and what about others and how can i split remaning string – Manjula Feb 11 '12 at 10:30
  • @Manjula add subsequent `substring` function calls for the other data, like `string timestamp = str.substring(34,42);` – Matten Feb 11 '12 at 10:38
  • i tried but its getting error like Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 42 – Manjula Feb 11 '12 at 10:43
  • 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. – Jörg Beyer Feb 11 '12 at 10:46