File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes String - Remove dashes from text Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String - Remove dashes from text" Watch "String - Remove dashes from text" New topic
Author

String - Remove dashes from text

Hesham Gneady
Ranch Hand

Joined: Feb 26, 2007
Posts: 66
Hello Ranchers ,

I have a long String and this is an example of it :


I want to delete all dashes like the second dash here (dashes that have text before & after them, but not space characters like the first dash). Doing this in regular expressions would be great, so i want to say :
"Find any dashes that have text before and after them(but no spaces) and if found then delete that dashes from the text".

Is there a method to do this ?

Thanks


Hesham
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Hesham Gneady wrote:I want to delete all dashes like the second dash here (dashes that have text before & after them, but not space characters like the first dash). Doing this in regular expressions would be great, so i want to say :
"Find any dashes that have text before and after them(but no spaces) and if found then delete that dashes from the text".


Sure. Regular expressions can do this with absolutely no difficulty whatsoever. What have you tried so far?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Hesham Gneady
Ranch Hand

Joined: Feb 26, 2007
Posts: 66
I know how to find the text using regular expressions, but i need a method in java to remove the dash from the found text.
For the example i wrote above, here is the regex that will grab the text "between-text" :



Now how to remove the dash ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Hesham Gneady wrote:Now how to remove the dash ?


You can use the replaceAll() method to change the matched text -- in this case, change the dash to something else, such as a space. As for not deleting whats around the dash, you can change those components of the match to either a look-ahead or look-behind.

Henry
Hesham Gneady
Ranch Hand

Joined: Feb 26, 2007
Posts: 66
Thanks a lot Henry, it's my first time to know about "lookahead" & "lookbehind" ... That was really helpful
Here is my regex :
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String - Remove dashes from text