• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

working within strings

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I've spent the last couple of days trying to figure this one out. Heres what I'm trying to do:
I have a string 'boothzan'.
I have a set of rules to apply to this string 'z only appears at the beginning or end of a word'
How do I have it go through the word and take out the letter 'z' creating the word 'boothan'?
The rules are being read in from a text file. Heres what I have
char space = '*';
for (a = 0; a < dropAll.size(); ++a) {
String drop = dropAll.get(a).toString();
for (b = 0; b< word.length(); ++b) {
if (drop.charAt(0) == word.charAt(b)) {
word.replace(word.charAt(b), space);
}
}
}
This doesn't work and I'm at a lose as to why. I also don't want to put the '*' in but I can't figure out how to remove anything. Also there are times when the character to be replace is something like 'th' or 'eks'. Using the /drop.charAt(0)/ won't work for that. As well this way doesn't allow for the letter to exist in a specific positions.
The words are all different so it needs to be flexible.
If you could at least point me in the right direction that would be great.
Thanks
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the methods on String. A few like indexOf(), substring() and length() ought to help. To remove a target character or string from a bigger string you'll have to take the chunk before the target and the chunk after it and concatenate them together. Oooh, if you're on a new enough JVM, look up replaceAll() instead of all that.
 
Mario Bonassin
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried those and they didn't do the job either. Substring() just gives back part of the word not a specific character. IndexOf() returns a int not a String so it can't be matched. At least I don't know how to do it.
As to replaceAll() I looked at that and I don't want every instance of the letter to be replaced. I want to pick and choose which will be replaced. Thats why I tried the replace(), but it uses char instead of strings.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The combination of indexOf and substring should be able to accomplish this. Something like the followin:
 
Mario Bonassin
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks that got me on the right track. But why does it go through the first loop twice?
Here the code:

if the word /dhazaaz/ is passed, and the two letter to remove are /d/ and /z/ the println give the following
[z, d] 2 0
dhazaaz dhazaaz 0 false false
dhaaaz dhaaaz 1 false false
dhaaa dhaaa 2 false true
[z, d] 2 1
haaaz haaaz 0 true false
[z, d] 2 0
dhaaa dhaaa 0 false true
[z, d] 2 1
haaaz haaaz 0 true false
dhaaaz final fix

It gives the correct word but it goes through the process twice, why?
Thanks
Mario
ps if anyone has a better way of doing this let me know thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic