• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Strings

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then the only option left is just concatenate the two substrings.
 
Villains always have antidotes. They're funny that way. Here's an antidote disguised as a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic