Shivam Sharma
Shivam Sharma

Reputation: 527

Finding way to remove single occurrence of sub string from the main string

Given a main String A and 3 substrings D,E,F.Need to find substrings D,E,F and remove them.If D or E or F is removed from given String it form new string A1. This process is repeated on A1 to get A2 and so on till process is not possible. My approach

import java.util.Scanner;
public class Try {
       public static void main(String[] args) {
            String c;
            Scanner scanner = new Scanner(System.in);
            System.out.println("Enter the string main string1:\n"); //Main String 
            String a = scanner.nextLine();
            System.out.println("Enter the substring 1:\n");
            String b = scanner.nextLine();
            String strNew = a.replace(b, ""); // strNew is after removing substring b
            System.out.println("New string1 "+strNew);
            System.out.println("Enter the substring 2:\n");
            String b1 = scanner.nextLine();
            String strNew1 = strNew.replace(b1, "");//strNew1 is after removing substring b1
            System.out.println("New string2 "+strNew1);
            System.out.println("Enter the substring 3:\n");
            String b2 = scanner.nextLine();
            String strNew2 = strNew1.replace(b2, "");//strNew is after removing substring b2
            System.out.println("New string1 "+strNew2);
            System.out.println("Lenght of substring is"+strNew2.length()); //Final length of substring 

       }
}  

But problem is that with this code if it find substring which have multiple frequency it removes all the frequency but we need to remove only one .For eg:-

Main String-bkllkbblb
Substring 1-kl //Gives blkbblb
Substring 2-bl// Remove bl from blkbblb should give-**kbblb** but it gives **kbb**
Substring 1-b

Need to find way to remove only one occurrence

Upvotes: 1

Views: 191

Answers (3)

RudRocks05
RudRocks05

Reputation: 13

well its quite simple you can use

replaceFirst(String regex,String replacement);

click here for reference

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520878

If I understand your question correctly, the behavior you want should be possible by using String#replaceFirst:

String input = "bkllkbblb";
input = input.replaceFirst("kl", "");
input = input.replaceFirst("bl", "");
System.out.println(input);

kbblb

Demo

Upvotes: 1

drowny
drowny

Reputation: 2147

Use replaceFirst method in String library. Code will be like below ;

import java.util.Scanner;

public class Try {
    public static void main(String[] args) {

        String c;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the string main string1:\n"); //Main String
        String a = scanner.nextLine();
        System.out.println("Enter the substring 1:\n");
        String b = scanner.nextLine();
        String strNew = a.replaceFirst(b, ""); // strNew is after removing substring b
        System.out.println("New string1 "+strNew);
        System.out.println("Enter the substring 2:\n");
        String b1 = scanner.nextLine();
        String strNew1 = strNew.replaceFirst(b1, "");//strNew1 is after removing substring b1
        System.out.println("New string2 "+strNew1);
        System.out.println("Enter the substring 3:\n");
        String b2 = scanner.nextLine();
        String strNew2 = strNew1.replaceFirst(b2, "");//strNew is after removing substring b2
        System.out.println("New string1 "+strNew2);
        System.out.println("Lenght of substring is"+strNew2.length()); //Final length of substring

    }

}

Upvotes: 1

Related Questions