Friday, 13 December 2013

Replacing punctuation with nothing

Time and again we get into situations where we need to remove certain characters from a string. Like this StackOverflow question. Needless to say, rebol makes this easy and intuitive.

Why only punctuation?

For this discussion, punctuation is something like a tilde or a needless comma. We can also remove other things, like the word ji that us Indians and the word san that the Japanese like to use after people's names. Or some spelling mistake all across a string for that purpose. Read on.

replace/all

If there is only one word or character that we have to replace, we can use replace/all. We can replace either with an empty string or with some other word or character. Like:

Removing stuff

We will create a function that will take two inputs: a string! and a block!. The block will contain punctuation that we want to remove as strings. For each string in the block, we will apply replace/all on the string. See:
See forall if you are confused about lines 5 and 6. (You can have a look at forskip too.)

Usage:

If you want, you can replace with some other string. Or replace with a string, or a block of strings as arguments.

If there is anything you wish to say or ask, feel free to use the comments. Also, feel free to tell a better way of doing it. :-)