public class ReplacePatternInString {

public static void main(String[] args) {
String pattern = "jitin";
StringBuilder stringToReplace = new StringBuilder("jitinJitinjitinJitin");
for (int i = 0; i < stringToReplace.length(); i++) { if ( i + pattern.length() + 1 <= stringToReplace.length() + 1 && stringToReplace.substring(i, i + pattern.length()).equals(pattern)) { stringToReplace.replace(i, i + pattern.length(), ""); // incrementing i according to length of pattern i = i + pattern.length() - 1; } } System.out.println("Final String is : " + stringToReplace); } }

Replace pattern in a string using JAVA.
Tagged on:                 

Leave a Reply

Your email address will not be published. Required fields are marked *