Using substring( ) in Java

By: Fazal  

You can extract a substring using substring( ). It has two forms. The first is String substring(int startIndex)

Here, startIndex specifies the index at which the substring will begin. This form returns a copy of the substring that begins at startIndex and runs to the end of the invoking string.

The second form of substring( ) allows you to specify both the beginning and ending index of the substring:

String substring(int startIndex, int endIndex)

Here, startIndex specifies the beginning index, and endIndex specifies the stopping point. The string returned contains all the characters from the beginning index, up to, but not including, the ending index. The following program uses substring( ) to replace all instances of one substring with another within a string:

// Substring replacement.
class StringReplace {
public static void main(String args[]) {
String org = "This is a test. This is, too.";
String search = "is";
String sub = "was";
String result = "";
int i;
do { // replace all matching substrings
i = org.indexOf(search);
if(i != -1) {
result = org.substring(0, i);
result = result + sub;
result = result + org.substring(i + search.length());
org = result;
} while(i != -1);

The output from this program is shown here:

This is a test. This is, too.
Thwas is a test. This is, too.
Thwas was a test. This is, too.
Thwas was a test. Thwas is, too.
Thwas was a test. Thwas was, too.

Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Hi puru, The syntax is substring(int startIndex, i
View Tutorial          By: Fazal at 2008-09-12 20:17:53

2. I need to know the parameters that we need to pass
View Tutorial          By: puru at 2008-09-08 00:33:46

3. The example program by itself doesn't solve any pu
View Tutorial          By: Fazal at 2008-03-17 21:23:10

4. Where is the methods related
View Tutorial          By: Osvaldo F. L. Mendes at 2008-03-17 15:40:48

5. voire pluse a des command java
View Tutorial          By: lahcine moubarek at 2009-02-16 01:59:17

6. thanks you.
View Tutorial          By: seo at 2009-04-19 03:27:46

7. Could you somehow use the substring method to pars
View Tutorial          By: Eric at 2009-04-26 19:20:26

8. @Eric

You would have to know the lo

View Tutorial          By: Guy at 2010-09-27 17:18:31

9. I think the programmer overlooked that "this
View Tutorial          By: Joe Schmoe at 2010-10-16 00:01:27

10. RegEx would be a good option here...

View Tutorial          By: Ima Retard at 2010-11-26 05:49:32

11. thanks for this sample.. it possible to handle the
View Tutorial          By: nel at 2011-06-13 22:59:45

12. can you help me in codings for get an output like
View Tutorial          By: Sudhar at 2011-08-24 05:47:53

13. Hey guys, can u help me out over here..

View Tutorial          By: Ridge Fernandes at 2012-01-19 04:09:45

14. obieramkar
View Tutorial          By: obieramkar at 2012-01-25 09:25:42

15. Fadnereippend
View Tutorial          By: Fadnereippend at 2012-02-13 12:43:51

16. can some one tell how to dry run above example
View Tutorial          By: neha at 2012-03-24 05:43:49

17. can someone explain above example in detail
View Tutorial          By: neha at 2012-03-24 05:57:34

18. org.replaceAll("is","was"); en
View Tutorial          By: nirosha at 2013-01-28 16:19:11

19. import*;
class CharaaDemofromnet
View Tutorial          By: bond at 2014-08-16 17:02:10

20. Spot onn with this write-up, I actuially feel this
View Tutorial          By: mantenimiento informatico empresas at 2017-03-10 07:16:15

21. I see your website needs some fresh & unique a
View Tutorial          By: MikkiBennete at 2017-04-30 14:21:01

22. Howdy! This post could not be written any better!
View Tutorial          By: carte playstation network at 2017-05-13 17:20:47

23. I see your blog needs some fresh articles. Writing
View Tutorial          By: DemetriaDhak at 2017-05-15 12:26:20

24. Outstanding info once again. I am looking forward
View Tutorial          By: push up bra dd at 2017-06-01 17:56:49

25. jaredpy3
View Tutorial          By: brittneyfk60 at 2017-06-08 10:42:16

26. Hi blogger, i must say you have hi quality content
View Tutorial          By: 76Evie at 2017-08-02 20:33:08