Java.lang.String

Source: Internet
Author: User

Package Com.etc.usual;import Java.util.arrays;public class TestString {public static void main (string[] args) {//TODO Aut O-generated method stubstring str= "qwesdzxc134532"; String str2= "123456";//1, charAt (int index) char c = str.charat (0);//system.out.println ("Str.charat (0):" +c); System.out.println ("Str.charat ():" + Str.charat (13)); for (int i=0;i<str.length (); i++) {//test length, the array is not with () length, the string is LengthSystem.out.print (Str.charat (i) + ",");//q , W,E,S,D,Z,X,C,1,3,4,5,3,2,}//2, CompareTo (String anotherstring) str= "123456"; System.out.println ("\ n" +str.compareto (str2)),//0//3, concat (String str) string Str3=str.concat ("ABCD"); System.out.println ("STR3:" +STR3);//str3:123456abcdsystem.out.println ("str:" +STR);//str:123456 str+= "Qwer"; str2= Str2.concat ("Qwer"); System.out.println ("str+:" +str);//str+:123456qwersystem.out.println ("str+:" +str2);//STR+:123456QWER//4, Str.contains (Charsequence CS)//returns True if and only if this string contains a sequence of specified char values. System.out.println ("contains:" +str.contains ("123"));//contains:truesyStem.out.println ("contains:" +str.contains ("123QW"));//contains:false//5, EndsWith (String suffix)// Tests whether this string ends with the specified suffix. System.out.println ("EndsWith:" +str.endswith ("Qwer"));//endswith:true str= "[email protected]"; System.out.println ("EndsWith:" +str.endswith ("@qq. com")),//ENDSWITH:FALSE//6, StartsWith (String prefix)// Tests whether this string starts with the specified prefix. System.out.println ("Startwith:" +str.startswith ("123"));//startwith:true System.out.println ("Startwith:" + Str.startswith ("www.")); /startwith:false//7, Equals (object AnObject)//compares this string to the specified object. Str=new string ("[email protected]"); Str2=new string ("[email protected]"); System.out.println (str+ "----" +str2);//[email protected] @sina. ComSystem.out.println ("Equals:" +str.equals ( STR2)//equals:truesystem.out.println ("= =:" + (STR==STR2));//==:false//8, indexOf (String str)// Returns the index at which the specified character first appears in this string. System.out.println ("IndexOf:" +str.indexof ("1"));//indexof:0 System.out.println ("IndexOf:" +str.indexof (' @ ')),//indexof:10//9, IsEmpty ()//when and only if LengTh () returns true for 0 o'clock. System.out.println ("IsEmpty ():" +str.isempty ());//isempty (): falsestr= ""; System.out.println ("IsEmpty ():" +str.isempty ());//isempty (): truestr= ""; System.out.println ("IsEmpty ():" +str.isempty ());//isempty (): FALSE//10, lastIndexOf (String str)// Returns the index of the rightmost occurrence of the specified substring in this string. Str= "[email protected]"; System.out.println ("IndexOf:" +str.indexof ("1")); Indexof:0system.out.println ("LastIndexOf:" +str.lastindexof ("1")),//LASTINDEXOF:19//11, Length ()//Returns the length of this string. System.out.println ("Length ():" +str.length ());//length (): 26//12, replace (char OldChar, char Newchar)//Returns a new string, It is obtained by replacing all the oldChar that appear in this string with Newchar. System.out.println ("str:" +STR);//str:[email protected]system.out.println ("Replace:" +str.replace ("1", "A")) ;//replace:[email protected]system.out.println ("str:" +STR);//str:[email protected]//13, Split (String Regex)//split This string according to the match of the given regular expression. Str= "123:45:678901111: @sina: 123.com"; string[] sp = Str.split (":"); System.out.println ("Split:" +arrays.tostring (sp));//split:[123, 678901111, @sina, 123.com] str= "123,45,678901111, @sina, 123.com"; System.out.println ("Split:" +arrays.tostring (Str.split (",")),//split:[123, 678901111, @sina, 123.COM]//14, substring (int beginindex)//Returns a new string that is a substring of this string.  SYSTEM.OUT.PRINTLN ("substring:" +str.substring (Str.length ()-5)); ToCharArray ()//convert this string to a new character array char[] Carr = Str.tochararray (); System.out.println ("ToCharArray:" +arrays.tostring (Carr));//string str11=new String ("123"); System.out.println ("Str11:" +str11+ "----" +str11.hashcode ())//str11:123----48690str11=str11+ "456"; System.out.println ("Str11:" +str11+ "----" +str11.hashcode ());//str11:123456----1450575459//stringbuffer sb=new StringBuffer ("123"); System.out.println ("SB:" +sb.tostring () + "----" +sb.hashcode ())//sb:123----366712642//sb.append ("456"); StringBuffer sb2=new StringBuffer ("123"); System.out.println ("SB2:" +sb2+ "----" +sb2.hashcode ())//sb2:123----1829164700/** * String: is a sequence of characters immutable. * * Same point: All character sequences are variable. * StringBuilder is single thread safe * StringBuffer is multithreaded security */}} 

  

Java.lang.String

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.