Methods commonly used strings in Java

First, create and initialize the string:

 

  1, the use of direct initialization string constant S = String "Hello!";


  2, using the constructor creates and initializes String (); // initialize an object representing the null character sequence


  String (value); // character already present Create a new constant string objects


  string (char [] value); // create an array of characters by using a string


  string (char [] value, int offset, int count); // offset into the array of characters taken count characters created a non-empty string


  String (StringBuffer buffer); // initialize StringBuffer object String object using


  two, using the method of the main class String:


  1, obtaining the length * .length (); // this array of different length acquired, *. length;


  2, comparing strings (1) equals () // determines whether the contents of the same


  (2) compareTo () // determines the magnitude relation between the string


  (3) compareToIgnoreCase (string int) // case letters are ignored in the comparison


  (4) == // address is the same as the content is determined


  (5) equalsIgnoreCase () // content is determined ignoring case is the same


  if you want to part of the string is the same for comparison, can be used


  (6) reagionMatches () // There are two public boolean regionMatches (int toffset, String other, int ooffset, int len); if a child represents a parameter other substrings in the string String object are the same sequence of characters , then true. string string object to be compared begins at index toffset, other string starting at index ooffset, length len.


  public boolean reagionMatches (boolean ignoreCase, int toffset, String other, int ooffset, int len); // indicates whether the comparison case-sensitive character string with two Boolean parameters.


  Third, find a location in the string of characters in


  public char charAt (int index); // Returns the character at the specified index index position, the index range from zero


  four specified search string for the first time or the last in a string word appearing positions


  provided in the string class two methods to find the position specified position of the first occurrence of the string


  (1) public int indexOf (string str); // start retrieving the string str, and returns the first the position of the first occurrence of -1 does not appear


  (2) public int indexOf (string str, int fromIndex); // fromIndex from the start character string retrieving str


  find the location of the last occurrence of two ways


  (1 ) public int lastIndexOf (String str) ;


  (2) public int lastIndexOf (String str, int fromIndex);


  if you do not care about the exact location of the string, you can use public boolean contains (CharSequence s);


  V, starting and ending character check character string


  beginning two strings method


  (1) public boolean startsWith (string prefix, int toffset); // if the sequence parameter string prefix represents the substring from the beginning of the object at index toffset, return to true


  (2) public Boolean startsWith (string prefix);


  string method ends of


  public boolean endsWith (string suffix);


  six, taken substring


  (. 1) public string the substring (int the beginIndex);


  (2) the substring public string (the beginIndex int, int endIndex); // return string from beginning to beginIndex endIndex-1 strings


  after 4 to be returned can be written Syetem.out.println (* subString () (* length () - 4)..);


  Alternatively seven string


  two kinds of method


  (1) public String replace (char oldChar, char newChar);


  (2) public String replace (CharSequence target, CharSequence replacement); // the original target sequences replaced with replacement sequence, and returns the new string


  (3) public String replaceAll (String regex, String replacement); // regular expression achieve matching string of


  eight, the case for converting a character string


  (. 1) public string the toLowerCase (Locale the locale);


  (2) the toLowerCase public string ();


  (. 3) public string toUpperCase (Locale the locale);


  (. 4) public string toUpperCase ();


  nine, removing the string trailing spaces


  * .trim ();


  X. string conversion


  1, the string into character array


  public char [] toCharArray ();


  2, the string into an array of strings


  public string [] split (string regex ); // regex matching is given


  3, the conversion is a string of other data types


  (. 1) public static string valueOf (Boolean B);


  (2) public static string valueOf (char C );


  (. 3) public static String valueOf (int I);


  (. 4) public static String valueOf (Long I);


  (. 5) public static String valueOf (a float F);


  (. 6) public static String valueOf (Double D);


  (. 7 ) public static string valueOf (char [] Data);


  (. 8) public static string valueOf (Object obj);


  variable initialization strings and create


  two methods:


  public the StringBuffer ();


  public the StringBuffer (int caoacity);


  the StringBuffer class the main methods used:


  First, the acquiring of variable length strings


  (. 1) public int length ();


  (2) public int Capacity ();


  (. 3) public void the setLength (int the newLength);


  two, variable string comparison


  with Comparative equals String class () method, but is different.


  Equals in class Object () method to compare two objects are equal address, not just compare the contents, but the String class overrides the equals () method inherited Object class, just compare the contents of two objects equality


  In StringBuffer class does not override equals Object class () method, and the content is the address comparison.


  Third, the addition or insertion string


  (1) adding the append the StringBuffer public (T type);


  (2) inserted public StringBuffer insert (int offset, type t); // offset for adding a string of type type


  IV reverse and deleting a string


  (1) reverse reverse the StringBuffer public ();


  (2) deleting the public StringBuffer delete (int start, int end);


  five, for reducing the storage space of the variable character sequence


  public void trimToSize ();


  VI. StringBuffer class is converted into String class


  public String toString ();

Guess you like

Origin www.cnblogs.com/hd92/p/10951510.html