String of Java

Source: Internet
Author: User

Reference http://how2j.cn/k/number-string/number-string-string/324.html

Create a string

String is a combination of characters, in Java, the string is a class, so we see the string is the object
Common ways to create strings:
1. Whenever a literal appears, the virtual machine creates a string
2. Call the construct method of string to create a string object
3. String stitching with + Plus will also create a new string object

Package character; Public classteststring { Public Static voidMain (string[] args) {String Garen="Galen";//Literal , the virtual machine creates a string object when it touches a literalString Teemo=NewString ("Tim Mo");//two string objects created                 Char[] cs =New Char[]{'Cui',' ,','Special'}; String Hero=NewString (CS);//creating a String object from a character arrayString Hero3= Garen + Teemo;//string stitching with + plus sign    }}
Final

String is modified to final, so it cannot be inherited.

Immutable

Immutable refers to an immutable
For example, a string object was created
String Garen = "Galen";
The exact meaning of an immutable term is:
Cannot increase length
Cannot reduce the length
Cannot insert character
Cannot delete character
Cannot modify character
Once the string is created, the contents of it can never be changed.

A String behaves as if it were a constant .

String formatting String.Format ();
Package character;  Public classteststring { Public Static voidMain (string[] args) {String name="Galen"; intKill =8; String title="Super God"; //Direct use + string connection, coding feeling is more cumbersome, and poor maintenance, easy to read poorString sentence = name+"in a continuous"+ Kill +"after the killing, the"+ title +"the title"; System. out. println (sentence); //Formatting Strings//%s represents a string,%d represents a number, and%n represents a newlineString Sentenceformat ="%s was awarded the title of%s after a series of%d kills%n"; String Sentence2=String.Format (Sentenceformat, name,kill,title); System. out. println (SENTENCE2); }}

The range of digits corresponding to the character A-Z-a-0-9 can be known by means of the following

Char ' A ' ;  Short s = (short) C;
Practice-Random string

Create a random string with a length of 5, random characters may be numbers, uppercase letters or lowercase letters

 Public StaticString getstr () {Char[] carr=New Char[5];  for(intI=0;i<5; i++){            CharC= (Char) (Math.random () * (122- -)+ -+1); if(Character.isletterordigit (c)) {Carr[i]=C; }Else{i--; }        }        returnstring.valueof (CARR); }
Practice-Sorting string arrays

Create a string array with a length of 8
Initialize this array with 8 random strings with a length of 5
Sort the array by the first letter of each string (ignoring case)

Note 1: You cannot use Arrays.sort () to write yourself
Note 2: Ignoring the case, that is, axxxx and axxxxx do not have a sequencing

 Public Static voidsort () {string[] Strarr=Newstring[8]; System. out. println ("before sorting");  for(intI=0;i<8; i++) {Strarr[i]=Getstr (); System. out. println (Strarr[i]); }         for(intI=0;i<8-1; i++){             for(intj=i+1;j<8; j + +){                CharC1=strarr[i].charat (0); CharC2=strarr[j].charat (0); if(Character.isuppercase (C1)) {C1=Character.tolowercase (C1); }                if(Character.isuppercase (C2)) {C2=character.tolowercase (C2); }                if(c1>C2) {String str=Strarr[i]; Strarr[i]=Strarr[j]; STRARR[J]=str; }}} System. out. println ("after sorting");  for(intI=0;i<8; i++) {System. out. println (Strarr[i]); }    }

String of Java

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.