Home > Mobile >  Replacing a substring of a string using java
Replacing a substring of a string using java

Time:12-22

Given three strings S, S1, and S2 consisting of N, M, and K characters respectively, the task is to modify the string S by replacing all the substrings S1 with the string S2 in the string S.

Example: Input: S = “abababa”, S1 = “aba”, S2 = “a” Output: aba

Below is the implementation of the above approach given to us:

import java.util.Scanner;

public class stringint {
    public static void substring(String s, String s1,String s2) {
        String ans = "";
        for (int i = 0; i < s.length(); i  ) {
            int k = 0;
            if (s.charAt(i) == s1.charAt(k) && i s1.length() <= s.length()) {
                int j;
                for (j = i; j < i s1.length(); j  ) {
                    if (s.charAt(j) != s1.charAt(k)) {
                        break;
                    }
                    else {
                        k = k   1;
                    }
                }
                if (j == i   s1.length()) {
                    ans  = (s2);
                    i = j - 1;
                }
                else {
                    ans  = (s.charAt(i));
                }
            }
            else {
                ans  = (s.charAt(i));
            }
        }
        System.out.println(ans);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the string s:");
        String s = sc.next();
        System.out.println("Enter the string s1:");
        String s1 = sc.next();
        System.out.println("Enter the string s2:");
        String s2 = sc.next();
        substring(s, s1, s2);
    }
}

As a result, the preceding code replaces the substring from the string. But I don't understand how the code works or how it performs the desired task of replacing the substring.

CodePudding user response:

why you are not using replaceAll method here? Is there any specific reason for this?

import java.util.Scanner;

public class stringint {

    public static void substring(String s, String s1,String s2){
     
    s = s.replaceAll("(?i)" s1,s2);

        System.out.println(s);
    
    }
        public static void main(String[] args) {
            Scanner sc= new Scanner(System.in);
            System.out.println("Enter the string s:");
            String s=sc.next();
            System.out.println("Enter the string s1:");
            String s1= sc.next();
            System.out.println("Enter the string s2:");
            String s2=sc.next();
            substring(s,s1,s2);


    }
}

Above mentioned code will be doing the same thing using repaceAll method.

CodePudding user response:

You could refer to below link to find multiple different util approaches to solve this.

Ref : https://www.baeldung.com/java-remove-replace-string-part

I would say using replaceAll() with regex specified would be the better solution.

s.replaceAll(regexTarget, replacement);

You can also make it work by ignoring case sensitivity.

  • Related