aspose file tools*
The moose likes Beginning Java and the fly likes Remove last character from String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Remove last character from String" Watch "Remove last character from String" New topic
Author

Remove last character from String

Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Go through the String class and see whether there are any methods which give accesss to sub-strings.
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
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
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

Joined: Jan 31, 2009
Posts: 189
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?

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

Joined: Jan 31, 2009
Posts: 189
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!!!
Phuc Bui
Greenhorn

Joined: Feb 19, 2009
Posts: 26


You can do this:


Cheers,
Phuc Bui

Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
Phuc Bui wrote:

You can do this:


Cheers,
Phuc Bui




It worked!!!

Thanks a million.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Remove last character from String