Dark Horse programmer-java Basic-string class

Source: Internet
Author: User

1. Overview

The string class is useful for describing string transactions. Provides a variety of methods for manipulating strings.

> The biggest feature of the string: once initialized, it cannot be changed.

2. Common methods of Operation:

2.1 Get:

1 public class StringText1 {2 public     static void Main (String srgs[]) 3     {  4//        STR is a class-type variable, "ABC" is an object. 5         String str = "Hello java!";  6//         STR and str1 differ in that there is an object in STR memory and two objects in str1 memory. 7         String str1 = new String ("Hello java!"), 8           9         SOP (Str.length ()), Ten for         (int i = 0; i < str.length ( ) ; i + +) SOP (             Str.charat (i))  , SOP (         str.indexof (' a ')),         SOP (Str.indexof (' a ', 7))         ; SOP (Str.indexof ("Java")),         SOP (Str.lastindexof (' a ')),     }18//    print function: public     static void SOP (Object obj)     {         System.out.println (obj);     }23}

1, the string contains the number of characters, that is, the length of the string; int length ();

Note: the length () of the String class is a method, and the length of a char type array call is a property.

2. Get a character on the location based on the location. char charAt (int index); Index is the subscript.

3. Gets the position of the character in the string, based on the character.

int indexOf (int ch); Returns the position of the first occurrence of CH in the string. Where CH is the ASCII code of the character.

int indexOf (int ch, int fromIndex), the position that first appears in the string, starting at the specified position in the FromIndex.

int IndexOf (String st); Returns the position of the first occurrence of the St in the string.

int IndexOf (String st, int fromIndex), where it first appears in the string, starting at the FromIndex specified position.

int lastIndexOf (int ch); Returns the index of the specified character at the last occurrence in this string.

2.2 Judgment:

1 public class String2 {2 public     static void main (String args[]) 3     {4         string str1 = "Hello java!";  5         String str2 = new String ("Hello java!");   6          7         System.out.println (Str1.equals (str2));   8         System.out.println (str1 = = str2); 9     } 10}

The results are not the same in lines 7th and 8th of the code above. The result of judging by the Equals method is true, because the String class overrides the Equals method of the object class, which is a unique way of judging the Equals method.

Common methods of judging are:

1. Determine if a string is included.

Boolean contains (Charsequence CS);

2. Whether the string is empty:

Boolean isEmpty (); The principle is to determine whether the character length is 0.

3. Determine if the string starts with the specified class tolerance:

Boolean startsWith (String str);

4. Determine if the string ends with the specified class tolerance:

Boolean EndsWith (String CS);

5. Determine if the string content is equal:

Boolean equals (Obeject obj);

6, judge whether the content is the same, and ignore the case.

Boolean equalsignorecase (String str)

2.3 Conversion

 1 public class String3 {2 public static void Main (String args[]) 3 {4 char[] ch = {' h ', ' e ', ' l ', ' L ', ' o ', ', ' j ', ' a ', ' V ', ' a '};  5//Constructor Conversion: 6//Convert character array to string: 7 string str1 = new string (CH); 8 System.out.println (STR1); 9//From ch[] subscript 6, followed by 4 characters, converted to a string.        Ten string str2 = new string (ch,6,4); System.out.println (STR2); 12 13//static method: 14// Converts a character array to a string: STR3 = string.copyvalueof (CH); System.out.println (STR3); 17//From ch[] subscript 6, followed by 4 characters, converted to a string. String STR4 = String.copyvalueof (ch,0,5); System.out.println (STR4); string str = "Hello java";              22//Convert string to character array, char[] ch1 = Str.tochararray (), 0 for (int i =; i < ch1.length; i + +) 25   System.out.print ("'" +ch1[i] + "'"); System.out.println (); }28} 

1. Convert the character array to a string:

Construction method: String (char[] ch)

String (char[] ch, int offset, int count); Converts a portion of a character array into a string.

Where: The offset parameter is the index of the first character of the Subarray, and the count parameter specifies the length of the sub-array. An exception is if the number of characters following the offset is less than count.

Note: The contents of the Subarray have been copied, and subsequent modifications to the character array do not affect the newly created string.

2. Converts a string into a character array.

Char[] ToCharArray ();

3. Turn the number of bytes into a string.

Construction method: String (byte[] by);

String (byte[] by, int offset, int count);

4. Turn the string into a byte array.

Byte[] GetByte ();

5. Convert the base data type to a string.

Static String valueOf (basic data type) ...

2.4 Replacement and cutting

String replace (char OldChar, char Newchar); Returns a new string that is newChar obtained by replacing all occurrences of this string oldChar .

String[] Split (String regex); Splits this string according to the match of the given regular expression.

public class String4 {public    static void main (String args[])    {        string str = "hello,java,string";         System.out.println (str);        Replacement character        String str1 = str.replace (', ', ');        System.out.println (STR1);//        cut        string[] str2 = Str.split (",");         for (int i = 0; i < str2.length  ; i++)             System.out.println (Str2[i]);}     }

2.5. Substring, transform, remove space and compare.

1, substring: Gets a portion of the string.

String substring (int begin); Starts from the specified position to the end. If the corner label does not exist, or a string angle is thrown.

String substring (int begin, int end); Contains the beginning, not including the tail.

2. Convert the string case:

String toUpperCase (); Turns a string into uppercase.

String toLowerCase (); Turns the string into lowercase.

3. Remove multiple spaces at both ends of the string: string trim ();

4. Compare two strings in a natural order. int CompareTo (String str); Returns a negative number when another string is larger than the string, or 0 if it is equal, otherwise returns a positive number.

1 public class String5 {2 public     static void main (String args[]) 3     {4         string str = "Hello java"; 5//        Get part of the string: 6         System.out.println (str.substring (0, 6)), 7         System.out.println (Str.touppercase ()), 8         String str1 = "             HELLO World      "; 9         System.out.println (Str1.tolowercase ());//        Remove multiple spaces at each end of the string:         System.out.println (Str1.trim ());         String str2 = "Hello World"; +//Compare        two strings for natural order comparison with         System.out.println (Str.compareto (str2));         System.out.println (' J '-' w ');     }17}

The results are the same on lines 14th and 15th.

Dark Horse programmer-java Basic-string class

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.