查找从主字符串中删除单个出现的子字符串的方法

时间:2018-09-01 15:11:16

标签: java string

给出一个主字符串A和3个子字符串D,E,F。需要找到子字符串D,E,F并将其删除。如果从给定的字符串中删除了D或E或F,它将形成新的字符串A1。在A1上重复此过程以获得A2,依此类推,直到无法进行该过程为止。我的方法

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 

       }
}  

但是问题在于,如果使用此代码,它会找到具有多个频率的子字符串,则它会删除所有频率,但是我们只需要删除一个频率。例如:-

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

需要找到一种方法来仅删除一次事件

3 个答案:

答案 0 :(得分:1)

在字符串库中使用replaceFirst方法。 代码如下所示;

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

    }

}

答案 1 :(得分:1)

如果我正确理解了您的问题,则可以通过使用String#replaceFirst来实现所需的行为:

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

kbblb

Demo

答案 2 :(得分:0)

非常简单,您可以使用

replaceFirst(String regex,String replacement);

click here for reference