substring( ) in Java

// 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); 

}

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|