Common method ## API (four) ---- String class

String class common method

##I. Overview

  In Java, as the object character string is of type String processed. String class located Java.lang package, by default, the packet is automatically imported into all programs

## Second, create a String object

  String s="Hello World";

  String s=new String("Hello World");

## Third, the usual method of the String class:

  1, the length of the length of the string find ();

    Syntax: String .length ();

  2, the string comparison

    Syntax: String 1.equals (String 2);

    Comparing the values ​​of two strings are the same, return a boolean value

  Note:

    1 (interview questions), in Java, double equal sign (==) and equals () method is applied to the comparison, comparing the content of two strings differ. "==" string comparison of two objects in memory address, determines whether the same is a string object, and equals () is a value two comparison string object.

    2, the process of comparing strings in Java, there are some lowercase, uppercase string some of their method of comparison is equalsIgnoreCase () method, which ignores the case of strings when comparing strings.

  3, the link string of the concat ();

    Syntax: String 1.concat (String 2);

    2 is spliced ​​string to the string 1 returns a new string.

  4, extract and query strings

method Explanation
public int indexOf(int ch) Search and returns the first character position appears ch
public int indexOf(String value) Search and returns the position of the first occurrence of the string value
public int lastIndexOf(int ch) Search and returns the last character position appears ch
public int lastIndexOf(String value) Search and returns the position of the last occurrence string value
public String substring(int index) Extracting the string starting from the specified index
public String substring(int beginindex,int dendingex) And character string extraction between beginindex endindex
pubic String trim() Taken before and after the space string returns a new string

  5, string into

  Syntax: String name .split (separator, limit);

    separator: is optional, represents to split this string matches the specified regular expression

    limit: Optional, the return value is used to limit the number of elements in the array.

  6, for example white:

    After the development process, with a relatively large number of return string length, string and splitting

public class Lyric {
    public static void main(String[] args) {
        //定义一个新的字符串
        String words="Hello Java World Demo";
        //返回该字符串的长度
        System.out.println(words.length());
        System.out.println("拆分前:"+words);
        System.out.println("拆分后:");
        String[] split = words.split(" ");
        for(String a:split){
            System.out.println(a);
        }
    }
}

Guess you like

Origin www.cnblogs.com/liurui-bk517/p/11088653.html