JAVA11-5 conversion capabilities of the String class

Source: Internet
Author: User
Tags java se


The conversion function of string:
Byte[] GetBytes (): Converts the string to a byte array.
Char[] ToCharArray (): Converts a string to a character array.
static string ValueOf (char[] CHS): Turns the character array into a string.
static string valueOf (int i): turns data of type int into a string.
Note: The valueof method of the string class can turn any type of data into a string.
String toLowerCase (): Turns the string into lowercase.
String toUpperCase (): Turns the string into uppercase.
String concat (String str): Concatenation of strings.

1  Public classStringTest1 {2 3  Public Static voidMain (string[] args) {4String s = "Java";5 6 //byte[] GetBytes (): Converts the string to a byte array. If you go to an array, you need to traverse7 byte[] B =s.getbytes ();8  for(intX=0;x<s.length (); x + +){9System.out.println (B[x]);//Convert to ASCII code tableTen } OneSystem.out.println ("-------------"); A  - //char[] ToCharArray (): Converts a string to a character array. If you go to an array, you need to traverse - Char[] CA =S.tochararray (); the  for(intX=0;x<s.length (); x + +){ - System.out.println (ca[x]); - } -System.out.println ("-------------"); +  - //static string ValueOf (char[] CHS): Turns the character array into a string.  +String S1 = s.valueof (CA);//call the character array formed above ASystem.out.println ("ValueOf:" +s1); atSystem.out.println ("-------------"); -  - //static string valueOf (int i): turns data of type int into a string.  - inti = 100; -String s2 =s.valueof (i); -SYSTEM.OUT.PRINTLN ("valueOf int:" +S2);//The 100 here has become a string. inSystem.out.println ("-------------"); -  to //string toLowerCase (): Turns the string into lowercase.  +String s3 = S.tolowercase ();//to create a new array -System.out.println ("toLowerCase:" +S3); theSystem.out.println ("s:" +s);//s itself has not changed * //string toUpperCase (): Turns the string into uppercase.  $String S4 =s.touppercase ();Panax NotoginsengSystem.out.println ("toUpperCase:" +S4); -System.out.println ("-------------"); the  + //String concat (String str): Concatenation of strings.  AString SS1 = "Person"; theString SS2 = "Sleep"; +String SS3 = Ss1+ss2;//recommend this, convenient -System.out.println ("The first way of stitching:" +SS3); $String SS4 =Ss1.concat (SS2); $System.out.println ("Splicing of the second way:" +ss4); -  -  the } - Wuyi } the  - /* Wu Requirement: The first letter of a string is capitalized and the remainder is lowercase. (only English uppercase and lowercase characters are considered) - Analysis: About A: Intercept the first letter of a string//preserve String type $ B: Intercept the remaining letters//keep String type - C: Change A to uppercase - D: Change B to lowercase - E: Splicing C and D together A */ +  Public classStringTest2 { the  -  Public Static voidMain (string[] args) { $ //Define a string theString s = "Java SE"; the  the //A: Intercept the first letter of a string//Preserve String Type theString A = s.substring (0,1);//get a string type for easy stitching later - //int a = S.charat (0); This method obtains the ASCII code table, the INT type in  the //B: Intercept the remaining letters.//Preserve String Type theString B = s.substring (1); About  the //C: Change A to uppercase theString C =a.touppercase (); the  + //D: Change B to lowercase -String D =b.tolowercase (); the Bayi //E: Splicing C and D together theString E = c+D; theSystem.out.println (E);//Java se -  - //a more convenient approach, chained: the System.out.println ( theS.substring (0,1). toUpperCase (). theConcat (s.substring (1). toLowerCase ()) the);//Java se - } the  the}

JAVA11-5 conversion capabilities of the String class

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.