File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes working within strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "working within strings" Watch "working within strings" New topic

working within strings

Mario Bonassin

Joined: Apr 08, 2004
Posts: 18
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.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Mario Bonassin

Joined: Apr 08, 2004
Posts: 18
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.
Scott Solomko

Joined: Apr 11, 2004
Posts: 4
The combination of indexOf and substring should be able to accomplish this. Something like the followin:

It is the soldier who salutes the flag, who serves beneath the flag, and whose coffin is draped by the flag, who allows the protester to burn the flag.
Mario Bonassin

Joined: Apr 08, 2004
Posts: 18
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?
ps if anyone has a better way of doing this let me know thanks.
I agree. Here's the link:
subject: working within strings
Similar Threads
How can I do this?
grabing letter
palindrome checker, tried to debug, keeps looping why?
Counting uppercase? Part 2!
Place A Word In A 2d Array