devxlogo

String Search Mechanism

String Search Mechanism

Question:
How do I do a string comparison where I want to determine if onestring is contained within another? For example:

Str1 = "Degree in IT";Str2 = "Diploma or Degree in IT";

I want to see if Str1 is contined in Str2.

Answer:
The String class supports this type of comparison through itsindexOf(String) method. The method will return the start index of thefirst occurrence of the substring or -1 if the substring does notoccur within the String object.

More general pattern matching can beperformed with a third-party regular expression package.

devx-admin

Share the Post: