给定一个主字符串 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
需要找到只删除一个事件的方法
心有法竹
饮歌长啸
相关分类