Java String Manipulation - Can this be made more efficient?

Discussion in 'Java' started by Man Alive, May 20, 2008.

  1. Man Alive

    Man Alive Guest

    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));
     
    Man Alive, May 20, 2008
    #1
    1. Advertising

  2. Man Alive

    j1mb0jay Guest

    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
     
    j1mb0jay, May 20, 2008
    #2
    1. Advertising

  3. Man Alive

    Roedy Green Guest

    On Tue, 20 May 2008 10:30:48 -0700, Man Alive <>
    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
     
    Roedy Green, May 20, 2008
    #3
  4. Man Alive

    Man Alive Guest

    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?
     
    Man Alive, May 20, 2008
    #4
  5. Man Alive

    Knuthy Guest

    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 <> 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
     
    Knuthy, May 20, 2008
    #5
  6. Man Alive

    Daniel Pitts Guest

    Roedy Green wrote:
    > On Tue, 20 May 2008 10:30:48 -0700, Man Alive <>
    > 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/>
     
    Daniel Pitts, May 20, 2008
    #6
  7. 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
     
    Joshua Cranmer, May 20, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Zorin
    Replies:
    6
    Views:
    88,219
  2. Alex Zorin
    Replies:
    1
    Views:
    478
    Michal Dzirba
    Aug 1, 2004
  3. javafreak

    Efficient String Manipulation ?

    javafreak, Nov 12, 2005, in forum: Java
    Replies:
    13
    Views:
    868
    javafreak
    Nov 14, 2005
  4. Hp
    Replies:
    12
    Views:
    640
    Karl Heinz Buchegger
    Oct 27, 2005
  5. Li Wang
    Replies:
    0
    Views:
    223
    Li Wang
    Apr 28, 2009
Loading...

Share This Page