duplicate: http://stackoverflow.com/questions/527595/split-string

hi, i'm have string like this


i want to split this


and store int Array when 0x01 value occurs in the string b'coz string length is not fixed. i have done splitting like this char 0x01; String packetArray[] = packets.split(packets,sp); but its showing array size 1 only means its not splitting the data,plz help me to solve the problem.

i want to split string by hexadecimal value not by dot or any charecter

thank u for reply.

+1  A: 

Why would you split by character U+0001? Don't you want to split by dot? Or are you using a dot in the question everywhere you'd actually have U+0001? Even so, it should still work if you use String.split correctly:

public class Test
    public static void main(String args[]) throws Exception
        char delimiter = 1;

        String test = "first" + delimiter + "second";

        String[] bits = test.split(String.valueOf(delimiter));

Prints 2.

Note that your use of String.split is wrong at the moment - you're trying to use the whole text as a delimiter! I suspect if you do:

String packetArray[] = packets.split(String.valueOf(sp));

you may find it just works immediately.

Jon Skeet
i want to split by hexadecimal valuenot by dot
What do you mean by "split by hexadecimal value"?
Jon Skeet
@umesh: Huh? char delimiter=0x????; Where ???? is the hex value of whatever char you want to split with.
Software Monkey
@Software Monkey: Agreed - the question is really confusing at the moment.
Jon Skeet
200209151422010231.10408360.00502240.105090.0200209151423010231.10408360.00502289.605090.0first caharecter is hexadecimal value i.e 0x01
1 is the same as 0x01. so when you set char delimiter = 1; that's setting it to 0x01.
boxes in the strings are hexadecimal values
but decimal value 1 and hexadecimal value 0x01 are different while comapring,i cant set delimeter=1 b'coz there is decimal 1 values in the string
@umesh: hex values are numbers simply written in base 16 instead of base 10.
David Grant
String packetArray[] = packets.split(String.valueOf(sp));this is working thank u very much for the reply
@umesh: There's a difference between 1 and '1'.
Jon Skeet
@umesh (but there's no difference between 1 and 0x1)
Jon Skeet
+1  A: 

I'd think twice about using split since those are obviously fixed width fields.

I've seen them before on another question here (several in fact so I'm guessing this may be homework (or a popular data collection device :-)) and it's plain that the protocol is:

  • STX (0x01).
  • 0x0f.
  • date (YYMMDD or DDMMYY).
  • time (HHMMSS).
  • 0x02.
  • value (XXXXXX.X).
  • 0x03.
  • 0x04.

And, given that they're fixed width, you should probably just use substrings to get the information out.

+1 for the déjà vu.
Zach Scrivena