* Replacing substrings in strings
@ 2009-04-14 17:18 Sebastian Tennant
2009-04-14 17:35 ` Linas Vepstas
2009-04-15 8:52 ` Ludovic Courtès
0 siblings, 2 replies; 7+ messages in thread
From: Sebastian Tennant @ 2009-04-14 17:18 UTC (permalink / raw)
To: guile-user
Hi Guilers,
I needed to replace two consecutive spaces in strings with ' ' so
I came up with this:
(define (replace-substring s t u)
"Return string S with all occurrences of string T replaced by string U."
(let ((rv ""))
(let loop ((r s)) ; r for 'remaining'
(let ((a 0) (b 0))
(set! a (string-contains r t)) ;match-start index
(if a
(begin
(set! b (+ a (string-length t))) ;match-end index
;; concatenate that which precedes match-start and u to rv
(set! rv (string-append rv (substring r 0 a) u))
(loop (substring r b))) ;repeat on that which follows match-end
(set! rv (string-append rv r)))))
rv))
(replace-string "Hello. How are you?" " " " ")
=> "Hello. How are you?"
It works, but seems like a lot of work to achieve something pretty
banal.
At first I thought Richard Todd's (string transform) module in guile-lib
would be able to help, but it works on single characters rather than
substrings.
Are there any there other string libraries I don't know about that could
be used to achieve this? Or is there a better algorithm for that
matter? Silly question - of course there is!
Regards,
Seb
--
Emacs' AlsaPlayer - Music Without Jolts
Lightweight, full-featured and mindful of your idyllic happiness.
http://home.gna.org/eap
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Replacing substrings in strings
2009-04-14 17:18 Replacing substrings in strings Sebastian Tennant
@ 2009-04-14 17:35 ` Linas Vepstas
2009-04-15 7:04 ` Sebastian Tennant
2009-04-15 8:52 ` Ludovic Courtès
1 sibling, 1 reply; 7+ messages in thread
From: Linas Vepstas @ 2009-04-14 17:35 UTC (permalink / raw)
To: Sebastian Tennant; +Cc: guile-user
2009/4/14 Sebastian Tennant <sebyte@smolny.plus.com>:
> Hi Guilers,
>
> I needed to replace two consecutive spaces in strings with ' ' so
Most programming languages use regex, and so does guile:
http://www.gnu.org/software/guile/manual/html_node/Regular-Expressions.html
--linas
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Replacing substrings in strings
2009-04-14 17:35 ` Linas Vepstas
@ 2009-04-15 7:04 ` Sebastian Tennant
2009-05-02 0:29 ` Neil Jerram
0 siblings, 1 reply; 7+ messages in thread
From: Sebastian Tennant @ 2009-04-15 7:04 UTC (permalink / raw)
To: guile-user
Quoth Linas Vepstas <linasvepstas@gmail.com>:
> 2009/4/14 Sebastian Tennant <sebyte@smolny.plus.com>:
>> I needed to replace two consecutive spaces in strings with ' ' so
>
> Most programming languages use regex, and so does guile:
Of course it can be done using regexps, I'm just a little surprised
there isn't already a replace-substring function knocking around.
Seb
--
Emacs' AlsaPlayer - Music Without Jolts
Lightweight, full-featured and mindful of your idyllic happiness.
http://home.gna.org/eap
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Replacing substrings in strings
2009-04-14 17:18 Replacing substrings in strings Sebastian Tennant
2009-04-14 17:35 ` Linas Vepstas
@ 2009-04-15 8:52 ` Ludovic Courtès
2009-04-15 9:09 ` Sebastian Tennant
1 sibling, 1 reply; 7+ messages in thread
From: Ludovic Courtès @ 2009-04-15 8:52 UTC (permalink / raw)
To: guile-user
Hello,
I would probably use, e.g., `regexp-substitute/global' as Linas
suggested. Otherwise, you could try using SRFI-13 as well if it's
enough (consecutive spaces are coalesced here):
(string-join (string-tokenize "hello, world.") " ")
=> "hello, world."
Thanks,
Ludo'.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Replacing substrings in strings
2009-04-15 8:52 ` Ludovic Courtès
@ 2009-04-15 9:09 ` Sebastian Tennant
2009-04-15 14:32 ` Ludovic Courtès
0 siblings, 1 reply; 7+ messages in thread
From: Sebastian Tennant @ 2009-04-15 9:09 UTC (permalink / raw)
To: guile-user
Quoth ludo@gnu.org (Ludovic Courtès):
> Otherwise, you could try using SRFI-13 as well if it's enough
> (consecutive spaces are coalesced here):
>
> (string-join (string-tokenize "hello, world.") " ")
> => "hello, world."
I need to only replace two consecutive spaces. Also string-tokenize (in
guile-1.8) doesn't like unicode strings... I have my answer - regexps,
although sledgehammers and nuts come to mind.
I remember reading that work is currently underway to implement wide
string handling in Guile. Any idea when it might make it into a stable
release?
Seb
--
Emacs' AlsaPlayer - Music Without Jolts
Lightweight, full-featured and mindful of your idyllic happiness.
http://home.gna.org/eap
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Replacing substrings in strings
2009-04-15 9:09 ` Sebastian Tennant
@ 2009-04-15 14:32 ` Ludovic Courtès
0 siblings, 0 replies; 7+ messages in thread
From: Ludovic Courtès @ 2009-04-15 14:32 UTC (permalink / raw)
To: guile-user
Sebastian Tennant <sebyte@smolny.plus.com> writes:
> I remember reading that work is currently underway to implement wide
> string handling in Guile. Any idea when it might make it into a stable
> release?
Mike Gran is working on it. It won't be released before the next stable
series (2.x), which isn't precisely scheduled. :-)
Thanks,
Ludo'.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Replacing substrings in strings
2009-04-15 7:04 ` Sebastian Tennant
@ 2009-05-02 0:29 ` Neil Jerram
0 siblings, 0 replies; 7+ messages in thread
From: Neil Jerram @ 2009-05-02 0:29 UTC (permalink / raw)
To: Sebastian Tennant; +Cc: guile-user
Sebastian Tennant <sebyte@smolny.plus.com> writes:
> Quoth Linas Vepstas <linasvepstas@gmail.com>:
>> 2009/4/14 Sebastian Tennant <sebyte@smolny.plus.com>:
>>> I needed to replace two consecutive spaces in strings with ' ' so
>>
>> Most programming languages use regex, and so does guile:
>
> Of course it can be done using regexps, I'm just a little surprised
> there isn't already a replace-substring function knocking around.
There is, and I guess it's what Linas had in mind:
regexp-substitute/global (from (ice-9 regex)).
For example:
;; Replace forward slashes by backward ones.
(set! path (regexp-substitute/global #f "/" path 'pre "\\" 'post))
Regards,
Neil
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2009-05-02 0:29 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-04-14 17:18 Replacing substrings in strings Sebastian Tennant
2009-04-14 17:35 ` Linas Vepstas
2009-04-15 7:04 ` Sebastian Tennant
2009-05-02 0:29 ` Neil Jerram
2009-04-15 8:52 ` Ludovic Courtès
2009-04-15 9:09 ` Sebastian Tennant
2009-04-15 14:32 ` Ludovic Courtès
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).