JAVA – string methods – 3

This the third post for Java string methods. In this post I will detail more functions and its functionalities.

split()

This method has two variants and splits this string around matches of the given regular expression.

Syntax:

or

  • regex — the delimiting regular expression.
  • limit — the result threshold which means how many strings to be returned.
  • It returns the array of strings computed by splitting this string around matches of the given regular expression.

Example:

Result:

Java – String startsWith() Method

This method has two variants and tests if a string starts with the specified prefix beginning a specified index or by default at the beginning.

Syntax:

or

 

  • prefix — the prefix to be matched.
  • toffset — where to begin looking in the string.
  • It returns true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise.

Example:

Result:

Java – String subSequence() Method

This method returns a new character sequence that is a subsequence of this sequence.

Syntax:

  • beginIndex — the begin index, inclusive.
  • endIndex — the end index, exclusive.
  • This method returns the specified subsequence.

Example:

Result:

Java – String substring() Method

This method has two variants and returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string or up to endIndex – 1 if second argument is given.

Syntax:

or

  • beginIndex — the begin index, inclusive.
  • endIndex — the end index, exclusive.
  • The specified substring.

Example:

This produces the following result: