String 'replace'

17 views
Skip to first unread message

Grégory Vanuxem

unread,
Jan 24, 2024, 5:33:28 AM1/24/24
to fricas...@googlegroups.com
Hello,

Small question, is there a simple to use function to replace some
characters or strings in a string, for example:

replace("ababab", "b", "c")
=> "acacac"

Regards,

- Greg

Qian Yun

unread,
Jan 24, 2024, 5:44:28 AM1/24/24
to fricas...@googlegroups.com
Are you replacing one char with another char,
or are you replacing n chars with n chars?

There's Lisp function SUBSTITUTE and REPLACE, but not very easy to use.
A wrapper is needed.

- Qian

Grégory Vanuxem

unread,
Jan 24, 2024, 5:54:17 AM1/24/24
to fricas...@googlegroups.com
Le mer. 24 janv. 2024 à 11:44, Qian Yun <oldk...@gmail.com> a écrit :
>
> Are you replacing one char with another char,
> or are you replacing n chars with n chars?

I would like both :) And eventually n chars with m chars like:
replace("abcd", "cd", "efgh")
=> "abefgh"

> There's Lisp function SUBSTITUTE and REPLACE, but not very easy to use.
> A wrapper is needed.

Oh, thanks, I forgot. I use another technique not available in FriCAS
for now though, but that could be handy I think.

>
> - Qian
>
> On 1/24/24 18:32, Grégory Vanuxem wrote:
> > Hello,
> >
> > Small question, is there a simple to use function to replace some
> > characters or strings in a string, for example:
> >
> > replace("ababab", "b", "c")
> > => "acacac"
> >
> > Regards,
> >
> > - Greg
> >
>
> --
> You received this message because you are subscribed to the Google Groups "FriCAS - computer algebra system" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to fricas-devel...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/fricas-devel/de204043-b21c-4998-84a5-95e44483362f%40gmail.com.

Waldek Hebisch

unread,
Apr 10, 2024, 6:41:01 PM4/10/24
to fricas...@googlegroups.com
On Wed, Jan 24, 2024 at 11:32:50AM +0100, Grégory Vanuxem wrote:
> Hello,
>
> Small question, is there a simple to use function to replace some
> characters or strings in a string, for example:
>
> replace("ababab", "b", "c")
> => "acacac"

For replacing single characters traditional FriCAS way would be to use
'map':

(1) -> map(c +-> (c = char("b") => char("c"); c), "ababab")

(1) "acacac"
Type: String
Of course, we can define:

replace(s, c1, c2) == map(c +-> (c = char(c1) => char(c2); c), s)

to get less verbose syntax. It gets more tricky when one wants
to replace more characters or especially replace substrings

--
Waldek Hebisch
Reply all
Reply to author
Forward
0 new messages