Java String Manipulation - Can this be made more efficient?

M

Man Alive

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));
 
J

j1mb0jay

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
 
R

Roedy Green

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
 
M

Man Alive

Eric said:
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?
 
K

Knuthy

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.
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
 
D

Daniel Pitts

Roedy said:
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 :)
 
J

Joshua Cranmer

Knuthy said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top