String matching: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
No edit summary
Line 2,189: Line 2,189:
First string contains second string : at index 1 and at index 8
First string contains second string : at index 1 and at index 8
First string ends with second string : true
First string ends with second string : true
</pre>

=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">window 1, @"String matching", (0,0,650,360)

void local fn DoIt
CFStringRef s1, s2
CFRange range
s1 = @"alphabravocharlie"
s2 = @"alpha"
if ( fn StringHasPrefix( s1, s2 ) )
print @"\"";s1;@"\" starts with \"";s2;@"\""
else
print @"\"";s1;@"\" does not start with \"";s2;@"\""
end if
print
s2 = @"bravo"
if ( fn StringHasPrefix( s1, s2 ) )
print @"\"";s1;@"\" starts with \"";s2;@"\""
else
print @"\"";s1;@"\" does not start with \"";s2;@"\""
end if
print
range = fn StringRangeOfString( s1, s2 )
if ( range.location != NSNotFound )
print @"\"";s1;@"\" contains \"";s2;@"\" at location ";(range.location)
else
print @"\"";s1;@"\" does not contain \"";s2;@"\""
end if
print
s2 = @"delta"
range = fn StringRangeOfString( s1, s2 )
if ( range.location != NSNotFound )
print @"\"";s1;@"\" contains \"";s2;@"\" at location ";(range.location)
else
print @"\"";s1;@"\" does not contain \"";s2;@"\""
end if
print
s2 = @"charlie"
if ( fn StringHasSuffix( s1, s2 ) )
print @"\"";s1;@"\" ends with \"";s2;@"\""
else
print @"\"";s1;@"\" does not end with \"";s2;@"\""
end if
print
s2 = @"alpha"
if ( fn StringHasSuffix( s1, s2 ) )
print @"\"";s1;@"\" ends with \"";s2;@"\""
else
print @"\"";s1;@"\" does not end with \"";s2;@"\""
end if
print
s1 = @"alpha delta charlie delta echo delta futurebasic"
s2 = @"delta"
range = fn StringRangeOfString( s1, s2 )
while ( range.location != NSNotFound )
print @"\"";s1;@"\" contains \"";s2;@"\" at location ";(range.location)
range.location++
range = fn StringRangeOfStringWithOptionsInRange( s1, s2, 0, fn CFRangeMake( range.location, len(s1)-range.location ) )
wend
end fn

fn DoIt

HandleEvents</syntaxhighlight>

{{out}}
<pre>
"alphabravocharlie" starts with "alpha"

"alphabravocharlie" does not start with "bravo"

"alphabravocharlie" contains "bravo" at location 5

"alphabravocharlie" does not contain "delta"

"alphabravocharlie" ends with "charlie"

"alphabravocharlie" does not end with "alpha"

"alpha delta charlie delta echo delta futurebasic" contains "delta" at location 6
"alpha delta charlie delta echo delta futurebasic" contains "delta" at location 20
"alpha delta charlie delta echo delta futurebasic" contains "delta" at location 31
</pre>
</pre>