Remove text from string

Hey everyone,

I’m trying to figure out a good way (in C#) to remove one string from another…

so basically i would have something like this…

str1 = lookAtMe
str2 = AtMe

result = String.Remove(str2, str1)

Basically i want result to be look (so that AtMe comes out).

Thanks!
Bryan

lazy pants like me use Replace in such cases :slight_smile:

str1 = lookAtMe
str2 = AtMe

result = str1.Replace(str2, “”)

but there are other tricks too.

What in your case is the problem is that there is no such function (the remove requires int indexes and length, so you would have to extract its position with IndexOf first).

the string class is documented at String Class (System) | Microsoft Learn

22 Likes

Hey thanks for the advice… i took the “lazy way” and it works like a charm. The upswing is the text i’m removing is a constant so YEAH ME!

Thanks a ton… i do appreciate it.

Cheers
Bryan

8 years later and this still works!

4 Likes

9.5 years* ,its a basic c# func but does the job perfecly

14 years later… it dosent work for me:(

1 Like

10 year later… This works for me :sunglasses:

12 year later…

One Eternity Later…

10 year later. This works for me :sunglasses:

10+ years later. I’m in history too now

am I too late 10+ Years, for that small trick?

11+ years still works, hey future generations!

10 years and six months later and this is exactly what I needed.

5,594,661 minutes later and it still works

2 Likes

It has been 4047 days or 11 years 29 days or 132 months 29 days and this solution still works.

2 Likes

11 years later, my json string just won’t have it.

Working like a charm for me.

13 years later! still helpful af

14 years later and… I don’t need it