We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › check if string contains another string
Page Index Toggle Pages: 1
check if string contains another string (Read 973 times)
check if string contains another string
Feb 21st, 2010, 4:50pm
 
i thought there was a method that let me check if a string contains a shorter string. lets say if "www.processing.org/discourse" contains the word processing... i checked the java reference http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html but couldnt find it.
Re: check if string contains another string
Reply #1 - Feb 21st, 2010, 5:31pm
 
if( string1.indexOf(string2) != -1 ){ /// ???
Re: check if string contains another string
Reply #2 - Feb 21st, 2010, 6:00pm
 
ah good, i get the idea.
if there is a more common way i would prefere it as i am working on a tutorial atm, so i dont want to make it more complicated as needed.
Re: check if string contains another string
Reply #3 - Feb 21st, 2010, 10:04pm
 
It is the most common way.
It might be worth mentioning also the more friendly .startsWith() and .endsWith(), the latter being perfect to check file extensions...

Ah, I had a vague recall... Yes! In Java 1.5 (you should check the latest versions of Java API...), Sun introduced the contains method, symmetrical to starts/endsWith...
Re: check if string contains another string
Reply #4 - Feb 21st, 2010, 11:02pm
 
great, this is what i was looking for. like you, i actually knew there was something called "contains" somewhere i just couldnt find it anymore.
Thanks for helping out.
Page Index Toggle Pages: 1