Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings

 
Mikael Jonasson
Ranch Hand
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2823
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 621
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 358
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the replace the character by a blank using replace() method of String class. I hope that may help you a bit.
 
Sean MacLean
author
Ranch Hand
Posts: 621
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 358
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then the only option left is just concatenate the two substrings.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic