Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java String Manipulation - Can this be made more efficient?

Reply
Thread Tools

Java String Manipulation - Can this be made more efficient?

 
 
Man Alive
Guest
Posts: n/a
 
      05-20-2008
I receive string in a method that always have at least one comma in it:

example:
1) california,sacramento
2)newyork,albany

I need to only extract the first part of the string; i.e. the string
portion before the comma: california, newyork

Currently, it is coded like this (stringName is the string being examined):

int commaLocation = stringName.indexOf(",");
int stringLengthBeforeComma = (stringName.length() - (commaLocation
+1));
System.out.Println("Part of String before comma: "
+stringName.substring(0,stringLengthBeforeComma));
 
Reply With Quote
 
 
 
 
j1mb0jay
Guest
Posts: n/a
 
      05-20-2008
On Tue, 20 May 2008 10:30:48 -0700, Man Alive wrote:

> I receive string in a method that always have at least one comma in it:
>
> example:
> 1) california,sacramento
> 2)newyork,albany
>
> I need to only extract the first part of the string; i.e. the string
> portion before the comma: california, newyork
>
> Currently, it is coded like this (stringName is the string being
> examined):
>
> int commaLocation = stringName.indexOf(","); int stringLengthBeforeComma
> = (stringName.length() - (commaLocation +1));
> System.out.Println("Part of String before comma: "
> +stringName.substring(0,stringLengthBeforeComma));


String s = "california,sacramento";
String[] parts = s.split(",");

String firstPart = s[0];
String secondPart = s[1];

Hope that answers your question !!!

Regards j1mb0jay
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      05-20-2008
On Tue, 20 May 2008 10:30:48 -0700, Man Alive <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>int commaLocation = stringName.indexOf(",");
>int stringLengthBeforeComma = (stringName.length() - (commaLocation
>+1));
>System.out.Println("Part of String before comma: "
>+stringName.substring(0,stringLengthBeforeComma)) ;


I would code that as:

final int commaPlace = s.indexOf( ',' );
if ( commaPlace < 0 )
{
throw new IllegalArgumentException( "missing comma");
}

final String state = s.substring( 0, commaPlace );
final String city = s.substring( commaPlace+1 );

other ways, especially if you had more fields.

1. use a regex spit. See http://mindprod.com/jgloss/regex.html

2. use CSVReader See http://mindprod.com/jgloss/csv.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Man Alive
Guest
Posts: n/a
 
      05-20-2008
Eric Sosman wrote:
> Man Alive wrote:
>> I receive string in a method that always have at least one comma in it:
>>
>> example:
>> 1) california,sacramento
>> 2)newyork,albany
>>
>> I need to only extract the first part of the string; i.e. the string
>> portion before the comma: california, newyork
>>
>> Currently, it is coded like this (stringName is the string being
>> examined):
>>
>> int commaLocation = stringName.indexOf(",");
>> int stringLengthBeforeComma = (stringName.length() - (commaLocation +1));
>> System.out.Println("Part of String before comma: "
>> +stringName.substring(0,stringLengthBeforeComma));

>
> Before worrying about efficiency, worry about correctness.
> By coincidence this fragment works for "california,sacramento",
> but it will fail on "newyork,albany" or "ohio,columbus" or
> "massachusetts,boston". Try them and see!
>
> ... and then, after you've fixed it, I'd guess that you
> could gain a little speed with indexOf(',') instead of
> indexOf(","), but there won't be much improvement beyond that.
>


Eric:

I could not catch the flaw you mentioned above, it works for all the
permutations listed.

Have I missed sometime?
 
Reply With Quote
 
Knuthy
Guest
Posts: n/a
 
      05-20-2008
This will process all the String which I think will be really slow for
searching only the first comma. For me Roedy Green solution is a good
one.
On 20 mai, 19:17, j1mb0jay <(E-Mail Removed)> wrote:
> On Tue, 20 May 2008 10:30:48 -0700, Man Alive wrote:
> > I receive string in a method that always have at least one comma in it:

>
> > example:
> > 1) california,sacramento
> > 2)newyork,albany

>
> > I need to only extract the first part of the string; i.e. the string
> > portion before the comma: california, newyork

>
> > Currently, it is coded like this (stringName is the string being
> > examined):

>
> > int commaLocation = stringName.indexOf(","); int stringLengthBeforeComma
> > = (stringName.length() - (commaLocation +1));
> > System.out.Println("Part of String before comma: *"
> > +stringName.substring(0,stringLengthBeforeComma));

>
> String s = "california,sacramento";
> String[] parts = s.split(",");
>
> String firstPart = s[0];
> String secondPart = s[1];
>
> Hope that answers your question !!!
>
> Regards j1mb0jay


 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      05-20-2008
Roedy Green wrote:
> On Tue, 20 May 2008 10:30:48 -0700, Man Alive <(E-Mail Removed)>
> wrote, quoted or indirectly quoted someone who said :
>
>> int commaLocation = stringName.indexOf(",");
>> int stringLengthBeforeComma = (stringName.length() - (commaLocation
>> +1));
>> System.out.Println("Part of String before comma: "
>> +stringName.substring(0,stringLengthBeforeComma));

>
> I would code that as:
>
> final int commaPlace = s.indexOf( ',' );
> if ( commaPlace < 0 )
> {
> throw new IllegalArgumentException( "missing comma");
> }
>
> final String state = s.substring( 0, commaPlace );
> final String city = s.substring( commaPlace+1 );
>
> other ways, especially if you had more fields.
>
> 1. use a regex spit. See http://mindprod.com/jgloss/regex.html
>
> 2. use CSVReader See http://mindprod.com/jgloss/csv.html

regex split is actually slower than a hand-coded indexOf based parser.
On the other hand, it is easier to get correct. If this is something
that needs to happen thousands of times per second, then don't use
regex. If it's something that happens once when a user clicks go, use
regex



--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Joshua Cranmer
Guest
Posts: n/a
 
      05-20-2008
Knuthy wrote:
> This will process all the String which I think will be really slow for
> searching only the first comma. For me Roedy Green solution is a good
> one.


Assume the average penalty per character is 100 clock cycles (probably a
gross overstatement), and then assume that you would be forced to
analyze an extra 20 characters per string. Taking the rule of thumb that
100ms is the minimum time it takes to notice something, the number of
strings required to make a noticeable delay on a paltry 1 GHz processor
is... 5,000 entries (if I did my math right). I doubt that such a
difference would be the bottleneck in your application.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rebel XT, made in Japan, made in Thailand jazu Digital Photography 10 12-12-2006 05:11 AM
java/jsp String manipulation problem questionmarc420@msn.com Java 5 01-19-2006 03:01 PM
Please Help!!more string manipulation Qs...in C++ Hp C++ 12 10-27-2005 01:40 PM
java.Contains(String search) method to be made in Java API ? Alex Zorin Java 6 08-01-2004 05:03 PM
java.Contains(String search) method to be made in Java API ? Alex Zorin Java 1 08-01-2004 11:19 AM



Advertisments