Summary of common methods for STRING in JAVA

  • 2020-04-01 02:16:04
  • OfStack

Create and initialize a string

String b = "hello";

Creates and initializes a string using the constructor

String (); // initializes a string to represent a sequence of null characters

String (value); // creates a new object using an existing string constant

String (char [] value); // creates a string using an array of characters

String (char [] value, int offset, int count); // intercepts the array of characters offset into the count character to create a non-empty string

String (StringBuffer buffer); // initializes the String object using the StringBuffer object

Ii. Use of main methods of String class:

1, get length *.length(); // this is different from getting the length in the array, *.length;

2. Compare the string (1) equals() // to determine if the content is the same

(2)compareTo() // determines the size of the string

(3)compareToIgnoreCase(String int) // ignore letter case when comparing

(4)== // determine whether the content and address are the same

(5)equalsIgnoreCase() // ignore the case to determine whether the content is the same

If you want to compare parts of a string to see if they are the same

(6)reagionMatches() // there are two kinds of public Boolean regionMatches(int toffset, String other,int ooffset,int len); Represents true if a substring of the String object to be compared is the same character sequence as a substring of the parameter other. The String of the String object to be compared starts from the index toffset, and the other String starts from the index ooffset, and the length is len.

Public Boolean reagionMatches(Boolean ignoreCase,int toffset,String other,int ooffset,int len); / / a Boolean parameter indicates whether the comparison of two strings is case-sensitive.

Find a character at a location in the string

Public char charAt (int index); // returns the character at the specified index position, starting at 0

Finds the first or last occurrence of a specified string in a string

The String class provides two methods for finding the first occurrence of a String at a specified location

(1) public int indexOf (String STR); // retrieves STR from the string and returns the position where it first appeared, no -1

(2)public int indexOf(String STR,int fromIndex); // retrieve STR from the first fromIndex character of the string

There are two ways to find the last location

(1) public int lastIndexOf (String STR);

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

If you do not care about the exact position of the string, you can use public Boolean contains(CharSequence s).

Check the beginning and ending characters of the string

The first string of both methods

(1)public Boolean starWith(String prefix,int toffset); // returns true if the string sequence denoted by the parameter prefix is a substring that the object starts from the index toffset

(2) public Boolean starWith (String prefix);

Ending string method

Public Boolean endsWith (String suffix);

Intercept substrings

(1) public String subString (int beginIndex);

(2)public String subString(int beginIndex,int endIndex); // returns a string from beginIndex to endindex-1

To return the last 4 bits, write syetem.out.println (*.substring ()(*.length()-4));

Substitution of strings

The two methods

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

(2)public String replace(CharSequence target,CharSequence replacement); // replace the original etarget sequence with a replacement sequence to return a new string

(3)public String replaceAll(String regex,String replacement); // USES regular expressions to match strings

Eight, string case for conversion

(1) public String toLowerCase (Locale Locale).

(2) public String toLowerCase ();

(3) public String toupperCase (Locale Locale).

(4) public String toUpperCase ();

Remove Spaces at the beginning and end of a string

*. The trim ();

Ten, string conversion

1. Converts a string to an array of characters

Public char [] toCharArray ();

2. Converts a string to an array of strings

Public String [] the split (String regex); //regex is the given match

3. Convert other data types to strings

(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(float f);

(6)public static String valueOf(double d);

(7)public static String valueOf(char[] data);

(8)public static String valueOf(Object obj);

Creation and initialization of mutable strings

Two methods:

Public StringBuffer ();

Public StringBuffer (int caoacity);

Use of main methods of StringBuffer class:

Get a variable string length

(1) public int length ();

(2) public int capacity ();

(3) public void setLength (int newLength);

Compare mutable strings

Compare with the equals() method of the String class, but not the same.

The equals() method in class Object compares the addresses of two objects, not just the contents, but the String class overrides the equals() method when it inherits the Object class, just comparing the contents of the two objects

In the StringBuffer class, the equals() method of the Object class is not overridden, so the address and content are compared.

Append and insert strings

(1) append public StringBuffer append(type t);

(2) insert public StringBuffer insert(int offset,type t); // add a string of type type at offset

Reverse and delete strings

(1) public StringBuffer reverse();

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

Reduce the storage space for variable character sequences

Public void the trimToSize ();

The StringBuffer class is converted to the String class

Public String toString ();


Related articles: