How to Use Substring() in Java Programming

How to Use Substring() in Java Programming
Hacking Exposed : Network Security Secrets and Solutions No. 6 by Stuart…
Current Bid: $18.00

What is a substring() in Java Programming

Substring() is a pre-defined method under the pre-defined class String. There are 2 kinds of substring(), first is with only 1 parameter and second is with 2 parameters.

Substring() #1: substring(int firstIndex)

Substring() #2: substring(int firstIndex, int lastIndex)

How to Use Substring() in Java Programming?

You can use substring() if you want to return a certain string minus the specified index or indexes of a certain string, print it on the screen or simply use it as a condition.

For example: If we will use the substring() #1 to return a certain string. Here how it goes.

Declared_String: Learn Java Programming

Java Code: Declared_String.substring(1);

The Java Code above will return ‘earn Java Programming’, without the first index ‘L’ since ‘L’ is the first index of ‘Learn Java Programming’. Similarly, if we have declared strings like as follows:

‘Decode’ with substring(2), it will return the string ‘code’ without the 2 indexes ‘d and e’, while if we have:

‘Debugging’ with substring(2, 5), it will return the string ‘bug’ without 2 first indexes ‘d and e’ and last 4 indexes ‘g, i, n and g, ’.

Note: that for substring(int firstIndex, int lastIndex), the last index meant in there is the number you declare minus 1. Like that on substring(2, 5), the 5 there was subtracted to 1, so it only subtracted 4 indexes instead of 5. To see the running code for this, check the code below:

package substringinjava;
import java.util.Scanner;
public class Main {
public static void main(String[] args)
String word1 = “Learn Java Programming”;
System.out.println(“Result for ‘Learn Java Programming’ with substring(1)”);
String word2 = “decode”;
System.out.println(“Result for Return ‘decode’ with substring(2)”);
String word3 = “debugging”;
System.out.println(“Return for ‘debugging’ with substring(2, 5)”);
System.out.println(word3.substring(2, 5));

Sample Output:

If you find this hub useful to you, please rate and share it. Happy Programming!

Related Java tutorials:

5 Important Tips to Learn Java Programming and Other Programming Languages
Basic Knowledge Required in Programming
How to Program in Java Using Netbeans: Complete Simple Easy Steps
Java Simple Codes for Beginners
Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming
Java Tutorial Examples

Java Video Tutorials

Java Programming Video Training tutorials CBT 30+ Hrs
Current Bid: $4.99
Advanced Java Programming Video Training tutorials CBT – 30+ Hrs – Master level
Current Bid: $4.99

Java Books

Programming with Java : A Multimedia Approach by Radhika S. Grover (2011,…
Current Bid: $.99
Introduction to Java Programming, Comprehensive Version 9e by Y. Daniel Liang
Current Bid: $50.80

Android Development Tutorial

InfiniteSkills Corona SDK iOS / Android Development Tutorial – Training DVD-ROM
Current Bid: $79.96

Other Java Code Samples:

Java Program: How to Use If Statement in Java Programming
Java Program: How to Use If-Else Statement in Java Programming
Java Program: Using Multi If and Else Statement in Java Programming
What is MDAS? Sample Java Source Code for MDAS
What is a Java Scanner Class and How to Use it?
Sample Program for JOptionpane and Java Source Code for Basic Calculator
Sample Java Program for String Tokenizer
Java Program: How to Sort Numbers in Ascending Order
Java Program: How to Sort Numbers in an Array in a Descending Order
Java Program: Palindrome Test Java Source Code
Java Program: Reverse String in Java Using For Loop
Java Program: Count the Number of String Characters in Java
Java Program: How to Parse a String into Integer in Java Programming
Java Program: How to Use Switch Statement in Java
Class in Java: Learn More about Java Classes with Sample Java Codes
Java Source Code: Sort Numbers in Selection Sort
Java Source Code Recursion: Recursive Koch Snow Flakes
Java Source code on Printing the Greatest Common Divisor (GCD) using Recursion
Java Source code: Reverse String in Java Using Recursion
Java Source code: How to Add numbers inside an Array Using Recursion
Java Source Code in a Recursive Linear Search
Java Source Code: A Recursive Asterisk Diamond Shape Program
Java Code Sample: Sort Numbers in Bubble Sort
Java Source Code Determine Person’s Salutation and Current Age
Java source code in Recursive function for X to the power Y
Java Source Code: Binary Search in Recursion
Java Source code sample: How to Add Numbers inside an Array using For Loop
Java source code Sample: Print Different Asterisk Shapes in Recursion