×

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 Boolean equals() method

The equals() method of Java Boolean class returns a Boolean value true if the specified argument is not null and is same as this object, else it returns false. Syntax public boolean...

2 minutes read.

Java FileOutputStream

What is FileOutputStream?When we need raw stream data written into a file, we need to look for another option: FileOutputStream. It is used when the file's data is byte-oriented. It comes under...

4 minutes read.

Convert JSON to Map in Java

JACKSON and GSON libraries are two extremely capable JSON-related libraries offered by Java. To efficiently work with the returned JSON data, we frequently need to convert JSON responses into a...

6 minutes read.

How to add double quotes in a string in Java

Strings are indicated using double-quotes. Double quotes are not printed; the values inside the double quotes are printed. There are different methods for adding double quotes to the string, such...

2 minutes read.

Switch Case with Enum in Java

From some conditions, the java switch statement executes one statement. Similar to the If-Else-If ladder statement, this can be Byte, short, int, long, enum, string, and some wrapper types like...

4 minutes read.

How to Split String by Comma in Java

strsplit() technique permits you to break a string given the explicit Java string delimiter. The Java string split property is frequently a space or a comma(,) that you want to...

7 minutes read.

Java Volatile Keyword

The compiler, runtime, or processors may use any kind of optimization if there aren't any required synchronizations. Although most of the time these improvements are advantageous, they occasionally can result...

6 minutes read.

How to check if date is valid in Java?

In this article, you will acknowledge about how to verify if a date is valid or not. For this you will learn the approach, you will be able to write...

3 minutes read.

How to Compare Two Strings in Java

How to Compare Two Strings in Java On the basis of reference or content, one can compare two strings in Java. String comparison is used in reference matching (== operator), sorting...

4 minutes read.

Radix Sort in Java

Radix Sort in Java Radix sort in Java uses the digits of the numbers given in the sorted array to sort the numbers. The radix sort uses the place value of...

5 minutes read.

Segment Tree in Java

Binary trees can address a variety of issues; however, the Segment Tree is more efficient in terms of time complexity. The segment tree in Java is represented using an array. Native...

4 minutes read.

Java Math min() Method

The min() method of Math class returns the smaller of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double min (double a, double...

2 minutes read.

Program to check whether a given character is present in a string or not

In this article, you will understand the logic to find out whether the given character is present in the string or not and find out the position of the specified...

3 minutes read.

How to Install Java on MAC

There are many possible ways to install java on mac. This article is based on the installation of java on mac. The operating system platform is Mac OS X, macOS and...

3 minutes read.

Java program to count the occurrences of each character

The count of each character is the frequency of the characters. For example, the given String is “Programming”. In the given string, the count of the characters ‘P’ -1, ’r’-2,...

6 minutes read.

Stack Program in Java

Stack Program in Java The Stack class is part of the collection framework that inherits the Vector class. Thus, the Stack class can also be called a subclass of the Vector...

5 minutes read.

Ternary Operator in Java

In some cases, the if...else expression in Java can be replaced by a ternary operator. Visit the Java if...else statement first before learning about the ternary operator. Ternary operator in Java A...

3 minutes read.

Java ResultSetMetaData

The data about another data is called Metadata. The ResultSetMetaData is used to store the data about ResultSet. The ResultSet Contains the columns, rows, names of table, datatypes etc. these...

2 minutes read.

Java Class Name

How to write a class name? The following considerations should be made while writing class names. The name of the current class shouldn't be based on a preset or existing class. Java keywords...

3 minutes read.

How to Set Java_home in Linux

To set the Java_home in Linux, we must follow several steps to make us understand it easily.  Java follows the principle called WORA (write once run anywhere). We know that java...

3 minutes read.