Thursday, July 9, 2009

Emacs lisp functions for string manipulation

We now proceed to the next set of functions. These functions deal with string manipulation.They are specific to emacs lisp and may not work in other dialects.

Function NameSyntaxInput formOutput form
concat(concat [str1] [str2] ... [strn])Any number of string arguments
str1, str2, ... strn
A single string which is the result of
concatenating str1, str2, ... strn
substring(substring arg1 start end])arg1 is a string while
start,end are numbers
Return the substring
arg1[start]...arg1[end]
message(message arg1 [arg2] ... [argn])arg1 has to be a string The input arg1 is formatted
and printed out.

N.B.:- The input arguments in square brackets are optional. That is, they may be omitted while writing the function forms in programs. But, the other arguments are compulsory.
The function concat takes in zero or more string arguments,attaches (or appends) each string one after another in the order in which it encounters them and returns the resulting string.If it is not given any arguments, it returns the empty string "".

The substring function as the name suggests returns the substring of a particular string.The three arguments it requires are the original string and the start and end positions within the string. It returns the letters of the string from the start position upto and including the letter at the end position. If the substring function is not given the end position then, the function returns the substring from the start position till the end of the original string.

The message function requires its first argument to be a string. Unlike an ordinary string, however, it can contain one or more symbols such as %s, %d and for each of these symbols encountered in the string,it requires an appropriate argument after the first one that will replace the symbol %s or %d. In case you know C, it is analogous to the printf() statement of C.
For instance,
  • In the function (message "Hello World!"), "Hello World!"is an ordinary string.
  • But, in (message "%s argument followed by %s argument and then %d, a number" "first" "second" 3)
    • The first argument arg1 = "%s argument followed by %s argument and then %d, a number"
    • The above string contains special symbols %s %s %d and for each symbol there is a corresponding argument as in
      • arg2 = "first" corresponding to the first %s
      • arg3 = "second" corresponding to the second %s
      • arg4 = 3 corresponding to the third special symbol %d
Thus, (message "%s argument followed by %s argument and then %d, a number" "first" "second" 3)
            => "first argument followed by second argument and then 3, a number"
Each of the %s or %d symbols get replaced by the actual arguments passed.
More on string manipulation and the message function in the next post.

2 comments:

Jared said...

Hi Abdullah,

Nice summary of the string functions. And thanks for the comment on my own blog.

Some recommendations that come to mind:

Firstly, I would like to subscribe to your blog using rss but I can't see an obvious way to do so (it could be I missed something). This post mentions using feedburner to convert your atom feed to rss. You could then show an rss icon in the sidebar of your blog.

Secondly, something that I always wanted to do for my own blog was to provide a proper index page, e.g. I'd say these are the posts on muse, these are the ones on db-mode, etc. You could have something similar.

And finally, I really like to see syntax highlighted code. If you develop your pages using muse+htmlize as I mentioned
here, it does everything for you.

Jared said...

You're welcome Abdullah. The RSS subscription works great. I'd prefer the full article rather than summaries but that is probably just personal preference. Another thing you could do is auto-approve comments (if that is possible) and just delete the spam. It is a little disheartening writing a comment and then not knowing if it will be approved.

Muse is pretty easy to use. My latest post has an example of the basic setup, and all you need to do to get the syntax highlighting is surround the code with <src lang="emacs-lisp">...</src> tags.

And finally, labels are good, but a hand edited index would be better. I can't complain to much though - I haven't done the same for my blog yet either. It is a lot of work.

Post a Comment