How to get a SubString of a String in Java

Submitted by: 
Language: 
Visitors have accessed this post 400 times.

This is a tutorial in which we are going to create a program that gets substring of a string in java. We all know that a substring is a part of a sentence or a string. Java String class provides substring method with some overloaded parameter.

So, now let's start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of subString.java.

2. Import java.lang package because in this library, substring class is there. Import also javax.swing hence we will use JOptionPane.showInputDialog for inputting and JOptionPane.showMessageDialog for displaying the output.

  1. import java.lang.*;
  2. import javax.swing.*;

3. In your main, initialize string sentence variable as your input using the JOptionPane.showInputDialog.

  1. String sentence;
  2. sentence = JOptionPane.showInputDialog(null, "Enter a sentence or CANCEL.");

Now, we will create again a variable named part as string. Here, we will get the substring of index 2 to our inputted sentence. The first substring method with single parameter startIndex will take some part of the string from the starting index until the last character in the string.

  1. String part = sentence.substring(2);

Then, we will create again a substring method. The second substring method take two parameters, startIndex and lastIndex. This method returns the substring start from startIndex to the lastIndex.

  1. part = sentence.substring(12, 21);

To display the output:

  1. JOptionPane.showMessageDialog(null, "Part of sentence: " + part);
  2. JOptionPane.showMessageDialog(null, "Part of sentence: " + part);

4. Now, input a sentence to our program like the image below.
input

Press F5 and run the program.

Output:

output

Hope this helps! :)

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.