Click here to Skip to main content
15,916,412 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I want to remove words form a string - Here is an example

String="a boy has a magic wand named a a"
Result="a boy has magic wand named a"

in the above example, the letter 'a' was removed when it is not part of a word(i.e. followed by non-blank or preceded by non-blank or at sentence beginning or at sentence end)

is there a quick way to do this?

Thanks.

EK
Posted
Comments
Yusuf 24-Jan-11 22:07pm    
your description states ...followed by non-blank or preceded by non-blank or at sentence beginning or at sentence end

Beginning or end? Aren't they contradictory? given your description, in the example you should not have removed the last 'a'
Sergey Alexandrovich Kryukov 24-Jan-11 23:03pm    
Not contradictory. This was an explanation of criteria "not part of a word". (More realistically punctuations, etc., should be taken into account -- I would sat if I new the purpose :-)
Yusuf 25-Jan-11 7:05am    
The way I understood his description, the last 'a' should not be removed, because it was preceded by non-blank. His description is OR'ed.
Sergey Alexandrovich Kryukov 25-Jan-11 13:35pm    
As I understood 3 'a' should be removed. Not quite clear requirement, apparently. Who cares? Trivial boring stuff anyway.

Just do a search and replace for " a ". Then check if the start or end of the sentence is the word 'a' and replace that if need be. You need three regex to do it, too, so there's no quicker way than that, that I can think of.
 
Share this answer
 
Thank you all for your help.
To clarify to Yusuf, I define a word as a string with a blank on both sides.
If either sides is not blank it is not a word.
Take for example the word case of "Sin Sin in Wisconsin". The 2nd Sin is a 'word' the last string in Winsconsin is not a word.

Thanks again for your help, as Christian said 3 function calls are necessary.

EK
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900