java-string (String class and surrounding classes of String class) (end)

In the previous section, the definition, characteristics, some common methods and surrounding methods of the String class were introduced.

java-string (String class and surrounding classes of String class) (on)

In this section, we continue to learn some of the commonly used methods in the String class.


stratsWith and endWith







stratsWith and endWith


Tests if this string starts with the specified prefix.

Tests whether this string begins with the specified prefix.


Tests if this string ends with the specified suffix.

Tests whether this string ends with the specified suffix.

 Code example:

  1. public class Demo1 {
  2. public static void main ( String[] args ) {
  3. String s = "Hello" ;
  4. System.out.println ( s.startsWith ( "Hello" ));
  5. System.out.println ( s.endsWith ( "Hello" ));
  6. System.out.println ( s.contains ( "Hello" ));
  7. System.out .println ( s.startsWith ( "ell" , 1 )); //Check if a string starts from a subscript position, if yes, return true
  8. }
  9. }


1) replace(char oldchar,char newchar) (replace character)

Returns a string resulting from replacing all occurrences of
 {@code oldChar} in this string with {@code newChar}.

Returns a new string that replaces all old characters with new characters.

2) repalce(CharSquence target,CharSquence replacement) (replacement string)

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence to get a new string and returns it.

replace with regular expressions

3) replaceAll(String regex, String replacement)

The regex here refers to the regular expression string. This is to introduce the method related to String, so I will not expand to explain the regular expression.

You can understand it through this friend's article

Regular Expression Usage and Examples

4) replaceFirst(String regex, String replacement)

Code example:

  1. String t = s.replace( 'l' , 'o' ); //replace character
  2. System.out.println ( s );
  3. System.out.println ( t );
  4. String tt = s.replace( "ll" , "xx" ); //replace the string
  5. System.out.println ( tt );
  6. System.out .println ( s.replaceAll ( "." , "xx" )); replace all characters with xx
  7. System.out .println ( s.replaceFirst ( "." , "xx" )); //replace the first occurrence of the character with xx
  8. . represents all characters in a regular expression

operation result: 

The first three methods are all replacement, only the fourth method is to replace the first occurrence of the character.


substring(int beginIndex)

substring(int beginIndex,int endIndex)

  1. String s = "Hello" ;
  2. System.out.println( s.sub string ( 2 ));
  3. System.out.println( s.sub string ( 2 , 5 ));

operation result: 


The split () method is used to split a string into an array of strings

split(String regex)   

 The overloaded form of split(String regex, int limit) is used to return only part of the characters

For example:


get [h, ello]

Code example:

  1. String s = "" ;
  2. String[] parts = s.split ( "//." , 2 );
  3. System.out.println( String (parts ) );
  4. .has a special meaning in regular expressions, to be used, it needs to be escaped
  5. / has special meaning in java string, use need to escape it


The trim() method is used to remove whitespace at the beginning and end of a string. The whitespace includes: spaces, tabs, newlines and other whitespaces.

The trim() method does not alter the original string.

The trim() method does not work with null, undefined, Number types.

  1. String s = " he llo " ;
  2. String t = s. trim ();
  3. System.out.println ( t ) ; _

 operation result:


method modifiermethod nameparameterbasic introduction
equals(...)another stringequality comparison
equalsIgnoreCase(...)another stringCase-ignoring equality comparisons
compareTo(...)another stringsize comparison
compareToIgnoreCase(...)another stringCase-ignoring case-by-case comparisons
length(...)noneget string length
charAt(...)subscriptGet the character corresponding to the subscript position
toCharArray(...)noneGet the char array corresponding to the string
indexOf(...)character to findFind the character or string you are looking for from front to back
lastIndexOf(...)character to findFind the character or string you are looking for from back to front
contains(...)substring to be searchedDetermine whether it contains the substring to be searched
valueOf(...)various typesOther types get string representation
staticformat(...)Formatting and parametersget string by formatting
startsWith(...)substringDetermine if it starts with a substring
endsWith(...)substringCheck if it ends with a substring
replace(...)character to be replaced, new characterReplace all characters to be replaced with new characters
split(...)Delimiter for regular expressionsSplit string by delimiter
staticjoin(...)delimiter, string array (sequence)Concatenate strings according to delimiters
trim(...)noneTrim leading and trailing whitespace characters from a string

The above briefly introduces some commonly used methods for beginners. If you need to know other methods, you can go to the official website of java to have a look (pure English)

Introduction and usage of all String methods

Or there are also detailed introductions in the structure in the idea.

If there is any problem with the personal writing, please criticize and correct it. If the sharing of the above content is helpful to you, please click three times! ! !

Tags: java-string (String class and surrounding classes of String class) (end)

java elementary java Development language rear end

Related: java-string (String class and surrounding classes of String class) (end)