• SEARCH BOX

Combine Jetty and Spring Application Context

Spring's Logo The goal of this post is to use one xml configuration file which is Spring's applicationContext.xml and load it only once for the duration of the application running in an embedded jetty server. Read more...

Tutorial on Android Layout

A thumbnail of adroid. Android is an open source platform for mobile devices. Not a hardware. It is a software stack as defined by Google. Encase your not familiar with the term, software stack is compose of an Operating System(OS), middle-ware and the key applications. Read more...

Hi, my name's Paul Labis.

I'm a developer in Makati City, Philippines. Focused on building online Java/J2EE based applications. I'm interested in freelance or part-time work from home.

Thursday, April 15, 2010

Check If a String Contains a Substring in Java

This article shows some useful ways on checking a string that contains a substring. I find this topic relevant to document as I may someday use this on another project. Also, other developers might look for an article online to help them solve their problem. Since this topic is very basic Java, I only added short description and sample code below. 

//Create a string to evaluate
String phrase = "The big brown fox jumped over the lazy dog!";

//Check if it contains a case sensitive substring
boolean evaluation = phrase.contains("brown"); //returns true
evaluation = phrase.contains("Brown"); //returns false
evaluation = phrase.indexOf("fox jumped") > 0; //returns true

//Check if phrase starts with a substring
evaluation = phrase.startsWith("Th"); //returns true

//Check if phrase ends with a substring
evaluation = phrase.endsWith("og"); //returns true

//To check string with ignore case, we should be using regular expression
evaluation = string.matches("(?i).*i am.*"); //returns true

//Check if phrase starts with a substring
evaluation = string.matches("(?i)th.*"); //returns true

//Check if phrase ends with a substring
evaluation = string.matches("(?i).*adam"); //returns true

That is all for this article. I hope this helps you.

0 comments :

Post a Comment

 

Author

Followers

Techie Projects

Open Source