[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: string-replace question -- changing names like variable_name to v
From: |
John McCabe |
Subject: |
Re: string-replace question -- changing names like variable_name to variableName |
Date: |
Tue, 03 Sep 2002 14:24:13 GMT |
On Tue, 03 Sep 2002 09:17:58 -0400, bc <bc@example.net> wrote:
>
>Is there an easier way to do this short of some lisp like:
>
>(replace-string "_a" "A")
>(replace-string "_A" "A")
>(replace-string "_b" "B")
yes.
>From the documentation of replace-string...
"This function is usually the wrong thing to use in a Lisp program.
What you probably want is a loop like this:
(while (search-forward FROM-STRING nil t)
(replace-match TO-STRING nil t))
which will run faster and will not set the mark or print anything.
(You may need a more complex loop if FROM-STRING can match the null
string
and TO-STRING is also null.)"
Are you using a lisp program? If so, you probably want to use a
regexp-search and replace.
Try this function:
(defun get-rid-of-underscore ()
(interactive)
(while (re-search-forward "_\\([a-zA-Z]\\)?" nil t)
(replace-match (upcase (match-string 1)) nil nil)))
on this data:
hello_aorld
hello_Aorld
hello_borld
hello_Borld
hello_corld
hello_Corld
hello_dorld
hello_Dorld
hello_eorld
hello_Eorld
Does it work?