Determining If a String Contains a Substring
 
This article deals with the determination, if a String contains another String...

String string = "Madam, I am Adam";
boolean b;

Starts with a specific Substring:
b = string.startsWith("Mad");
(returns true...)

Ends with a specific Substring:
b = string.endsWith("dam");
(returns true...)

Substring is somewhere inside the original String:
b = string.indexOf("I am") > 0;
(returns true...)

// To ignore case, regular expressions must be used:

// Starts with
b = string.matches("(?i)mad.*");

// Ends with
b = string.matches("(?i).*adam");

// Anywhere
b = string.matches("(?i).*i am.*");


Source: http://javaalmanac.com/egs/java.lang/HasSubstr.html
 
admin / Dec 07, 2005
   
 
 
Login: 
Pass: 
 
 
     
     
2004 - 2019 / lookass.ch
makememad@lookass.ch