Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Better way to string parse ?  RSS feed

 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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="";
}

}

}
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Adam Nace
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool thanks guys for your input

Andrew
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!