Word swapping in Java implementation strings

Source: Internet
Author: User
Suppose the string is "Hello world!" and now you want to change it to "! World Hello "
problem : The difficulty of this problem is how to ensure that the word is not reversed when the string is reversed
Problem Solving :

The first method of solution. First, the word is reversed, and then the string is reversed;
The second method of solution. First, the string is reversed, and then the word is reversed;

This is only the second solution:
* Core Code section: * *

public  void reverse(String s){        StringBuffer stringBuffer = new StringBuffer(s);        String a = stringBuffer.reverse().toString();//将字符串逆置        String[] b = a.split("\\s+");//分理出其中的非空格部分        stringBuffer = new StringBuffer();        for(String c:b){            StringBuffer stringBuffer1 = new StringBuffer(c);            stringBuffer.append(" "+stringBuffer1.reverse.toString());//此处双引号里面的空格不可少        }        System.out.print(stringBuffer.toString());    }

Word swapping in Java implementation strings

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.