1

If I have:

string st1 = "this.is.a.string.type";
string st2 = "string";

Is there any built-in String method to remove st2 from st1 if the exact sequence of chars exists in st1? So I want to end up with "this.is.a..type".

0

4 Answers 4

5

The simplest would be:

st1 = st1.Replace(st2, "");

but that isn't necessarily very efficient.

Sign up to request clarification or add additional context in comments.

Comments

4
string st3 = st1.Replace(st2, string.Empty);

1 Comment

+1 for string.empty. I know there's no difference but it still makes me squirm when I see "".
0

This will work out for you

string st3 = st1.Replace(st2,"");

This will remove all instances of st2 from st1, and return the result

2 Comments

Only if the OP uses the return value. As a statement on its own (as shown), it's just eating CPU.
@Jon - I fixed it before I saw your comment, but that is of course absolutely correct (as usual).
0

What about:

st1.Replace(st2, "")

it will replace all occurrences though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.