Using substring() method
The substring() method could be used to search and replace a substring in the main String. The below example searches and replaces the first instance of a substring in the original String. package JavaTest1;
//This class search and replace the first
//instance of a String in the main String.
public class SearchReplaceSubString {
/**
* @param args
*/
public static void main(String[] args) {
String str = "This is a search and replace of substring example";
System.out.println("The original String is: " + str);
// We want to look for the String "search" and replace it with "find"
String searchQuery = "search";
String replaceWord = "find";
int startIndexVal = 0;
int endIndexVal = 0;
// look for the starting index of searchQuery
startIndexVal = str.indexOf(searchQuery);
//This class search and replace the first
//instance of a String in the main String.
public class SearchReplaceSubString {
/**
* @param args
*/
public static void main(String[] args) {
String str = "This is a search and replace of substring example";
System.out.println("The original String is: " + str);
// We want to look for the String "search" and replace it with "find"
String searchQuery = "search";
String replaceWord = "find";
int startIndexVal = 0;
int endIndexVal = 0;
// look for the starting index of searchQuery
startIndexVal = str.indexOf(searchQuery);
// evaluate the ending Index of searchQuery
endIndexVal = startIndexVal + searchQuery.length();
// Form the string again using the substring() method
str = str.substring(0, startIndexVal) + replaceWord
+ str.substring(endIndexVal);
System.out.println("The modified String is: " + str);
}
}
endIndexVal = startIndexVal + searchQuery.length();
// Form the string again using the substring() method
str = str.substring(0, startIndexVal) + replaceWord
+ str.substring(endIndexVal);
System.out.println("The modified String is: " + str);
}
}
When the above code is executed the output is:-
The original String is: This is a search and replace of substring example
The modified String is: This is a find and replace of substring example
The modified String is: This is a find and replace of substring example
Using replace() method
The String Class provides a very effective way to replace all the occurrences of a substring in the main stringIt uses the replace() method of the String class. The example below looks for a substring and replaces all the occurrences of that substring with a new substring.
package JavaTest1;
//This class search and replace all the
//instances of a substring in the main String.
public class SearchReplaceSubString {
/**
* @param args
*/
public static void main(String[] args) {
String str = "Using Replace method for the searching and searching and replacing of a substring in a String";
System.out.println("The original String is: " + str);
// We want to look for the String "search" and replace it with "find"
String searchWord = "search";
String replaceWord = "find";
// Replace all the occurrences of the searchWord with the replaceWord
str = str.replace(searchWord, replaceWord);
System.out.println("The modified String is: " + str);
}
}
//This class search and replace all the
//instances of a substring in the main String.
public class SearchReplaceSubString {
/**
* @param args
*/
public static void main(String[] args) {
String str = "Using Replace method for the searching and searching and replacing of a substring in a String";
System.out.println("The original String is: " + str);
// We want to look for the String "search" and replace it with "find"
String searchWord = "search";
String replaceWord = "find";
// Replace all the occurrences of the searchWord with the replaceWord
str = str.replace(searchWord, replaceWord);
System.out.println("The modified String is: " + str);
}
}
When the example code is run, then the output is:-
The original String is: Using Replace method for the searching and searching and replacing of a substring in a String
The modified String is: Using Replace method for the finding and finding and replacing of a substring in a String
The modified String is: Using Replace method for the finding and finding and replacing of a substring in a String
Related Articles
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.