String in Java

Source: Internet
Author: User

1.CLASSPATH and path

CLASSPATHA.classpath= path is  transferred from another disk to the storage *. java disk so that the program can run B. CLASSPATH=.   Interview Question: What is the difference between PATH and CLASSPATH?         Path is a property that belongs to the operating system and defines the program path that is executed.        CLASSPATH is the load path of the defined class, regardless of the number of paths defined, a "." is defined, indicating that the class is loaded by the current directory. 

2.1 String gets

1.1: The string contains the number of characters, which is the length of the string. intLength (): Get lengths1.2: Gets the position of a character based on its location. CharCharAt (intindex)1.3: Gets the position of the character in the string, based on the character. intIndexOf (intCH): Returns the position of the first occurrence of CH in the string. intIndexOf (intChintFromIndex): Gets the position where Ch appears in the string, starting at the specified position in FromIndex. intindexOf (String str): Returns the position of the first occurrence of STR in a string. intIndexOf (String str,intFromIndex): Gets the position where Str appears in the string, starting at the specified position in FromIndex. 1.4:intLastIndexOf (String str): Reverse index.

2.2 judgment

2.1: Whether a substring is contained in a string.   Boolean contains (str); Special: IndexOf (str): The first occurrence of STR can be indexed, if returned-1That indicates that the STR does not exist in the string.       Therefore, it can also be used to determine whether the specified is included. if(Str.indexof ("a")!=1Moreover, the method can be used to judge and obtain the position that appears.
2.2: Whether there is content in the string. Boolean isEmpty (): The principle is to determine whether the length is 0. 2.3: Whether the string starts with the specified content. Boolean startsWith (str); 2.4: Whether the string ends with the specified content. Boolean endsWith (str); 2.5: Determines whether the character content is the same, and the Equals method in the object class is replicated. Boolean equals (str); 2.6: Determines whether the content is the same and ignores case. Boolean.equalsignorecase ();

2.3 Conversion

3.1: Converts a character array into a string. Constructor: String (Char[]) String (Char[],offset,count): Converts a portion of a character array into a string static method:StaticString copyvalueof (Char[]); StaticString copyvalueof (Char[] Data,intOffsetintcount); StaticString ValueOf (Char[]); 3.2: Converts a string into a character groupChar[] ToCharArray ();3.3: Converts a byte array into a string. String (byte[]) String (byte[],offset,count): Converts a portion of a byte array into a string3.4: Converts a string into a byte array. byte[] getBytes ()3.5: Converts the base data type to a string,StaticString ValueOf (int) StaticString ValueOf (Double) //" " is the same as the value of string.valueof (3)Special: Strings and byte arrays can be encoded during the conversion process. 

2.4 Replacement

String replace (Oldchar,newchar);

2.5 Cutting

String[] Split (regex);

2.6 sub-strings. Gets the part of a string

String subString (begin); String subString (begin,end);

2.7 Convert, remove spaces, compare

7.1 : Converts a string to uppercase or lowercase   String Touppercsae () Big to small    7.2: Remove   multiple spaces at both ends of a string 7.3: A natural order comparison   of two strings  int compareTo (string);

3. take a look at the following code, the following code is for the above string seven usage and one by one examples:

classstringmethoddemo{ Public Static voidMethod_zhuanhuan_qukong_bijiao () {String s="Hello Java"; //The print result is: (both Hello and Java front and back doors have spaces) Hello JavaSOP (S.touppercase ()); //The print result is: (both Hello and Java front and back doors have spaces) Hello JAVASOP (S.tolowercase ()); //Print and result: "Hello Java" with no spacesSOP (S.trim ()); //the uppercase of the comparison number, the print result is: 1, because B corresponds to the ASCII value is 98,//A correspondence is 97, so b-a=1String S1 ="ABC"; String S2="AAA"; SOP (S1.compareto (S2)); }  Public Static voidmethod_sub () {String s="abcdef"; //The print result is: Cdef, starting at the specified position and ending. If the corner label does not exist, a string corner mark will appear out of bounds. SOP (S.substring (2)); //The print result is: CD, contains the head, does not contain the tail. SOP (S.substring (2,4)); }  Public Static voidMethod_split () {String s="Zhangsan,lisi,wangwu"; String[] Arr= S.split (",");  for(intx=0; x<arr.length; X + +) {SOP (arr[x]); } }  Public Static voidMethod_replace () {String s="Hello Java"; //String S1 = s.replace (' A ', ' n '); //String S1 = s.replace (' W ', ' n '); If the character to be replaced does not exist, the original string is returnedString S1= S.replace ("Java"," World");//The print result is: Hello worldSOP ("s="+s);//The print result is: Hello Java value cannot be changed once the string is initializedSop"s1="+S1);//The print result is: Hello jnvn }  Public Static voidMethod_trans () {Char[] arr = {'a','b','C','D','e','F'}; String s=NewString (arr,1,3); SOP ("s="+s);//The print result is: BCDString S1="zxcvbnm"; Char[] CHS =S1.tochararray ();  for(intx=0; x<chs.length; X + +) {SOP ("ch="+CHS[X]);//The print result is: Ch=z,x,c,v,b,n,m  } }  Public Static voidmethod_is () {String str="Arraydemo.java"; //determine if the file name is the beginning of an array wordSOP (Str.startswith ("Array")); //determine if the file name is a. java fileSOP (Str.endswith (". Java")); //determine if the file contains a demoSOP (Str.contains ("Demo")); }   Public Static voidMethod_get () {String str="ABCDEAKPF"; //lengthSOP (Str.length ()); //get characters by indexSOP (Str.charat (4)); //SOP (Str.charat (40)), stringindexoutofboundsexception occurs when accessing a corner label that does not exist in a string (string angle out of bounds exception)//get index by character//SOP (Str.indexof (' a '));SOP (Str.indexof ('a',3));//the print is 5, because the corner Mark 3 is D,//so start looking for a on the back of D, and the 5th Corner is a.//SOP (Str.indexof (' t ', 3)) Print:-1, if no corner label is found, return-1//Reverse Index The position where a character appears (looks from right to left, but the corner mark still starts from the left)SOP (Str.lastindexof ("a")); }  Public Static voidMain (string[] args) {Method_zhuanhuan_qukong_bijiao (); //method_sub (); //Method_split (); //Method_replace (); //Method_trans (); //method_is (); //method_get ();  /*String S1 = "abc";  String s2 = new String ("abc");  String s3 = "ABC";  System.out.println (S1==S2);  System.out.println (S1==S3); */ }  Public Static voidsop (Object obj) {System. out. println (obj);}}

String in Java

Related Article

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.