File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Remove last character from String

 
Deeps Mistry
Ranch Hand
Posts: 189
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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?

 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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!!!
 
Phuc Bui
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You can do this:


Cheers,
Phuc Bui

 
Deeps Mistry
Ranch Hand
Posts: 189
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic