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
System.out.println(org);
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.
Archived Comments
1. Hi blogger, i must say you have hi quality content here.
Your website should go viral. You ne
View Tutorial By: 76Evie at 2017-08-02 20:33:08
2. jaredpy3
View Tutorial By: brittneyfk60 at 2017-06-08 10:42:16
3. Outstanding info once again. I am looking forward
for your next post;)
Here i
View Tutorial By: push up bra dd at 2017-06-01 17:56:49
4. I see your blog needs some fresh articles. Writing manually is time consuming, but there is solution
View Tutorial By: DemetriaDhak at 2017-05-15 12:26:20
5. Howdy! This post could not be written any better! Reading through this article reminds me of my prev
View Tutorial By: carte playstation network at 2017-05-13 17:20:47
6. I see your website needs some fresh & unique articles.
Writing manually is time consuming
View Tutorial By: MikkiBennete at 2017-04-30 14:21:01
7. Spot onn with this write-up, I actuially feel this
amazing site needs much more attention. I
View Tutorial By: mantenimiento informatico empresas at 2017-03-10 07:16:15
8. import java.io.*;
class CharaaDemofromnet
{
public static void main(String args
View Tutorial By: bond at 2014-08-16 17:02:10
9. org.replaceAll("is","was"); enough to replace all is with the was.
View Tutorial By: nirosha at 2013-01-28 16:19:11
10. can someone explain above example in detail
View Tutorial By: neha at 2012-03-24 05:57:34
11. can some one tell how to dry run above example
View Tutorial By: neha at 2012-03-24 05:43:49
12. Fadnereippend
View Tutorial By: Fadnereippend at 2012-02-13 12:43:51
13. obieramkar
View Tutorial By: obieramkar at 2012-01-25 09:25:42
14. Hey guys, can u help me out over here..
this is a game called wheel of fortune..
nd wa
View Tutorial By: Ridge Fernandes at 2012-01-19 04:09:45
15. can you help me in codings for get an output like this:
If user input is abc, the substring h
View Tutorial By: Sudhar at 2011-08-24 05:47:53
16. thanks for this sample.. it possible to handle the error of indexOf if it could not find the string?
View Tutorial By: nel at 2011-06-13 22:59:45
17. RegEx would be a good option here...
[^A-Za-z0-9]+is[^A-Za-z0-9]+
View Tutorial By: Ima Retard at 2010-11-26 05:49:32
18. I think the programmer overlooked that "this" has "is" within it. So, if I'm co
View Tutorial By: Joe Schmoe at 2010-10-16 00:01:27
19. @Eric
You would have to know the locations of where you would like to parse before us
View Tutorial By: Guy at 2010-09-27 17:18:31
20. Could you somehow use the substring method to parse text? For example, I have the string:
&q
View Tutorial By: Eric at 2009-04-26 19:20:26
21. thanks you.
View Tutorial By: seo at 2009-04-19 03:27:46
22. voire pluse a des command java
View Tutorial By: lahcine moubarek at 2009-02-16 01:59:17
23. Hi puru, The syntax is substring(int startIndex, int endIndex). Therefore you should pass two intege
View Tutorial By: Fazal at 2008-09-12 20:17:53
24. I need to know the parameters that we need to pass on substring() methods.
View Tutorial By: puru at 2008-09-08 00:33:46
25. The example program by itself doesn't solve any purpose. However it shows how to use the substring f
View Tutorial By: Fazal at 2008-03-17 21:23:10
26. Where is the methods related
View Tutorial By: Osvaldo F. L. Mendes at 2008-03-17 15:40:48
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Java program to get location meta data from an image
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program