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.

See also  How College Students Can Shape the Future of Tech Responsibility
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist