wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings" Watch "Strings" New topic
Author

Strings

Mikael Jonasson
Ranch Hand

Joined: May 16, 2001
Posts: 158
Am I just blind or is there no remove() function in a string?
Say I want to remove the 2nd character of the string, the only way I've found is to build a new string of the two substrings (all characters before the one I want to remove and all the ones behind).
I know it's not posible to change a string, but is there a method that returnes a new string without the character I don't want there?
/Mike
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
You might look at the StringBuffer class. delete() and deleteCharAt() will do what you want. You have to find the location first.
Sean MacLean
author
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
I considered StringBuffers as well, however, unless you're going to be doing serveral manipulations on the String you might as well just do it using String.substring. A custom utility class might be nice as well if it's something you are doing a lot.

Sean
Mikael Jonasson
Ranch Hand

Joined: May 16, 2001
Posts: 158
What I was doing was a small program printing all permutations of a set of characters. I could find a way to remove the "used" character, so I ended up using substring.
I will have to look into the StringBuffer-class..
Thanx
/Mike
rani bedi
Ranch Hand

Joined: Feb 06, 2001
Posts: 358
You can use the replace the character by a blank using replace() method of String class. I hope that may help you a bit.


Cheers,<br />Rani<br />SCJP, SCWCD, SCBCD
Sean MacLean
author
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
Actually, I tried that first myself and it simply creates a space. The problem is that you can't call replace like this (using strings - there's no string method defined for this)
String.replace( String s1, String s2 );
If you could do this then you could pass an empty string (or maybe even a null value). Unfortunately, the remove method only accepts characters and, being primatives, you can't assign a null value and '' to a character. The only way would be to do this,
s.replace( s.charAt( 1 ), ' ' );
But this leaves and empty space in the resulting string
"a cde"
Sean
rani bedi
Ranch Hand

Joined: Feb 06, 2001
Posts: 358
then the only option left is just concatenate the two substrings.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings
 
Similar Threads
how to remove duplicate characters
Remove last character from String
rtrim function in jdk 1.4
Removing linefeed from String.
textarea