This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Better way to string parse ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Better way to string parse ?" Watch "Better way to string parse ?" New topic
Author

Better way to string parse ?

Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Hi all ,

I have a 16 char string I need to parse into 4 substrings. The first substring needs to be the first 3chars, next needs to be the next 3 chars, and the last two are 5 chars each. Right now I am just using substring to break up the four. Like this:

string1 = bigString.substring(0,3);
string2 = bigString.substring(3,6);
...
...

Is there a better way to do this giving that the parse pattern is always going to be the same and the bigString length is never going to change?

Thanks,

AMD
James Brown
Greenhorn

Joined: Aug 01, 2006
Posts: 4
sorry i can't hava a better way. but i think you can chang a
string to a char[]
like these codes:

public void getStringParse(String str){
char[] c=str.toCharArray();
String newStr="";
for(int i=0;i<c.length;i++){
newStr+=c[i];
if(i%3==0){
System.out.println(newStr);
newStr="";
}

}

}


good luck everyone
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
Andrew,

I believe that your solution is better. I can glance at your code and see exactly what you are trying to do. Yes, you have 4 lines of code that look the same, but so what.

"hi me"'s code is very obfuscated and will be difficult to maintain.

The programmers that have to review or maintain the code will thank you.

One enhancement I would make would be to replace your "magic numbers" with constants with meaningful names:

James Brown
Greenhorn

Joined: Aug 01, 2006
Posts: 4
hi Scott Johnson
i think you are right. do you have better way to slove the question.and use "String.substring()" is the best way ? thank you
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12928
    
    3

James, please read the JavaRanch official policy on registered names and change your display name: <first name>, <space>, <last name> please. Sorry, but just "James" is not good enough.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
do you have better way to slove the question


I don't have a better solution than Andrew's. It's simple, fast and easy for a reviewer to understand.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The substringing solution is clear enough. If the repetition bothers you, you could hide it in a special parsing class. I worked in another language (EASEL) with "skip" and "take" syntax something like...

EASEL could also take up to a delimiter or pattern, and skip a number or a pattern. In Java this might fit best in a Stream concept. Does that look better or worse to you?
[ August 03, 2006: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
In my personal opinion, the only way to improve upon the substring version is to create a well named method out of it.

This provides 2 advantages:

1. It will make the code in the calling location clearer, because the name of the method will basically describe exactly what it does.

2. It will be re-usable.


For example:



Then, at the point where you use the method, you simply do this:



This would be the MOST clear way for the maintenance programmer to understand what that code was doing.

You could also write a variation that would use the length of each sub-string instead of the start positions, and compute the breakLocation values yourself.

- Adam
[ August 03, 2006: Message edited by: Adam Nace ]
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Cool thanks guys for your input

Andrew
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Better way to string parse ?
 
Similar Threads
How does substring method works?
search long string for several strings
How to find last 5 chars in string
Terminated Insert.
recursion problem