×

Java String subSequence() method

This method returns a new character sequence i.e. subsequence of current sequence

Syntax:

public CharSequence subSequence(int beginIndex, int endIndex)

Parameter:

beginIndex ? begin index, inclusive.

endIndex ? end index, exclusive.

Return: specified subsequnce

Throws:

It throws IndexOutOfBoundsException if begin or end index is negative , if end is greater than length(), or if start is greater than end.

Java String subSequence() Example 1

public class JavaStringSubSequenceEx1
{
      public static void main(String[] args)
      {
            String s1 = "A subSequence Example.";
            CharSequence cs = s1.subSequence(2, 13);
            System.out.println();
            System.out.println(cs);
        System.out.println();
      }
}

Output:

subsequence

Java String subSequence() Example 2

public class JavaStringSubSequenceEx2
{
   public static void main(String args[])
   {
      String Str = new String("Welcome to tutorialandexample.com");
      System.out.print("Return Value :" );
      System.out.println(Str.subSequence(0, 10) );
      System.out.print("Return Value :" );
     System.out.println(Str.subSequence(10, 29) );
   }
}

Output:

Return Value :Welcome to
Return Value : tutorialandexample

Java String subSequence() method Example 3

import java.lang.Math;
public class JavaStringSubSequenceEx3{
    // driver code
    public static void main(String args[])
    {
        String Str = "Welcome to tutorialandexample";
        // throws an error as index is negative
        System.out.print("Returns: ");
        System.out.println(Str.subSequence(-1, 7));
    }
}

Output:

Returns: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
       at java.lang.String.substring(String.java:1960)
       at java.lang.String.subSequence(String.java:2003)
       at JavaStringSubSequenceEx3.main(JavaStringSubSequenceEx3.java:13)

Java String subSequence() Example 4

public class JavaStringSubSequenceEx4
{
   public static void main(String args[]) {
      String Str = new String("Welcome to Tutorialandexample.com");
          System.out.println(Str.subSequence(11, 29) );
   }
}

Output:

Tutorialandexample

Related Topics

Java CountDownLatch

Another crucial classes for concurrent execution is CountDownLatch. It is a synchronisation tool that enables one or more threads to await until a series of tasks started by another thread...

4 minutes read.

Java Database Connectivity with Oracle

JDBC: A Programmer can develop a complete application using the Java built-in API’s. So, for storing the data required for solving a real-world problem is stored into a database. To connect...

5 minutes read.

Math fma () method in java

In java, the Math module constitutes of fma () Method in it. This method can be accessed in two ways which can be differentiated by the parameters which are given...

4 minutes read.

Pig Latin Program in Java

Pig Latin Program in Java Pig Latin is a method for translating words of the English language into a different language. It is an encrypted word that is generated by using the following steps....

4 minutes read.

Classes and Objects in Java Example Programs

Classes and Objects in Java Example Programs Java is an Object-Oriented programming language, i.e., everything in Java is associated with objects and objects are associated with classes. The classes and objects...

5 minutes read.

Java Worker Class

What is a Worker? A service which executes Work - flow and Activities is referred to as a Worker. On user-controlled hosts, workers are defined and put into action. The Worker...

3 minutes read.

How to Convert String to Date in Java

How to Convert String to Date in java You can convert String to Date in Java by using the parse() method. There are two classes which have parse() method namely, DateFormat and SimpleDateFormat classes....

2 minutes read.

Difference between JIT and JVM in Java

In this tutorial, we will discuss the difference between JIT (Just In Time Compiler) and JVM (Java Virtual Machine) in Java. Before we move to the differences, let's understand what...

4 minutes read.

Static Array in Java

In this tutorial, we will study static arrays in Java. An array is a data structure that is of great importance in any programming language. It is classified into two...

3 minutes read.

Bucket Sort in Java

Bucket Sort in JavaBucket sort is also called bin sort. Bucket sort first puts the elements of the array or list into different buckets. The first bucket contains elements of the smallest value. The...

11 minutes read.

Java String vs StringBuffer

Java String vs StringBuffer In this section, we will discuss the key differences between String and StringBuffer class. Before moving to the ahead in this section, let’s introduce with both classes. String...

4 minutes read.

Java Abstraction

Java Abstraction Abstraction is an advanced feature of Java to make it transparent. The main motive behind the abstraction is to deal with ideas, not with events. Abstraction is a process...

4 minutes read.

Java Math cosh() Method

The cosh() method of Math class returns the first hyperbolic cosine((e+e)/2) of a double value. Syntax: public static double cosh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic cosine is to be...

2 minutes read.

Java Math getExponent() Method

The getExponent() method of Math class returns the unbiased exponent of the argument. Syntax: public static int getExponent (double d) Parameters: The parameter ‘d’ represents the double value. Return Value: The getExponent () method returns the...

1 minute read.

Parallel Arrays Sort in Java

Sorting is a technique of arranging a sequence of numbers in ascending order, that is, the first number being lowest and gradually incrementing the numbers such that the last number...

7 minutes read.

Java Math toIntExact() Method

The toIntExact() method of Java Math class returns the int value of the given long argument, throwing an exception if the value overflows an int. Syntax: public static int toIntExact (long value) Parameters: The...

2 minutes read.

How to Convert String to Object in Java

How to Convert String to Object in Java The Object is the super class of all classes. So you can assign a string to Object directly. There are two methods to...

3 minutes read.

Java Enumeration

In a computer language, enumerations express a set of named constants. For instance, the four suits in a deck of playing cards could be represented by the enumerators Club, Diamond,...

4 minutes read.

Minimum Lights to Activate Java Snippet Class

Minimum Lights to Activate Problem in Java In prison, there is a hallway that is N units long. Given an N-dimensional array A. If the light at the ith position is...

3 minutes read.

Two Decimal Places Java

When a double data type is used in Java before a variable, this indicates 15 digits after decimal point. However, there are situations when we only require 2 decimal places...

4 minutes read.