Java string uppercase and lowercase letters are mutually modified

Source: Internet
Author: User

ImportJava.util.Scanner; Public classTest {/*** TEST Data * *@paramargs*/     Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); String s=Sc.nextline ();        System.out.println (Exchangestringa (s));    System.out.println (EXCHANGESTRINGB (s)); }    /*** Way One * *@paramS *@return     */     Public Staticstring Exchangestringa (string s) {StringBuffer result=NewStringBuffer (); String lowerstring= S.tolowercase ();//these two stepsString upstring = S.touppercase ();//is the key         for(inti = 0; I < s.length (); i++) {            if(S.charat (i) = =Lowerstring.charat (i))            {Result.append (Upstring.charat (i)); } Else{result.append (Lowerstring.charat (i)); }        }        returnresult.tostring (); }    /*** Method Two with the letter ASCII code to operate the difference between the case * a-z:97-122 * a-z:65-90 *@paramS *@return     */     Public Staticstring Exchangestringb (string s) {CharC[] =S.tochararray ();  for(inti = 0; i < c.length; i++) {            if(C[i] >= && C[i] <= 90) {C[i]= (Char) (C[i] + 32); } Else{C[i]= (Char) (C[i]-32); }        }        returnstring.valueof (c); }}

Java string uppercase and lowercase letters are mutually modified

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.