Reverse Words in a String (JAVA)

Source: Internet
Author: User

Given an input string, reverse the string word by word.

For example,
Given s = " the sky is blue ",
Return " blue is sky the ".

1  Public classSolution {2      Publicstring Reversewords (string s) {3         if(S.equals (""))returns;4String Arr[]=s.split ("");5String new_word= "";6          for(inti=arr.length-1;i>=0;i--)7         {8         if(Arr[i].equals (""))Continue;9New_word+=arr[i]+ "";Ten         } Onenew_word=new_word.tostring (). Trim (); A             returnNew_word; -     } -}

After finishing reading the answers to others found useful StringBuilder

So I went to study the three difference in 100000 complexity stringbulder and buffer are not a bit faster than string.

So:

1. If you want to manipulate a small amount of data with = String

2. Manipulating large amounts of data under a single-threaded operation string buffer = StringBuilder
3. Multi-threaded operation string buffer operation large amount of data = StringBuffer

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.