[LadyGaGa] an incorrect pen question

Source: Internet
Author: User

 

There is a question: to completely delete str1 from str2, use C # To implement

I asked if code is used for implementation. The feedback is that my answer is simple:

 

If (String. IsEmptyOrNull (str1) return;

If (String. IsEmptyOrNull (str2) return;

Str2 = str2.Replace (str1, String. Empty );

The other party asked me if I could give all the strings to replace in this method. I was a small hacker. Because I'm not sure if replace is all or if the first match is met, replace will be returned.

I will answer the question. recursion can be used.

So let me use recursion to do the following:

Public void Replace (ref string str1, ref string str2)
{

If (String. IsEmptyOrNull (str1) return;

If (String. IsEmptyOrNull (str2) return;

If (str2.IndexOf (str1) =-1) return;
Else str2 = str2.Replace (str1, String. Empty );

Replace (ref str1, ref str2 );
}

You can see that this method is feasible.

 

However, the result is that I tried my first method, which is also possible.

 

Now I understand that they want to implement the String Replace principle, but the result is wrong. In fact, it is simple. The built-in method or string replace code is not allowed in the question requirements.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.