Regex expression for replacing particular symbols in text- ®and ™

Socks93

Active member
Joined
Oct 22, 2021
Messages
29
Programming Experience
Beginner
Hi,

I'm wanting to create a regular expression which will take a string, and if that string includes symbols ® or™ I want to then wrap those symbols with a <Sup> tag.

e.g. "hello® this is a test™"

outcome = hello <sup>®</sup> this is a test <sup>™</sup>

Can anyone help?

Thanks
 
If the OP didn't care about the potential overhead of using an RE, then they should also not care about the potential memory overhead, would be my argument. :)

(My post #12 uses a string builder, but the code becomes more complex and harder to understand.)
 
I was surprised at this:

1693989664944.png
 
I am too.

I suspect that the string interpolation allocates internally.

I feel like this:
C#:
sb.Append($"{prefix}{input[index]}{suffix}");
can be made into this to cut down on the allocations:
C#:
sb.Append(prefix);
sb.Append(input[index]);
sb.Append(suffix);
 
Last edited:
I'm not sure where the other string allocations would come from other than the range operator access on line 14, and the Substring() on line 18. May need to breakout the profiler.
 

Latest posts

Back
Top Bottom