String class in java (video materials from instructor Bi Xiangdong)

Source: Internet
Author: User

Package cn. itcast. p1.string. demo; public class StringDemo {/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub/** features of the String class: * The String object will not be changed once initialized. **/StringDemo2 ();} public static void stringDemo2 () {// TODO Auto-generated method stubString s = "abc"; // create a String object in the constant pool. String s1 = new String ("abc"); // create two objects. One is new and the other is in heap memory. System. out. println (s = s1); // falseSystem. out. println (s. equals (s1); // equals in the string class describes the equals in the Object to establish the basis for the string class to determine whether the string Object is the same. // Compare the string content. // System. out. println ("s =" + s); // System. out. println ("s1 =" + s1);}/*** demonstrates the first method of string definition and clarifies the characteristics of the String constant pool. * If the pool does not exist, it is created and used directly. */Private static void stringDemo1 () {String s = "abc"; // "abc" is stored in the String constant pool. // S = "nba"; String s1 = "abc"; System. out. println (s = s1); // true? // System. out. println ("s =" + s );}}

Package cn. itcast. p1.string. demo; public class StringConstructorDemo {/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub/** you can use the String constructor to convert a byte array or character array into a String. */StringConstructorDemo2 ();} private static void stringConstructorDemo2 () {char [] arr = {'w', 'A', 'P', 'Q ', 'X'}; String s = new String (arr, 1, 3); System. out. println ("s =" + s);} public static void stringConstructorDemo () {String s = new String (); // equivalent to String s = ""; not equivalent to String s = null; byte [] arr = {97,66, 67,68}; String s1 = new String (arr); System. out. println ("s1 =" + s1 );}}

Package cn. itcast. p1.string. demo; public class StringMethodDemo {/*** @ param args */public static void main (String [] args) {/** function classification of strings based on object-oriented ideas. * "Abcd" ** 1, get: * 1.1 get the number (length) of Characters in the string. * int length (); * 1.2 get characters by position. * Char charAt (int index); * 1.3 obtain the position of the first occurrence of the character in the string based on the character. * int indexOf (int ch) * int indexOf (int ch, int fromIndex): searches for the first occurrence location of ch from the specified position * int indexOf (String str ); * int indexOf (String str, int fromIndex); * obtain the first position in the String based on the String. * int lastIndexOf (int ch) * int lastIndexOf (int ch, int fromIndex): searches for the first occurrence location of ch from the specified location * int lastIndexOf (String str ); * int lastIndexOf (String str, int fromIndex); * 1.4 obtain one of the strings Some strings. It is also called a substring. * String substring (int beginIndex, int endIndex) // contains begin and does not contain end. * String substring (int beginIndex); ***** 2, conversion. * 2.1 convert a String into a String Array (String cutting) * String [] split (String regex): involves regular expressions. * 2.2 convert a String into a character array. * Char [] toCharArray (); * 2.3 converts a string into a byte array. * Byte [] getBytes (); * 2.4 converts letters in a string into uppercase and lowercase letters. * String toUpperCase (): uppercase * String toLowerCase (): lowercase * 2.5 replace the content in the String * String replace (char oldch, char newch); * String replace (String s1, string s2); * 2.6 removes spaces at both ends of the String. * String trim (); * 2.7 concatenates strings. * String concat (string); ** 3: determines if the content of the String * 3.1 is the same? * Boolean equals (Object obj); * boolean compare signorecase (string str); the upper-case comparison string content is ignored. * Does the 3.2 string contain the specified string? * Boolean contains (string str); * Indicates whether a 3.3 string starts with a specified string. Whether to end with a specified string. * Boolean startsWith (string); * boolean endsWith (string); ** 4, comparison. **/StringMethodDemo_4 (); // System. out. println ("abc ". concat ("kk"); // System. out. println ("abc" + "kk"); // System. out. println (String. valueOf (4) + 1); // System. out. println ("" + 4 + 1);} private static void stringMethodDemo_4 () {System. out. println ("abc ". compareTo ("aqz");} private static void stringMethodDemo_3 () {String s = "abc"; System. out. println (s. equals ("ABC ". toLowerCase (); System. out. println (s. equalsIgnoreC Ase ("ABC"); System. out. println (s. contains ("cc"); String str = "ArrayDemo. java "; System. out. println (str. startsWith ("Array"); System. out. println (str. endsWith (". java "); System. out. println (str. contains ("Demo");} private static void stringMethodDemo_2 () {String s = "Zhang San, Li Si, Wang Wu"; String [] arr = s. split (","); for (int I = 0; I <arr. length; I ++) {System. out. println (arr [I]);} char [] chs = s. toCharArray (); for (I Nt I = 0; I <chs. length; I ++) {System. out. println (chs [I]);} s = "AB "; byte [] bytes = s. getBytes (); for (int I = 0; I <bytes. length; I ++) {System. out. println (bytes [I]);} System. out. println ("Abc ". toUpperCase (); String s1 = "java"; String s2 = s1.replace ('Q', 'z'); System. out. println (s1 = s2); // trueSystem. out. println ("-" + "AB c ". trim () + "-");} private static void stringMethodDemo_1 () {String s = "abcdae "; System. out. println ("length:" + s. length (); // 6System. out. println ("char:" + s. charAt (2); // c // StringIndexOutOfBoundsExceptionSystem. out. println ("index:" + s. indexOf ('k'); // 0 //-1 we can determine whether the character or string exists based on-1. System. out. println ("lastIndex:" + s. lastIndexOf ('A'); // 4System. out. println ("substring:" + s. substring (2, 4 ));}}


Package cn. itcast. p1.string. demo; public class StringObjectDemo {/*** @ param args */public static void main (String [] args) {// String s1 = "abc "; // String s2 = "abc"; // intern (): String s1 = new String ("abc"); String s2 = s1.intern (); system. out. println (s1 = s2); // false }}

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.