The string nature and common methods of Java Foundation Summary

Source: Internet
Author: User

Package Cn.wen.basic.test;import Java.io.unsupportedencodingexception;import Java.util.arrays;import  org.junit.test;/* * Face question string and stringbuffer difference * System Review String * String is a constant once the input cannot be modified, the re-assignment is two objects * The source can see that the string is decorated with a final modifier For constants * String inheritance serializable (serializable) interface and comparable (comparison class) and Charsequence (serialized String) class * The source defines the class as the value used for the character store * API says string created value cannot be changed. The string buffers can be changed (StringBuffer). String support for Shared */public class TestString {/* * String method to learn how to remember methods name * string sharing * Char array of type string can be converted to strings of string type * String source code    This constructor is used to implement a char array to convert to String * public string (char value[]) {this.value = arrays.copyof (value, value.length); } */@Testpublic void Teststringcharshare () {String str= "abc"; System.out.println (str);//equivalent to char[] data={' A ', ' B ', ' C '}; String Str1=new string (data);     System.out.println (STR1);} @Test public void Teststringconstructor () {string str0=new string ();//Initializes a newly created string object that represents the empty character sequence byte[] Bytes=n     EW byte[1024];     String str1=new string (bytes);//Constructs a new string by decoding the specified byte array using the platform default character set; Str1= "ADFS";     System.out.println (STR1); try {string Str2=new string (bytes, "UTF-8");//sets the character set to decode the specified byte array to construct a new string//similar method string Str3=new string (bytes,1, BYTES.LENGTH-1);//Constructs a new string by decoding the specified byte array using the platform default character set. String Str4=new string (bytes,0,bytes.length-1, "UTF-8"),//bytes byte array 0 specifies the starting position of the intercepted byte, and length specifies the last position of the intercept byte. UTF-8 Specifies the decoding set char[] data1={' A ', ' B ', ' C ', ' d ', ' e ', ' f '}; String Str5=new string (data1,1,4); System.out.println (STR5);} catch (Unsupportedencodingexception e) {//If the type of encoding is not supported or the encoding format input error throws an exception//TODO auto-generated catch               Blocke.printstacktrace ();}     } @Test public void Teststringmethod () {String str1= "abcdefghijklmnopqrstuvwxyz";     Char D=str1.charat (3);//Gets the substring in string string return char type System.out.println (d);     int A=str1.codepointat (0);//Gets the ACCI code value of the index character System.out.println (a);     int C=str1.codepointbefore (4);//Gets the ACCI code value of the first character of the first index character System.out.println (c);     int Acci=str1.codepointcount (1, 5);//Gets the specified range of Unicode code points System.out.println (ACCI);     String str2= "C"; IntComp=str1.compareto (STR2);//Compare the size of two strings by dictionary order.     SYSTEM.OUT.PRINTLN (comp);     System.out.println ("-------------------------"); String[] Str3=str1.split ("D");//split string returns D before and D after the string does not contain d for (int i=0;i<str3.length;i++) {System.out     . println (Str3[i]);     } System.out.println ("--------------------------");     String strson=str1.substring (1, 5);//intercept string 1 for start position, 5 for End position System.out.println (Strson);     String strson1=str1.substring (3);//intercept string, starting from 3 System.out.println (StrSon1);     Charsequence cs=str1.subsequence (1, 4);//returns a character sequence System.out.println (CS);     String strupper=str1.touppercase ();//lowercase to uppercase//tolowercase uppercase to lowercase System.out.println (strupper); }
}

The string class is a very common class in Java, and string provides a number of great ways to help programmers with string manipulation, which is useful in JEE because the data sent by the foreground is often in the form of strings or JSON arrays.

The string nature and common methods of Java Foundation Summary

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.