For a given string
String name = "Test";
String welcomeMessage = "Welcome" + name + ", You have notification!";
How can we check if welcomeMessage contains "Welcome, you have notification" substring by escaping name variable as name variable keeps on changing?
I want to achieve
welcomeMessage.contains("Welcome, You have notification!"); //to return true
What would be the best way to skip name variable?
String#startsWith & endsWithThe String class provides specific methods:
startsWithendsWithExample:
boolean containsPhrases =
message.startsWith( "Welcome" )
&&
message.endsWith( ", You have notification!" )
;
very simple
String name = "Test";
String welcomeMessage = "Welcome" + name + ", You have notification!";
System.out.println(welcomeMessage + " matches " + welcomeMessage.matches("Welcome.+You have notification!"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With