-
August 30th, 2010, 12:01 PM
#1
extraction from a string
Hello, I had a string like this:
....... x.y.z.home.room.bed.sheet.type.price
I need to put in an array the last three( sheet, type, price). How can I do, the best chance?
thanks
-
August 30th, 2010, 12:17 PM
#2
Re: extraction from a string
You could use the String's split method to split the string at each '.' and extract the last 3 array elements.
-
August 30th, 2010, 01:47 PM
#3
Re: extraction from a string
Originally Posted by keang
You could use the String's split method to split the string at each '.' and extract the last 3 array elements.
potentially I could have something of 1000 string to split; do you think split is even fast or is there something better? thanks
-
August 30th, 2010, 02:16 PM
#4
Re: extraction from a string
Why not try it and see if it's fast enough before worrying about it.
-
August 30th, 2010, 03:44 PM
#5
Re: extraction from a string
Originally Posted by keang
Why not try it and see if it's fast enough before worrying about it.
Maybe could be not good even I don't notice that its speed is slow..?
-
August 30th, 2010, 04:21 PM
#6
Re: extraction from a string
Maybe could be not good even I don't notice that its speed is slow..?
Sorry, I can't understand that sentence. Are you saying that although it doesn't appear to be slow to you it may not be efficient enough?
-
August 30th, 2010, 05:41 PM
#7
Re: extraction from a string
OK see I assume split is the only chance; now: what's the best?
Code:
StringBuilder sb = new StringBuilder();
for (int ii=0; ii < 1000; ii++ ){
sb.append("home" + ".");
}
sb.replace(sb.length()-1, sb.length()-1, "");
String[] sp = sb.toString().split("[.]");
//way 1
for (int i=0; i < sp.length && i<=2; i++)
System.out.println ( sp[sp.length-1-i] );
//way2
List<String> list = new ArrayList<String>( Arrays.asList(sb.toString().split("[.]") ));
for (int q=0; q < 3; q++) {
System.out.println( list.remove( list.size() -1 ) );
}
way1 or way2? I would prefer a stack but I'm not able to convert the split string in a stack<String>
thanks
-
August 31st, 2010, 08:20 AM
#8
Re: extraction from a string
OK see I assume split is the only chance;
No, I didn't say that. There are many ways of doing this but using the split() method is the easiest to code and understand.
The point is you shouldn't be worrying about performance efficiencies until you know there is a problem. And by 'know' I mean having analyzed the code using a profiling tool and not just because you think it might be a problem.
That depends on what you mean by 'Best'. Your code returns the items in reverse order - is that really you want?
Assuming you mean coding style (and you want the items in the correct order) I would do:
Code:
StringBuilder sb = new StringBuilder();
for ( int ii = 0; ii < 1000; ii++ ) {
sb.append("home" + ii + ".");
}
sb.setLength(sb.length() - 1);
String[] sp = sb.toString().split("\\.");
for ( int i = sp.length - 3; i < sp.length; i++ ) {
System.out.println(sp[i]);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|