Java Implementation substitution space ("sword refers to offer" Niu Ke net StringBuffer) _ Jian refers to offer

Source: Internet
Author: User
Tags stringbuffer

Recently in the brush "sword refers to offer" in the programming problem, but the internet about "sword refers to offer" solution is mostly C or C + +, and the official (author) is also in C + + to explain, here himself with Java wrote some of the answer code of the topic (of course, there are also some of the answers to the online others, The source does not indicate please point out, the invasion of the deletion, hope to be able to help everyone's study.


Topic Description Please implement a function that replaces a space in a string with a "%20". For example, when the string is Are Happy, the replaced string is We%20are%20happy. New Ket Network Topic Screenshot:



PS. This requires that the incoming parameter be stringbuffer type .

Answer:

[Java] View Plain Copy publicclasssolution {       public String  Replacespace (STRINGBUFFER&NBSP;STR)  {           for (int  k=0; k<str.length ()  k++)                   {                  char index = str.charat (k);                       if (index ==  '   ')                          {                          str.replace (k, k+1,  "%20");   //here with the ingenious, do not considerAfter the transformation length problem.                       }                  }                        return str.tostring ();       }  }   

Related Article

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.