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. :-)

4 comments:

  1. I had to study Computer programs in college for a sem. And how I hated doing that and religiously even flunked for the exam :D

    P.S. Long time.... how come you not writing much these days?

    ReplyDelete
  2. I am definitely confused now.

    Happy New Year KK.

    ReplyDelete
  3. One of my sons has been a software analyst for over 20 years. We learned "Basic" together when I bought a Commodore Vic-20 in the 1980s. But he so excelled beyond my grasp of that and other computer languages that I happily withdrew from the competition. I now ask him for advice on the slightest programming matters. Sound familiar? Excellent post!

    ReplyDelete