String Common methods-api__string

Source: Internet
Author: User
Tags array to string lowercase
the string class is useful for describing string things. It provides a number of ways to manipulate strings.

1. Gets the number of characters contained in the 1.1 string, which is the length of the string.
int length (): Gets length 1.2 gets a character from the position.
char charAt (int index) 1.3 Gets the character from the character in the change string position int indexOf (int ch): Returns the position in which CH first appears in the string. int indexOf (int ch, int fromindex): Starts at the fromindex specified position, gets the position where the CH appears in the string. int indexOf (String str): Returns the position in which Str appears for the first time in a string. int indexOf (string str, int fromindex): Starts at the fromindex specified position, obtaining the position of STR in the string. int lastindexof (int ch): Starting at the end of a string.

2, to determine whether the 2.1 string contains a substring Boolean contains (str): Special: IndexOf (str): You can index the first occurrence of STR, if return-1. Indicates that the STR is not present in the string. Therefore, it can also be used to determine whether the specified is contained. if (Str.indexof ("AA")!=-1) and the method can be judged, it can also obtain the position that appears. 2.2 Characters whether there is content Boolean IsEmpty (): The principle is to determine whether the length is 0. 2.3 Whether the string begins with the specified content. Boolean startswith (str). 2.4 Whether the string ends in the specified content Boolean endsWith (str). 2.5 Determines whether the string content is the same, and the Equals method Boolean Equalsignorecase () in the object class is overwritten. 3, Conversion 3.1 converts a character array to a string constructor: String (char[]) string (char[],offset,count): Converts a portion of a character array into a string. static method: Static String copyvalueof (char[]); Static String copyvalueof (char[] data,int offset,int count); Static String valueof (char[]); 3.2 Converts a string into an array of characters. Char[] ToCharArray (); 3.3 Converts a byte array to a string (byte[]) string (byte[],offset,count), converts a portion of a byte array to string 3.4, converts a string to a byte array byte[] GetBytes (); 3.5 Converts the base data type to a string valueof (int), the int data type + "", or an empty string, to a string static string valueof (double); 3+ "";//string.value (3); Special: Strings and byte arrays in the conversion process in China, you can specify the encoding table. 4. Replace

String replace (Oldchar,newchar);

5, cutting string[] Split (regex), cutting one character in a string into a string array 6, substring, getting part of the string string SUBSTRING (begin); String substring (begin,end); 7, convert, remove space, compare 7.1 to turn the string to uppercase or lowercase string touppercase (); String toLowerCase (); 7.2 Remove string trim () with multiple spaces at both ends 7.3 Compare the natural order of two strings int compareTo (string); code example:

public class StringDemo01 {public static void sop (Object obj) {System.out.println (obj);

        The public static void Method_1 () {string S1 = "abc";//S1 is a class type variable, and "ABC" is the maximum feature of an object//string: Once initialized, it cannot be changed.
        String s2 = new String ("abc"); What's the difference between S1 and S2?

        S1 is an object in memory and S2 has two objects in memory.
        System.out.println (S1==S2); SOP (S1.equals (S2));//string class The Equals method in the object class is used to determine whether the string is the same} public static void Method_2 (string s) {C

        Har ch = s.charat (3);//returns the char value at the specified index SOP ("ch=" +ch);

        int num = S.codepointat (3);//returns the character at the specified index (Unicode code point) SOP ("num=" +num);
        String S1 = "QQ";

        S1 = S1.concat ("MM");//connect the specified string to the end of this string.
        SOP ("s1=" +s1);
        SOP ("QQ" + "MM");
        String a = "OPQ";

        String B = "OPQ";
    System.out.println ("A==b:" + (a==b));
        The public static void Method_7 () {String s = "Hello Java"; SOP (S.tolowercase ());//lowercase SOP (s.touppercase ());/Capital SOP (S.trim ());//Go to the end of the space String S1 = "ALC";
        String s2 = "AAA"; SOP (S1.compareto (S2));//Compares two strings, returns a positive number is greater than the latter, 0 is equal, negative numbers instead} public static void Method_substring () {string s

        = "ABCDEFGHIJKLMN";
        SOP (S.substring (2));//from the specified position to the end, if the corner mark does not exist, there will be a string corner across the bounds of the exception.
    SOP (S.substring (2, 4));//containing head, not including tail, s.substring (0,s.length ());
        The public static void Method_split () {String s = "Zhangsan,list,wangwu,chenqi";
        string[] Array = S.split (",");
        for (String Str:array) {sop (str);
        The public static void Method_replace () {String s = "Hello java";
        string S1 = s.replace (' q ', ' n ');//If the character you want to replace does not exist, return the original string.
        String s2 = s.replace ("java", "World");
        SOP ("s=" +s1);
        SOP ("s1=" +s1);
    SOP ("s2=" +s2);
        The public static void Method_trans () {char[] arr = {' A ', ' B ', ' C ', ' d ', ' e ', ' f '};
        string s = new string (arr,1,3);
        SOP ("s=" +s); StRing S1 = "ZXCVBNM";
        char[] CHS = S1.tochararray ();
        for (char c:chs) {SOP ("ch=" +c);
        The public static void Method_is () {String str = ' Arraydemo.java ';
        Determine if the file name is an Array word opening sop (Str.startswith ("array"));
        Determines whether the file name is a. Java-terminated file sop (Str.endswith (". Java"));
    Determine whether the document contains demo SOP (Str.contains ("demo"));
        public static void Method_get () {String str = ' ABCDEAKPF ';
        Length SOP (Str.length ());
        Gets the character sop (Str.charat (3)) based on the index;//stringindexoutofboundsexception occurs when a corner mark that does not exist in the string is accessed.
    Get index according to character SOP (Str.indexof (' m ', 3));//If not found, return-1//Reverse index A character appearing position sop (Str.lastindexof ("a"));
        public static void Main (string[] args) {//Method_get ();//method_is ();
    Method_7 (); }

}

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.