我想通过让用户键入Lisence车牌号码来删除整个输入,这将是一个唯一的数字。
字符串将打印出类似于"Car Ford F1 LC4PR0 Black“的内容。
有没有办法只找到"LC4PR0“并删除整个字符串?
public static void Add_Vehicle(ArrayList<String> list){
int listsize = list.size();
if(listsize == 50){
System.out.println("Vehicle Garage Full! 50/50 Vehicles");
}
else{
Scanner input = new Scanner(System.in);
System.out.println("Enter Vehicle Type. Car/Motorbike");
String VehicleType = input.nextLine().toUpperCase();
if(VehicleType.equals("CAR")){
String VehicleCar = "Car";
System.out.println("Enter Vehicle Make");
String CarMake = input.nextLine();
System.out.println("Enter Vehicle Model");
String CarModel = input.nextLine();
System.out.println("Enter Vehicle Lisence Plate.");
String CarPlate = input.nextLine();
System.out.println("Enter Vehicle Colour");
String CarColour = input.nextLine();
String CarDetails = VehicleCar + " " + CarMake + " " + CarModel + " " + CarPlate + " " + CarColour;
list.add(CarDetails);
}
else if(VehicleType.equals("MOTORBIKE")){
String VehicleMotorbike = "Car";
System.out.println("Enter Vehicle Make");
String MotorbikeMake = input.nextLine();
System.out.println("Enter Vehicle Model");
String MotorbikeModel = input.nextLine();
System.out.println("Enter Vehicle Lisence Plate.");
String MotorbikePlate = input.nextLine();
System.out.println("Enter Vehicle Colour");
String MotorbikeColour = input.nextLine();
String MotorbikeDetails = VehicleMotorbike + " " + MotorbikeMake + " " + MotorbikeModel + " " + MotorbikePlate + " " + MotorbikeColour;
list.add(MotorbikeDetails);
}
else{
System.out.println("Please Only Enter Vehicle Type, Car or Motorbike!");
}
Menu(list);
}
}```发布于 2019-12-02 10:32:50
如果您知道特定的车牌模式,您可以使用正则表达式来查找所有的出现情况。
发布于 2019-12-02 10:33:04
使用String#split(String)。
将切片字符串拆分为带参数的字符串数组。
例如,"test test2 test3".split(" ")返回{"test","test2","test3"}。
请参阅下面的代码。
Java 7
public String search(String target){
for(String n : list){
String[] split = n.split(" ");
if(split[3].equals(target)){
return split[3]; // Return n if you want to return all string
}
// Cannot find target
return null;
}Java 8
public String search(String str){
List<String> lst = list.filter(s -> s.split(" ")[3].equals(str)).collect(Collectors.toList());
return lst.size() == 0 ? null : lst.get(0).split(" ")[3]; // Do not split if you want to return all string
}https://stackoverflow.com/questions/59131859
复制相似问题