Tutorial Gateway

  • C Language
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • Js

String subSequence in Java

by suresh

The String subSequence in Java is one of the Java String Method which is used to return new character sequence that is subsequence of the user specified string.

In this article we will show you, How to use String subSequence in Java Programming language with example.

Syntax of a String subSequence in Java

The basic syntax of the string.subSequence in Java Programming language is as shown below.

The following method will accept the integer value that is the starting index position (Starting_index) where the extraction will start as the first parameter and last index position (End_index) where the character sequence will end as the second argument. Java String.subSequence method will return the character sequence starting from the Starting_index up to End_index but not included.

public CharSequence subSequence(int Startig_index, int End_index); // It will return Character Sequence 

//In order to use in program
String_Object.subSequence(int Startig_index, int End_Index)
  • String_Object: Please specify the valid String Object.
  • Starting_Index: Please specify the starting index position. This is the index position where the extraction of character sequence will start.
  • End_Index: Please specify the ending index position. Java String.subSequence function will return upto this index position but it wilkl not include the character at this position (End_index).

Example for String subSequence in Java

The Java string.subSequence method is used to extract the part of a string and return in new string.

This program will help you to understand the string.subSequence method.

JAVA CODE

//String. substring in Java example
package StringFunctions;

public class SubSequenceMethod {
	public static void main(String[] args) {
		String str = "Tutorials on Java Programming";
		String str1 = "We are abc working at abc company";
		
		System.out.println("subSequence of str = " + str.subSequence(0, 10));
		System.out.println("subSequence of str1 = " + str1.subSequence(0, 12));
		
		System.out.println("subSequence of str = " + str.subSequence(5, 10));
		System.out.println("subSequence of str1 = " + str1.subSequence(5, 12));
		
		System.out.println("subSequence of str = " + str.subSequence(6, str.length()));
		System.out.println("subSequence of str1 = " + str1.subSequence(5, str1.length()));
	}
}

OUTPUT

String subSequence in Java 1

ANALYSIS

First we declared two String variables str, str1 and assigned corresponding values using following statement

String str = "Tutorials on Java Programming";
String str1 = "We are abc working at abc company";

Following statement will call the public CharSequence subSequence (int Starting_Index, int End_Index) method and return the character sequence starting from index 0 to index position 9

System.out.println("subSequence of str = " + str.subSequence(0, 10));

This statement will call the public CharSequence subSequence (int Starting_Index, int End_Index) method and return the character sequence starting from index 5 to index position 9

System.out.println("subSequence of str = " + str.subSequence(5, 10));

Following statement will return the character sequence starting from index 6 to end of the string. Here, we used the String length function to find the string length

System.out.println("subSequence of str = " + str.subSequence(6, str.length()));

Thank You for Visiting Our Blog

Placed Under: Java

Trending Posts

Java Leap Year Program

Java Program to Count Positive and Negative Numbers in an Array

SQL CURRENT_TIMESTAMP

Python Program to Reverse a Number

Copy Column Transformation in SSIS

Java String compareTo

Java Array Methods

JavaScript getUTCSeconds

Python Iterator

Count Records in SQL Group

  • C Programs
  • Java Programs
  • SQL FAQ’s
  • Python Programs
  • SSIS
  • Tableau
  • JavaScript

Copyright © 2019 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy