Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 寻找从主字符串中删除单个子字符串的方法_Java_String - Fatal编程技术网

Java 寻找从主字符串中删除单个子字符串的方法

Java 寻找从主字符串中删除单个子字符串的方法,java,string,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 = n

给定一个主字符串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

需要找到只删除一个匹配项的方法

使用字符串库中的
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

    }

}

如果我正确理解了您的问题,您想要的行为应该可以通过使用:


嗯,你可以使用它,它非常简单

replaceFirst(String regex,String replacement);

replaceFirst(String regex,String replacement);