Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -1,20 +0,0 @@
|
|||
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
|
||||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
procedure Match_Strings is
|
||||
S1 : constant String := "abcd";
|
||||
S2 : constant String := "abab";
|
||||
S3 : constant String := "ab";
|
||||
begin
|
||||
if S1'Length >= S3'Length and then S1 (S1'First..S1'First + S3'Length - 1) = S3 then
|
||||
Put_Line (''' & S1 & "' starts with '" & S3 & ''');
|
||||
end if;
|
||||
if S2'Length >= S3'Length and then S2 (S2'Last - S3'Length + 1..S2'Last) = S3 then
|
||||
Put_Line (''' & S2 & "' ends with '" & S3 & ''');
|
||||
end if;
|
||||
Put_Line (''' & S3 & "' first appears in '" & S1 & "' at" & Integer'Image (Index (S1, S3)));
|
||||
Put_Line
|
||||
( ''' & S3 & "' appears in '" & S2 & ''' &
|
||||
Integer'Image (Ada.Strings.Fixed.Count (S2, S3)) & " times"
|
||||
);
|
||||
end Match_Strings;
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
$string1 = "arduinoardblobard"
|
||||
$string2 = "ard"
|
||||
|
||||
; == Determining if the first string starts with second string
|
||||
If StringLeft($string1, StringLen($string2)) = $string2 Then
|
||||
ConsoleWrite("1st string starts with 2nd string." & @CRLF)
|
||||
Else
|
||||
ConsoleWrite("1st string does'nt starts with 2nd string." & @CRLF)
|
||||
EndIf
|
||||
|
||||
; == Determining if the first string contains the second string at any location
|
||||
; == Print the location of the match for part 2
|
||||
; == Handle multiple occurrences of a string for part 2
|
||||
$start = 1
|
||||
$count = 0
|
||||
$pos = StringInStr($string1, $string2)
|
||||
While $pos
|
||||
$count += 1
|
||||
ConsoleWrite("1st string contains 2nd string at position: " & $pos & @CRLF)
|
||||
$pos = StringInStr($string1, $string2, 0, 1, $start + $pos + StringLen($string2))
|
||||
WEnd
|
||||
If $count = 0 Then ConsoleWrite("1st string does'nt contain 2nd string." & @CRLF)
|
||||
|
||||
; == Determining if the first string ends with the second string
|
||||
If StringRight($string1, StringLen($string2)) = $string2 Then
|
||||
ConsoleWrite("1st string ends with 2nd string." & @CRLF)
|
||||
Else
|
||||
ConsoleWrite("1st string does'nt ends with 2nd string." & @CRLF)
|
||||
EndIf
|
||||
|
|
@ -1,70 +0,0 @@
|
|||
::NOTE #1: This implementation might crash, or might not work properly if
|
||||
::you put some of the CMD special characters (ex. %,!, etc) inside the strings.
|
||||
::
|
||||
::NOTE #2: The comparisons here are case-SENSITIVE.
|
||||
::NOTE #3: Spaces in strings are considered.
|
||||
|
||||
@echo off
|
||||
setlocal enabledelayedexpansion
|
||||
|
||||
::The main things...
|
||||
set "str1=qwertyuiop"
|
||||
set "str2=qwerty"
|
||||
call :str2_lngth
|
||||
call :matchbegin
|
||||
|
||||
set "str1=qweiuoiocghiioyiocxiisfguiioiuygvd"
|
||||
set "str2=io"
|
||||
call :str2_lngth
|
||||
call :matchcontain
|
||||
|
||||
set "str1=blablabla"
|
||||
set "str2=bbla"
|
||||
call :str2_lngth
|
||||
call :matchend
|
||||
|
||||
echo.
|
||||
pause
|
||||
exit /b 0
|
||||
::/The main things.
|
||||
|
||||
::The functions...
|
||||
:matchbegin
|
||||
echo.
|
||||
if "!str1:~0,%length%!"=="!str2!" (
|
||||
echo "%str1%" begins with "%str2%".
|
||||
) else (
|
||||
echo "%str1%" does not begin with "%str2%".
|
||||
)
|
||||
goto :EOF
|
||||
|
||||
:matchcontain
|
||||
echo.
|
||||
set curr=0&set exist=0
|
||||
:scanchrloop
|
||||
if "!str1:~%curr%,%length%!"=="" (
|
||||
if !exist!==0 echo "%str1%" does not contain "%str2%".
|
||||
goto :EOF
|
||||
)
|
||||
if "!str1:~%curr%,%length%!"=="!str2!" (
|
||||
echo "%str1%" contains "%str2%". ^(in Position %curr%^)
|
||||
set exist=1
|
||||
)
|
||||
set /a curr+=1&goto scanchrloop
|
||||
|
||||
:matchend
|
||||
echo.
|
||||
if "!str1:~-%length%!"=="!str2!" (
|
||||
echo "%str1%" ends with "%str2%".
|
||||
) else (
|
||||
echo "%str1%" does not end with "%str2%".
|
||||
)
|
||||
goto :EOF
|
||||
|
||||
:str2_lngth
|
||||
set length=0
|
||||
:loop
|
||||
if "!str2:~%length%,1!"=="" goto :EOF
|
||||
set /a length+=1
|
||||
goto loop
|
||||
::/The functions.
|
||||
|
|
@ -1,24 +1,24 @@
|
|||
import extensions;
|
||||
|
||||
public program()
|
||||
public Program()
|
||||
{
|
||||
var s := "abcd";
|
||||
|
||||
console.printLine(s," starts with ab: ",s.startingWith("ab"));
|
||||
console.printLine(s," starts with cd: ",s.startingWith("cd"));
|
||||
Console.printLine(s," starts with ab: ",s.startingWith("ab"));
|
||||
Console.printLine(s," starts with cd: ",s.startingWith("cd"));
|
||||
|
||||
console.printLine(s," ends with ab: ",s.endingWith("ab"));
|
||||
console.printLine(s," ends with cd: ",s.endingWith("cd"));
|
||||
Console.printLine(s," ends with ab: ",s.endingWith("ab"));
|
||||
Console.printLine(s," ends with cd: ",s.endingWith("cd"));
|
||||
|
||||
console.printLine(s," contains ab: ",s.containing("ab"));
|
||||
console.printLine(s," contains bc: ",s.containing("bc"));
|
||||
console.printLine(s," contains cd: ",s.containing("cd"));
|
||||
console.printLine(s," contains az: ",s.containing("az"));
|
||||
Console.printLine(s," contains ab: ",s.containing("ab"));
|
||||
Console.printLine(s," contains bc: ",s.containing("bc"));
|
||||
Console.printLine(s," contains cd: ",s.containing("cd"));
|
||||
Console.printLine(s," contains az: ",s.containing("az"));
|
||||
|
||||
console.printLine(s," index of az: ",s.indexOf(0, "az"));
|
||||
console.printLine(s," index of cd: ",s.indexOf(0, "cd"));
|
||||
console.printLine(s," index of bc: ",s.indexOf(0, "bc"));
|
||||
console.printLine(s," index of ab: ",s.indexOf(0, "ab"));
|
||||
Console.printLine(s," index of az: ",s.indexOf(0, "az"));
|
||||
Console.printLine(s," index of cd: ",s.indexOf(0, "cd"));
|
||||
Console.printLine(s," index of bc: ",s.indexOf(0, "bc"));
|
||||
Console.printLine(s," index of ab: ",s.indexOf(0, "ab"));
|
||||
|
||||
console.readChar()
|
||||
Console.readChar()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +0,0 @@
|
|||
(defun string-contains (needle haystack)
|
||||
(string-match (regexp-quote needle) haystack))
|
||||
|
||||
(string-prefix-p "before" "before center after") ;=> t
|
||||
(string-contains "before" "before center after") ;=> 0
|
||||
(string-suffix-p "before" "before center after") ;=> nil
|
||||
|
||||
(string-prefix-p "center" "before center after") ;=> nil
|
||||
(string-contains "center" "before center after") ;=> 7
|
||||
(string-suffix-p "center" "before center after") ;=> nil
|
||||
|
||||
(string-prefix-p "after" "before center after") ;=> nil
|
||||
(string-contains "after" "before center after") ;=> 14
|
||||
(string-suffix-p "after" "before center after") ;=> t
|
||||
|
|
@ -1,30 +0,0 @@
|
|||
sequence first, second
|
||||
integer x
|
||||
|
||||
first = "qwertyuiop"
|
||||
|
||||
-- Determining if the first string starts with second string
|
||||
second = "qwerty"
|
||||
if match(second, first) = 1 then
|
||||
printf(1, "'%s' starts with '%s'\n", {first, second})
|
||||
else
|
||||
printf(1, "'%s' does not start with '%s'\n", {first, second})
|
||||
end if
|
||||
|
||||
-- Determining if the first string contains the second string at any location
|
||||
-- Print the location of the match for part 2
|
||||
second = "wert"
|
||||
x = match(second, first)
|
||||
if x then
|
||||
printf(1, "'%s' contains '%s' at position %d\n", {first, second, x})
|
||||
else
|
||||
printf(1, "'%s' does not contain '%s'\n", {first, second})
|
||||
end if
|
||||
|
||||
-- Determining if the first string ends with the second string
|
||||
second = "uio"
|
||||
if length(second)<=length(first) and match_from(second, first, length(first)-length(second)+1) then
|
||||
printf(1, "'%s' ends with '%s'\n", {first, second})
|
||||
else
|
||||
printf(1, "'%s' does not end with '%s'\n", {first, second})
|
||||
end if
|
||||
|
|
@ -1 +0,0 @@
|
|||
"cheesecake" "cheese" head? ! t
|
||||
|
|
@ -1 +0,0 @@
|
|||
"sec" "cheesecake" subseq? ! t
|
||||
|
|
@ -1 +0,0 @@
|
|||
"cheesecake" "cake" tail? ! t
|
||||
|
|
@ -1 +0,0 @@
|
|||
"sec" "cheesecake" subseq-start ! 4
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
USE: regexp
|
||||
"Mississippi" "iss" <regexp> all-matching-slices [ from>> ] map ! { 1 4 }
|
||||
|
|
@ -1,2 +1,22 @@
|
|||
// For this task consider the following strings
|
||||
String string = "string matching";
|
||||
String suffix = "ing";
|
||||
// The most idiomatic way of determining if a string starts with another is the String.startsWith method.
|
||||
string.startsWith(suffix)
|
||||
// Another way is to use a combination of String.substring and String.equals
|
||||
string.substring(0, suffix.length()).equals(suffix)
|
||||
// To determine if a string contains at least one occurrence of another string, use the String.contains method.
|
||||
string.contains(suffix)
|
||||
// A slightly more idiomatic approach is String.indexOf, which also returns the index of the first character.
|
||||
string.indexOf(suffix) != -1
|
||||
// The most idiomatic way of determining whether a string ends with another is the String.endsWith method.
|
||||
string.endsWith(suffix);
|
||||
//Similarly, a combination of String.substring and String.equals can be used.
|
||||
string.substring(string.length() - suffix.length()).equals(suffix)
|
||||
// If you're looking to find the index of each occurrence, you can use the following.
|
||||
int indexOf;
|
||||
int offset = 0;
|
||||
while ((indexOf = string.indexOf(suffix, offset)) != -1) {
|
||||
System.out.printf("'%s' @ %d to %d%n", suffix, indexOf, indexOf + suffix.length() - 1);
|
||||
offset = indexOf + 1;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,32 +0,0 @@
|
|||
public class JavaApplication6 {
|
||||
public static void main(String[] args) {
|
||||
String strOne = "complexity";
|
||||
String strTwo = "udacity";
|
||||
stringMatch(strOne, strTwo);
|
||||
}
|
||||
|
||||
public static void stringMatch(String one, String two) {
|
||||
boolean match = false;
|
||||
if (one.charAt(0) == two.charAt(0)) {
|
||||
System.out.println(match = true); // returns true
|
||||
} else {
|
||||
System.out.println(match); // returns false
|
||||
}
|
||||
for (int i = 0; i < two.length(); i++) {
|
||||
int temp = i;
|
||||
for (int x = 0; x < one.length(); x++) {
|
||||
if (two.charAt(temp) == one.charAt(x)) {
|
||||
System.out.println(match = true); //returns true
|
||||
i = two.length();
|
||||
}
|
||||
}
|
||||
}
|
||||
int num1 = one.length() - 1;
|
||||
int num2 = two.length() - 1;
|
||||
if (one.charAt(num1) == two.charAt(num2)) {
|
||||
System.out.println(match = true);
|
||||
} else {
|
||||
System.out.println(match = false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1 +1,40 @@
|
|||
string.startsWith(suffix)
|
||||
"abcd".startsWith("ab") //returns true
|
||||
"abcd".endsWith("zn") //returns false
|
||||
"abab".contains("bb") //returns false
|
||||
"abab".contains("ab") //returns true
|
||||
int loc = "abab".indexOf("bb") //returns -1
|
||||
loc = "abab".indexOf("ab") //returns 0
|
||||
loc = "abab".indexOf("ab",loc+1) //returns 2
|
||||
|
||||
public class JavaApplication6 {
|
||||
public static void main(String[] args) {
|
||||
String strOne = "complexity";
|
||||
String strTwo = "udacity";
|
||||
stringMatch(strOne, strTwo);
|
||||
}
|
||||
|
||||
public static void stringMatch(String one, String two) {
|
||||
boolean match = false;
|
||||
if (one.charAt(0) == two.charAt(0)) {
|
||||
System.out.println(match = true); // returns true
|
||||
} else {
|
||||
System.out.println(match); // returns false
|
||||
}
|
||||
for (int i = 0; i < two.length(); i++) {
|
||||
int temp = i;
|
||||
for (int x = 0; x < one.length(); x++) {
|
||||
if (two.charAt(temp) == one.charAt(x)) {
|
||||
System.out.println(match = true); //returns true
|
||||
i = two.length();
|
||||
}
|
||||
}
|
||||
}
|
||||
int num1 = one.length() - 1;
|
||||
int num2 = two.length() - 1;
|
||||
if (one.charAt(num1) == two.charAt(num2)) {
|
||||
System.out.println(match = true);
|
||||
} else {
|
||||
System.out.println(match = false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
string.substring(0, suffix.length()).equals(suffix)
|
||||
|
|
@ -1 +0,0 @@
|
|||
string.contains(suffix)
|
||||
|
|
@ -1 +0,0 @@
|
|||
string.indexOf(suffix) != -1
|
||||
|
|
@ -1 +0,0 @@
|
|||
string.endsWith(suffix);
|
||||
|
|
@ -1 +0,0 @@
|
|||
string.substring(string.length() - suffix.length()).equals(suffix)
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
int indexOf;
|
||||
int offset = 0;
|
||||
while ((indexOf = string.indexOf(suffix, offset)) != -1) {
|
||||
System.out.printf("'%s' @ %d to %d%n", suffix, indexOf, indexOf + suffix.length() - 1);
|
||||
offset = indexOf + 1;
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
"abcd".startsWith("ab") //returns true
|
||||
"abcd".endsWith("zn") //returns false
|
||||
"abab".contains("bb") //returns false
|
||||
"abab".contains("ab") //returns true
|
||||
int loc = "abab".indexOf("bb") //returns -1
|
||||
loc = "abab".indexOf("ab") //returns 0
|
||||
loc = "abab".indexOf("ab",loc+1) //returns 2
|
||||
|
|
@ -1,3 +1,23 @@
|
|||
# startswith/1 is boolean:
|
||||
"abc" | startswith("ab")
|
||||
#=> true
|
||||
|
||||
# index/1 returns the index or null,
|
||||
# so the jq test "if index(_) then ...." can be used
|
||||
# without any type conversion.
|
||||
|
||||
"abcd" | index( "bc")
|
||||
#=> 1
|
||||
|
||||
# endswith/1 is also boolean:
|
||||
|
||||
"abc" | endswith("bc")
|
||||
#=> true
|
||||
|
||||
# Using the regex functions available in jq 1.5:
|
||||
|
||||
"abc" | test( "^ab")
|
||||
|
||||
"abcd" | test("bc")
|
||||
|
||||
"abcd" | test("cd$")
|
||||
|
|
|
|||
|
|
@ -1,6 +1,11 @@
|
|||
# index/1 returns the index or null,
|
||||
# so the jq test "if index(_) then ...." can be used
|
||||
# without any type conversion.
|
||||
# In jq 1.4 or later:
|
||||
jq -n '"abcdabcd" | indices("bc")'
|
||||
[
|
||||
1,
|
||||
5
|
||||
]
|
||||
|
||||
"abcd" | index( "bc")
|
||||
#=> 1
|
||||
# In jq 1.5, the regex function match/1 can also be used:
|
||||
$ jq -n '"abcdabcd" | match("bc"; "g") | .offset'
|
||||
1
|
||||
5
|
||||
|
|
|
|||
|
|
@ -1,3 +0,0 @@
|
|||
# endswith/1 is also boolean:
|
||||
"abc" | endswith("bc")
|
||||
#=> true
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
"abc" | test( "^ab")
|
||||
|
||||
"abcd" | test("bc")
|
||||
|
||||
"abcd" | test("cd$")
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
# In jq 1.4 or later:
|
||||
jq -n '"abcdabcd" | indices("bc")'
|
||||
[
|
||||
1,
|
||||
5
|
||||
]
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
$ jq -n '"abcdabcd" | match("bc"; "g") | .offset'
|
||||
1
|
||||
5
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
Module StringMatch {
|
||||
A$="Hello World"
|
||||
Print A$ ~ "Hello*"
|
||||
Print A$ ~ "*llo*"
|
||||
p=Instr(A$, "llo")
|
||||
Print p=3
|
||||
\\ Handle multiple occurance for "o"
|
||||
p=Instr(A$, "o")
|
||||
While p > 0 {
|
||||
Print "position:";p;{ for "o"}
|
||||
p=Instr(A$, "o", p+1)
|
||||
}
|
||||
Print A$ ~ "*orld"
|
||||
}
|
||||
StringMatch
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
Module StringMatch {
|
||||
A="Hello World"
|
||||
Print A ~ "Hello*"
|
||||
Print A ~ "*llo*"
|
||||
p=Instr(A, "llo")
|
||||
Print p=3
|
||||
\\ Handle multiple occurance for "o"
|
||||
p=Instr(A, "o")
|
||||
While p > 0 {
|
||||
Print "position:";p;{ for "o"}
|
||||
p=Instr(A, "o", p+1)
|
||||
}
|
||||
Print A ~ "*orld"
|
||||
}
|
||||
StringMatch
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
let match1 s1 s2 =
|
||||
let len1 = String.length s1
|
||||
and len2 = String.length s2 in
|
||||
if len1 < len2 then false else
|
||||
let sub = String.sub s1 0 len2 in
|
||||
(sub = s2)
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
let match2 s1 s2 =
|
||||
let len1 = String.length s1
|
||||
and len2 = String.length s2 in
|
||||
if len1 < len2 then false else
|
||||
let rec aux i =
|
||||
if i < 0 then false else
|
||||
let sub = String.sub s1 i len2 in
|
||||
if (sub = s2) then true else aux (pred i)
|
||||
in
|
||||
aux (len1 - len2)
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
let match3 s1 s2 =
|
||||
let len1 = String.length s1
|
||||
and len2 = String.length s2 in
|
||||
if len1 < len2 then false else
|
||||
let sub = String.sub s1 (len1 - len2) len2 in
|
||||
(sub = s2)
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
let match2_loc s1 s2 =
|
||||
let len1 = String.length s1
|
||||
and len2 = String.length s2 in
|
||||
if len1 < len2 then (false, -1) else
|
||||
let rec aux i =
|
||||
if i < 0 then (false, -1) else
|
||||
let sub = String.sub s1 i len2 in
|
||||
if (sub = s2) then (true, i) else aux (pred i)
|
||||
in
|
||||
aux (len1 - len2)
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
let match2_num s1 s2 =
|
||||
let len1 = String.length s1
|
||||
and len2 = String.length s2 in
|
||||
if len1 < len2 then (false, 0) else
|
||||
let rec aux i n =
|
||||
if i < 0 then (n <> 0, n) else
|
||||
let sub = String.sub s1 i len2 in
|
||||
if (sub = s2)
|
||||
then aux (pred i) (succ n)
|
||||
else aux (pred i) (n)
|
||||
in
|
||||
aux (len1 - len2) 0
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
/* Let s be one string, t be the other that might exist within s. */
|
||||
/* 8-1-2011 */
|
||||
k = index(s, t);
|
||||
if k = 0 then put skip edit (t, ' is nowhere in sight') (a);
|
||||
else if k = 1 then
|
||||
put skip edit (t, ' starts at the beginning of ', s) (a);
|
||||
else if k+length(t)-1 = length(s) then
|
||||
put skip edit (t, ' is at the end of ', s) (a);
|
||||
else put skip edit (t, ' is within ', s) (a);
|
||||
|
||||
if k > 0 then put skip edit (t, ' starts at position ', k) (a);
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
/* Handle multiple occurrences. */
|
||||
n = 1;
|
||||
do forever;
|
||||
k = index(s, t, n);
|
||||
if k = 0 then
|
||||
do;
|
||||
if n = 1 then put skip list (t, ' is nowhere in sight');
|
||||
stop;
|
||||
end;
|
||||
else if k = 1 then
|
||||
put skip edit ('<', t, '> starts at the beginning of ', s) (a);
|
||||
else if k+length(t)-1 = length(s) then
|
||||
put skip edit ('<', t, '> is at the end of ', s) (a);
|
||||
else put skip edit ('<', t, '> is within ', s) (a);
|
||||
n = k + length(t);
|
||||
|
||||
if k > 0 then
|
||||
put skip edit ('<', t, '> starts at position ', trim(k)) (a);
|
||||
else stop;
|
||||
end;
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
$str1 =~ /^\Q$str2\E/ # true if $str1 starts with $str2
|
||||
$str1 =~ /\Q$str2\E/ # true if $str1 contains $str2
|
||||
$str1 =~ /\Q$str2\E$/ # true if $str1 ends with $str2
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
index($str1, $str2) == 0 # true if $str1 starts with $str2
|
||||
index($str1, $str2) != -1 # true if $str1 contains $str2
|
||||
rindex($str1, $str2) == length($str1) - length($str2) # true if $str1 ends with $str2
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
substr($str1, 0, length($str2)) eq $str2 # true if $str1 starts with $str2
|
||||
substr($str1, - length($str2)) eq $str2 # true if $str1 ends with $str2
|
||||
|
|
@ -1 +0,0 @@
|
|||
print $-[0], "\n" while $str1 =~ /\Q$str2\E/g; # using a regex
|
||||
|
|
@ -1 +0,0 @@
|
|||
my $i = -1; print $i, "\n" while ($i = index $str1, $str2, $i + 1) != -1; # using index
|
||||
|
|
@ -1,33 +1,22 @@
|
|||
-->
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">word</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"the"</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- (also try this with "th"/"he")</span>
|
||||
<span style="color: #000000;">sentence</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"the last thing the man said was the"</span>
|
||||
<span style="color: #000080;font-style:italic;">-- sentence = "thelastthingthemansaidwasthe" -- (practically the same results)
|
||||
with javascript_semantics
|
||||
constant sentence = "abracadabra", w1 = "abra", w2 = "bar",
|
||||
fmt = """
|
||||
For the string "%s":
|
||||
1: Starts with "%s": %t.
|
||||
Starts with "%s": %t.
|
||||
2: Contains "%s": %t%s.
|
||||
Contains "%s": %t%s.
|
||||
3: Ends with "%s": %t.
|
||||
Ends with "%s": %t.
|
||||
"""
|
||||
function jma(string word, sentence)
|
||||
sequence r = match_all(word,sentence)
|
||||
return iff(r={} ? "" : ", at "&join(r,", ", " and ","%d"))
|
||||
end function
|
||||
|
||||
-- A common, but potentially inefficient idiom for checking for a substring at the start is:</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #008000;">"yes(1)"</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000080;font-style:italic;">-- A more efficient method is to test the appropriate slice</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">)>=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">and</span> <span style="color: #000000;">sentence</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">)]=</span><span style="color: #000000;">word</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #008000;">"yes(2)"</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000080;font-style:italic;">-- Which is almost identical to checking for a word at the end</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">)>=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">and</span> <span style="color: #000000;">sentence</span><span style="color: #0000FF;">[-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">)..-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">word</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #008000;">"yes(3)"</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000080;font-style:italic;">-- Or sometimes you will see this, a tiny bit more efficient:</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">)>=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">and</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">)-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #008000;">"yes(4)"</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000080;font-style:italic;">-- Finding all occurences is a snap:</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">r</span>
|
||||
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #000080;font-style:italic;">-- or equivalently:</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">match_all</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">)</span>
|
||||
<!--
|
||||
printf(1,fmt,{sentence,w1,begins(w1,sentence),
|
||||
w2,begins(w2,sentence),
|
||||
w1,match(w1,sentence),jma(w1,sentence),
|
||||
w2,match(w2,sentence),jma(w2,sentence),
|
||||
w1,ends(w1,sentence),
|
||||
w2,ends(w2,sentence)})
|
||||
|
|
|
|||
|
|
@ -1,5 +0,0 @@
|
|||
"spicywiener".StartsWith("spicy")
|
||||
"spicywiener".Contains("icy")
|
||||
"spicywiener".EndsWith("wiener")
|
||||
"spicywiener".IndexOf("icy")
|
||||
[regex]::Matches("spicywiener", "i").count
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
Procedure StartsWith(String1$, String2$)
|
||||
Protected Result
|
||||
If FindString(String1$, String2$, 1) =1 ; E.g Found in possition 1
|
||||
Result =CountString(String1$, String2$)
|
||||
EndIf
|
||||
ProcedureReturn Result
|
||||
EndProcedure
|
||||
|
||||
Procedure EndsWith(String1$, String2$)
|
||||
Protected Result, dl=Len(String1$)-Len(String2$)
|
||||
If dl>=0 And Right(String1$, Len(String2$))=String2$
|
||||
Result =CountString(String1$, String2$)
|
||||
EndIf
|
||||
ProcedureReturn Result
|
||||
EndProcedure
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
If OpenConsole()
|
||||
PrintN(Str(StartsWith("Rosettacode", "Rosetta"))) ; = 1
|
||||
PrintN(Str(StartsWith("Rosettacode", "code"))) ; = 0
|
||||
PrintN(Str(StartsWith("eleutherodactylus cruralis", "e"))) ; = 3
|
||||
PrintN(Str(EndsWith ("Rosettacode", "Rosetta"))) ; = 0
|
||||
PrintN(Str(EndsWith ("Rosettacode", "code"))) ; = 1
|
||||
PrintN(Str(EndsWith ("Rosettacode", "e"))) ; = 2
|
||||
|
||||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
|
|
@ -1,42 +0,0 @@
|
|||
Procedure startsWith(string1$, string2$)
|
||||
;returns one if string1$ starts with string2$, otherwise returns zero
|
||||
If FindString(string1$, string2$, 1) = 1
|
||||
ProcedureReturn 1
|
||||
EndIf
|
||||
ProcedureReturn 0
|
||||
EndProcedure
|
||||
|
||||
Procedure contains(string1$, string2$, location = 0)
|
||||
;returns the location of the next occurrence of string2$ in string1$ starting from location,
|
||||
;or zero if no remaining occurrences of string2$ are found in string1$
|
||||
ProcedureReturn FindString(string1$, string2$, location + 1)
|
||||
EndProcedure
|
||||
|
||||
Procedure endsWith(string1$, string2$)
|
||||
;returns one if string1$ ends with string2$, otherwise returns zero
|
||||
Protected ls = Len(string2$)
|
||||
If Len(string1$) - ls >= 0 And Right(string1$, ls) = string2$
|
||||
ProcedureReturn 1
|
||||
EndIf
|
||||
ProcedureReturn 0
|
||||
EndProcedure
|
||||
|
||||
If OpenConsole()
|
||||
PrintN(Str(startsWith("RosettaCode", "Rosetta"))) ; = 1, true
|
||||
PrintN(Str(startsWith("RosettaCode", "Code"))) ; = 0, false
|
||||
|
||||
PrintN("")
|
||||
PrintN(Str(contains("RosettaCode", "luck"))) ; = 0, no occurrences
|
||||
Define location
|
||||
Repeat
|
||||
location = contains("eleutherodactylus cruralis", "e", location)
|
||||
PrintN(Str(location)) ;display each occurrence: 1, 3, 7, & 0 (no more occurrences)
|
||||
Until location = 0
|
||||
|
||||
PrintN("")
|
||||
PrintN(Str(endsWith ("RosettaCode", "Rosetta"))) ; = 0, false
|
||||
PrintN(Str(endsWith ("RosettaCode", "Code"))) ; = 1, true
|
||||
|
||||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
$haystack.starts-with($needle) # True if $haystack starts with $needle
|
||||
$haystack.contains($needle) # True if $haystack contains $needle
|
||||
$haystack.ends-with($needle) # True if $haystack ends with $needle
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
so $haystack ~~ /^ $needle / # True if $haystack starts with $needle
|
||||
so $haystack ~~ / $needle / # True if $haystack contains $needle
|
||||
so $haystack ~~ / $needle $/ # True if $haystack ends with $needle
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
substr($haystack, 0, $needle.chars) eq $needle # True if $haystack starts with $needle
|
||||
substr($haystack, *-$needle.chars) eq $needle # True if $haystack ends with $needle
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
$haystack.match($needle, :g)».from; # List of all positions where $needle appears in $haystack
|
||||
$haystack.indices($needle :overlap); # Also find any overlapping instances of $needle in $haystack
|
||||
|
|
@ -1 +0,0 @@
|
|||
N;/^\(.*\).*\n\1$/!d;s/\n.*//
|
||||
|
|
@ -1 +0,0 @@
|
|||
N;/.*\(.*\).*\n\1$/!d;s/\n.*//
|
||||
|
|
@ -1 +0,0 @@
|
|||
N;/\(.*\)\n\1$/!d;s/\n.*//
|
||||
|
|
@ -1,43 +0,0 @@
|
|||
Function StartsWith(s1,s2)
|
||||
StartsWith = False
|
||||
If Left(s1,Len(s2)) = s2 Then
|
||||
StartsWith = True
|
||||
End If
|
||||
End Function
|
||||
|
||||
Function Contains(s1,s2)
|
||||
Contains = False
|
||||
If InStr(1,s1,s2) Then
|
||||
Contains = True & " at positions "
|
||||
j = 1
|
||||
Do Until InStr(j,s1,s2) = False
|
||||
Contains = Contains & InStr(j,s1,s2) & ", "
|
||||
If j = 1 Then
|
||||
If Len(s2) = 1 Then
|
||||
j = j + InStr(j,s1,s2)
|
||||
Else
|
||||
j = j + (InStr(j,s1,s2) + (Len(s2) - 1))
|
||||
End If
|
||||
Else
|
||||
If Len(s2) = 1 Then
|
||||
j = j + ((InStr(j,s1,s2) - j) + 1)
|
||||
Else
|
||||
j = j + ((InStr(j,s1,s2) - j) + (Len(s2) - 1))
|
||||
End If
|
||||
End If
|
||||
Loop
|
||||
End If
|
||||
End Function
|
||||
|
||||
Function EndsWith(s1,s2)
|
||||
EndsWith = False
|
||||
If Right(s1,Len(s2)) = s2 Then
|
||||
EndsWith = True
|
||||
End If
|
||||
End Function
|
||||
|
||||
WScript.StdOut.Write "Starts with test, 'foo' in 'foobar': " & StartsWith("foobar","foo")
|
||||
WScript.StdOut.WriteLine
|
||||
WScript.StdOut.Write "Contains test, 'o' in 'fooooobar': " & Contains("fooooobar","o")
|
||||
WScript.StdOut.WriteLine
|
||||
WScript.StdOut.Write "Ends with test, 'bar' in 'foobar': " & EndsWith("foobar","bar")
|
||||
Loading…
Add table
Add a link
Reference in a new issue