• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Remove last character from String

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Can you suggest me ways to remove last character from String in java.

For example:
String str = "abcdefg";

I want to remove g and display "abcdef"

How do i do this in java.

Thanks
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Go through the String class and see whether there are any methods which give accesss to sub-strings.
 
Deeps Mistry
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Go through the String class and see whether there are any methods which give accesss to sub-strings.



Hi,
Thanks for your prompt reply.
Actually what i want to do is:

I am retrieving a column from database and setting it to a String. But after each column retrieval i want to add a "," to separate them.

Say for example i have a column named Items which contains say 'Oven' as 1 st entry 'Tray as second entry and so on.
So i want to store it like Oven,Tray in the String.

while(rs3.next())
{

Items += rs3.getString(2);
}

where do i add the "," character?

I hope you got my Query.

Thanks.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is completely different from what you asked earlier.

Don't use String concatenation. Use a StringBuilder and its append method. Much better performance. The only problem is that you need to add a comma after all but the last entries. You can probably do that with an

if(rs.hasNext()){}

block inside the loop.
 
Deeps Mistry
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That is completely different from what you asked earlier.

Don't use String concatenation. Use a StringBuilder and its append method. Much better performance. The only problem is that you need to add a comma after all but the last entries. You can probably do that with an

if(rs.hasNext()){}

block inside the loop.



Hi,

I changed my code as follows:

StringBuilder strItems = new StringBuilder();

while(rs3.next())
{

//Items += rs3.getString(2);
strItems.append(rs3.getString(2));
if(rs3.hasNext())
{
strItems.append(",");
}

}

But i am getting compilation error saying method hasNext is undifened for type ResultSet.

Am i doing anything wrong?

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But i am getting compilation error saying method hasNext is undifened for type ResultSet.



Well, it's a bit more complex than that....

ResultSet doesn't have a hasNext() method. The closest equivalent is the next() method, which returns whether there is a next row. You are already using this method.... However, this method also advances the cursor, so checking twice before reading will actually cause you to get every other result. You will have to play with you loop a bit, to only call next() once, and to get the commas right.

Henry
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
D*mn.

You are right; ResultSet hasn't got a hasNext() method.
Several possibilities:
1: Try appending the comma after every item in the ResultSet, then remove the last comma [that will produce an Exception from an empty Result Set.]
2: Or think how you can get a hasNext variable for the loop3: Probably the best: use the isLast() method and a ! operator to see whether to append the comma.
 
Deeps Mistry
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

But i am getting compilation error saying method hasNext is undifened for type ResultSet.



Well, it's a bit more complex than that....

ResultSet doesn't have a hasNext() method. The closest equivalent is the next() method, which returns whether there is a next row. You are already using this method.... However, this method also advances the cursor, so checking twice before reading will actually cause you to get every other result. You will have to play with you loop a bit, to only call next() once, and to get the commas right.

Henry



Ok...so what can be done is:

i add "," after every value.

So my output would be Oven,Tray,

Now maybe what i can do is just remove the last character from this String.

Can you suggest me ways to do the same?

Thanks a lot for your help!!!
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


You can do this:


Cheers,
Phuc Bui

 
Deeps Mistry
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Phuc Bui wrote:

You can do this:


Cheers,
Phuc Bui




It worked!!!

Thanks a million.
 
If a regular clown is funny, then a larger clown would be funnier. Math. Verified by this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic