String matching

From Rosetta Code
Revision as of 03:12, 15 October 2010 by Casebash (talk | contribs) (Created page with '{{task}} Given two strings, demonstrate the following 3 types of matchings: 1. Determining if the first string starts with second string 2. Determining if the first string cont…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
String matching
You are encouraged to solve this task according to the task description, using any language you may know.

Given two strings, demonstrate the following 3 types of matchings:

1. Determining if the first string starts with second string 2. Determining if the first string contains the second string at any location 3. Determining if the first string ends with the second string

Optional requirements: A) Print the location of the match for part 2 B) Handle multiple occurrences of a string for part 2.

Python

<lang python> "abcd".startswith("ab")#returns true "abcd".endswith("zn")#returns false location="abab".find("ab")#returns 0 location="abab".find("ab",2)#returns 2 </lang>