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:

public String[] split(String regex, int limit)

or

public String[] split(String regex)
  • 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:

public class test01 {

   public static void main(String args[]){
      String Str = new String("Another-Hello-World-!");

      for (String retval: Str.split("-", 1)){
          System.out.println(retval);
       }
       System.out.println("");
       
      for (String retval: Str.split("-", 2)){
         System.out.println(retval);
      }
      System.out.println("");
      
      for (String retval: Str.split("-", 3)){
          System.out.println(retval);
       }
       System.out.println("");

      for (String retval: Str.split("-", 0)){
         System.out.println(retval);
      }
      System.out.println("");

      for (String retval: Str.split("-")){
         System.out.println(retval);
      }
   }
}

Result:

Another-Hello-World-!

Another
Hello-World-!

Another
Hello
World-!

Another
Hello
World
!

Another
Hello
World
!

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:

public boolean startsWith(String prefix, int toffset)

or

public boolean startsWith(String prefix)

 

  • 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:

public class test02{
   public static void main(String args[]){
      String Str = new String("Hello Sami");

      System.out.println(Str.startsWith("Hello") );
      System.out.println(Str.startsWith("Sami", 6) );
      System.out.println(Str.startsWith("Sami") );
   }
}

Result:

true
true
false

Java – String subSequence() Method

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

Syntax:

public CharSequence subSequence(int beginIndex, int endIndex)
  • beginIndex — the begin index, inclusive.
  • endIndex — the end index, exclusive.
  • This method returns the specified subsequence.

Example:

public class test03 {

	public static void main(String args[]) {
		String Str = new String("Hello Sami");

		System.out.println(Str.subSequence(0, 5));
		System.out.println(Str.subSequence(6, 10));
	}
}

Result:

Hello
Sami

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:

public String substring(int beginIndex)

or

public String substring(int beginIndex, int endIndex)
  • beginIndex — the begin index, inclusive.
  • endIndex — the end index, exclusive.
  • The specified substring.

Example:

public class test04 {

	public static void main(String args[]) {
		String Str = new String("Hello Sami and John");

		System.out.println(Str.substring(6));
		System.out.println(Str.substring(6, 10));
	}
}

This produces the following result:

Sami and John
Sami

 

Comments on this post

No comments.

Leave a Reply

Your email address will not be published. Required fields are marked *

twelve − three =

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Trackbacks and Pinbacks on this post

No trackbacks.

TrackBack URL