File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Java String parsing help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java String parsing help" Watch "Java String parsing help" New topic
Author

Java String parsing help

Prasanth Duggirala
Ranch Hand

Joined: Dec 22, 2006
Posts: 36
Hi All,

i have been struck on String parsing.

i have a string like this given bellow.
String s="01043000000002010000481C01000000000000000000000000000000000000000000000000000
0000000000000000000000000007F25FC0000000000004149040000000000005D2174000000
02040000005D49740000000000005D1D740000000000004175040000000000007F55FC000000000
000001C000000000000007DE6A8000000000000521484000000000000239A780000000000
0304007A80D0000000000000572A54000000000000005F240000000000007F45880000000000004
13F240000000000005D54900000000000005D64900000000000005D4A7000000000000041
040460D00000000000007F7A78000000000000000000000000000000000000000000000000000000000000";

it will changed according to the data.

but some values will changes this values will be add to the string like 0104,0204,0304,0404 these will be present in the string. the length also will be changes.

i want to sort them. some times 0204 will come first or 0304 will second. i have parse it and sort it.

please help me in this issue.

thanking you.

Thanks & Regards,<br />Prasanth Duggirala.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Please show us what you have got so far. Are you splitting the String, and, if so, how?
Prasanth Duggirala
Ranch Hand

Joined: Dec 22, 2006
Posts: 36
public class Test
{
public static void main(String args[])
{
String s="01043000000002010000481C010000000000000000000000000000000000000000000000000
000000000000000000000000000007F25FC0000000000004149040000000000005D21740000000
2040000005D49740000000000005D1D740000000000004175040000000000007F55FC00000000000
0001C000000000000007DE6A8000000000000521484000000000000239A7800000000000304007A80
D0000000000000572A54000000000000005F240000000000007F4588000000000000413F2400
00000000005D54900000000000005D64900000000000005D4A7000000000000041040460D00
000000000007F7A78000000000000000000000000000000000000000000000000000000000000";
int fst=s.length()/4+17;
int snd=s.length()/2+33;
int trd=s.length()/1-86;
//int frt=s.length()-16;
//System.out.println(fst+" "+snd+" "+trd+" ");
System.out.println(s.length());
System.out.println("First "+s.substring(4,fst));
System.out.println("Second "+s.substring(156,snd));
System.out.println("Third "+s.substring(308,trd));
System.out.println("Fourth "+s.substring(460,s.length()));
}
}

out put
--------
First 3000000002010000481C01000000000000000000000000000000000000000000000000000000
0000000000000000000000007F25FC0000000000004149040000000000005D2174000000
Second 0000005D49740000000000005D1D740000000000004175040000000000007F55FC0000000000000
01C000000000000007DE6A8000000000000521484000000000000239A780000000000
Third 007A80D0000000000000572A54000000000000005F240000000000007F4588000000000000413F2
40000000000005D54900000000000005D64900000000000005D4A7000000000000041
Fourth 60D00000000000007F7A7800000000000000000000000000000000000000000000000000
0000000000
Prasanth Duggirala
Ranch Hand

Joined: Dec 22, 2006
Posts: 36
I did like this but i am thinking some other way would be dynamically update string.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You can't dynamically update Strings; you would need a StringBuilder for that.
Your method is error-prone, because it depends on the String having a constant length. How many 0s do you have? Can you use the String#split method to split on a sequence of 0s?
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
I don't understand what you are trying to do. Can you explain in detail how you are paring this String? Perhaps you can give a couple of examples instead of just one?
Prasanth Duggirala
Ranch Hand

Joined: Dec 22, 2006
Posts: 36
Hi All,

What we are trying to do is we are reading a 4 messages at a time by
differentiating it with 0104 is first message like goes on 0404 some time order may goes wrong that why we are using this id like 0104.

what i want to do is.
sorting by 0104 and keeping into first 0204 as second and 0304 0404 as a sorting manner and make this stings as a single.

by removing the code 0104,0204,0304,0404.

i was struck up here.

Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
[quote=Prasanth Duggirala]Hi All,

What we are trying to do is we are reading a 4 messages at a time by
differentiating it with 0104 is first message like goes on 0404 some time order may goes wrong that why we are using this id like 0104.

what i want to do is.
sorting by 0104 and keeping into first 0204 as second and 0304 0404 as a sorting manner and make this stings as a single.

by removing the code 0104,0204,0304,0404.

i was struck up here.

[/quote]

Can't you simply do:

[code]String s="01043000000 .... ";
s = s.replace("0104", "First");
// etc.[/code]

?
Prasanth Duggirala
Ranch Hand

Joined: Dec 22, 2006
Posts: 36
[quote=Piet Verdriet][quote=Prasanth Duggirala]Hi All,

What we are trying to do is we are reading a 4 messages at a time by
differentiating it with 0104 is first message like goes on 0404 some time order may goes wrong that why we are using this id like 0104.

what i want to do is.
sorting by 0104 and keeping into first 0204 as second and 0304 0404 as a sorting manner and make this stings as a single.

by removing the code 0104,0204,0304,0404.

i was struck up here.

[/quote]

Can't you simply do:

[code]String s="01043000000 .... ";
s = s.replace("0104", "First");
// etc.[/code]

?[/quote]

Sorry i think you did get it.

i have to separate the entire sting by taking 0104,0204,0304,0404 as a primary key
-----------------------------
String s="[color=red]0104[/color]3000000002010000481C01000000000000000000000000000000000000000000000000000
0000000000000000000000000007F25FC0000000000004149040000000000005D2174000000
[color=red]0204[/color]0000005D49740000000000005D1D740000000000004175040000000000007F55FC000000000
000001C000000000000007DE6A8000000000000521484000000000000239A780000000000
[color=red]0304[/color]007A80D0000000000000572A54000000000000005F240000000000007F45880000000000004
13F240000000000005D54900000000000005D64900000000000005D4A7000000000000041
[color=red]0404[/color]60D00000000000007F7A78000000000000000000000000000000000000000000000000000000000000";

i like to separate the sting like this------------

First 3000000002010000481C01000000000000000000000000000000000000000000000000000000
0000000000000000000000007F25FC0000000000004149040000000000005D2174000000
Second 0000005D49740000000000005D1D740000000000004175040000000000007F55FC0000000000000
01C000000000000007DE6A8000000000000521484000000000000239A780000000000
Third 007A80D0000000000000572A54000000000000005F240000000000007F4588000000000000413F2
40000000000005D54900000000000005D64900000000000005D4A7000000000000041
Fourth 60D00000000000007F7A7800000000000000000000000000000000000000000000000000
0000000000
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
I still don't understand you.
No matter, perhaps someone else does.

Best of luck.
Prasanth Duggirala
Ranch Hand

Joined: Dec 22, 2006
Posts: 36
[quote=Piet Verdriet]I still don't understand you.
No matter, perhaps someone else does.

Best of luck.[/quote]

Hi

what i want to do is in j2me.
i am waiting for messages to read in single time 152 char we can send at time.

all the messages we are reading and keeping in one string.

the string we have identify like 0104 means 1st message with 4messages and 0204 means 2nd message with 4messages continues... 0404 4th message 4messages.

String s is the value with may changes like 0104{value}0304{value}0204{VALUE}0404{VALUE} this may not be constant.

in previous example i have create for constant value.

i am asking can we write a code to identify 0104{value},0204{value},0304{value},0404{value} values in a order.

i think iam clear now.
Prasanth Duggirala
Ranch Hand

Joined: Dec 22, 2006
Posts: 36
i think i got the process

String[] message = new String [4];

message[0]="01043000000002010000481C010000000000000000000000000000000000000000000000000000000000000000000000000000007F25FC0000000000004149040000000000005D2174000000";

message[1]="0304007A80D0000000000000572A54000000000000005F240000000000007F4588000000000000413F240000000000005D54900000000000005D64900000000000005D4A7000000000000041";

message[2]="02040000005D49740000000000005D1D740000000000004175040000000000007F55FC000000000000001C000000000000007DE6A8000000000000521484000000000000239A780000000000";

message[3]="040460D00000000000007F7A78000000000000000000000000000000000000000000000000000000000000";

String mainmessage="";
for(int i=0;i<message.length;i++){
if(message[i].toString().startsWith("0104")){
mainmessage=message[i].toString().substring(4);
System.out.println("0104 "+mainmessage);
}
if(message[i].toString().startsWith("0204")){
mainmessage=message[i].toString().substring(4);
System.out.println("0204 "+mainmessage);
}
if(message[i].toString().startsWith("0304"))
{
mainmessage=message[i].toString().substring(4);
System.out.println("0304 "+mainmessage);
}
if(message[i].toString().startsWith("0404")){
mainmessage=message[i].toString().substring(4);
System.out.println("0404 "+mainmessage);
}

}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java String parsing help