public static void main(String[] args) throws ParseException { //User name String name = ""; //Character conversion and replacement String message = "***Hello, your order has been paid successfully, we will reply you as soon as possible Shipping."; //Replace *** String replace = null; try { replace = message.replace("***", name); } catch (Exception e) { e.printStackTrace(); } System.out .println(replace); }
When the string is an empty string, no error will be reported
Console output:
public static void main(String[] args) throws ParseException { //User name String name = null; //Character conversion and replacement String message = "***Hello, your order has been paid successfully, we will send you a message as soon as possible Goods."; //Replace *** String replace = null; try { replace = message.replace("***", name); } catch (Exception e) { e.printStackTrace(); } System.out. println(replace); }
If it is null, an error will be reported, see the requirement to add a non-null judgment