String matching: Difference between revisions
Content added Content deleted
m (→{{header|J}}: Icon and Unicon) |
(Go solution) |
||
Line 320: | Line 320: | ||
'qwertyuiop' contains 1 instances of 'wert' |
'qwertyuiop' contains 1 instances of 'wert' |
||
'qwertyuiop' does not end with 'random garbage' |
'qwertyuiop' does not end with 'random garbage' |
||
=={{header|Go}}= |
|||
<lang go>package main |
|||
import ( |
|||
"fmt" |
|||
"strings" |
|||
) |
|||
func match(first, second string) { |
|||
fmt.Printf("1. %s starts with %s: %t\n", |
|||
first, second, strings.HasPrefix(first, second)) |
|||
i := strings.Index(first, second) |
|||
fmt.Printf("2. %s contains %s: %t,\n", first, second, i >= 0) |
|||
if i >= 0 { |
|||
fmt.Printf("2.1. at location %d,\n", i) |
|||
for start := i+1;; { |
|||
if i = strings.Index(first[start:], second); i < 0 { |
|||
break |
|||
} |
|||
fmt.Printf("2.2. at location %d,\n", start+i) |
|||
start += i+1 |
|||
} |
|||
fmt.Println("2.2. and that's all") |
|||
} |
|||
fmt.Printf("3. %s ends with %s: %t\n", |
|||
first, second, strings.HasSuffix(first, second)) |
|||
} |
|||
func main() { |
|||
match("abracadabra", "abr") |
|||
}</lang> |
|||
Output: |
|||
<pre> |
|||
1. abracadabra starts with abr: true |
|||
2. abracadabra contains abr: true, |
|||
2.1. at location 0, |
|||
2.2. at location 7, |
|||
2.2. and that's all |
|||
3. abracadabra ends with abr: false |
|||
</pre> |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |