Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -1,4 +1,5 @@
|
|||
{{basic data operation}}
|
||||
{{basic data operation}}
|
||||
[[Category: String manipulation]] [[Category:Simple]]
|
||||
Given two strings, demonstrate the following 3 types of matchings:
|
||||
|
||||
# Determining if the first string starts with second string
|
||||
|
|
|
|||
70
Task/String-matching/Batch-File/string-matching.bat
Normal file
70
Task/String-matching/Batch-File/string-matching.bat
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
::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.
|
||||
24
Task/String-matching/DCL/string-matching.dcl
Normal file
24
Task/String-matching/DCL/string-matching.dcl
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
$ first_string = p1
|
||||
$ length_of_first_string = f$length( first_string )
|
||||
$ second_string = p2
|
||||
$ length_of_second_string = f$length( second_string )
|
||||
$ offset = f$locate( second_string, first_string )
|
||||
$ if offset .eq. 0
|
||||
$ then
|
||||
$ write sys$output "first string starts with second string"
|
||||
$ else
|
||||
$ write sys$output "first string does not start with second string"
|
||||
$ endif
|
||||
$ if offset .ne. length_of_first_string
|
||||
$ then
|
||||
$ write sys$output "first string contains the second string at location ", offset
|
||||
$ else
|
||||
$ write sys$output "first string does not contain the second string at any location"
|
||||
$ endif
|
||||
$ temp = f$extract( length_of_first_string - length_of_second_string, length_of_second_string, first_string )
|
||||
$ if second_string .eqs. temp
|
||||
$ then
|
||||
$ write sys$output "first string ends with the second string"
|
||||
$ else
|
||||
$ write sys$output "first string does not end with the second string"
|
||||
$ endif
|
||||
|
|
@ -1,22 +1,25 @@
|
|||
#define system'routines.
|
||||
#define extensions.
|
||||
|
||||
// --- Program ---
|
||||
#import system.
|
||||
#import extensions.
|
||||
|
||||
#symbol program =
|
||||
[
|
||||
literalControl starting:"hello" &with:"hel" ?
|
||||
[
|
||||
consoleEx writeLine:"hello starts with hel".
|
||||
].
|
||||
#var s := "abcd".
|
||||
|
||||
literalControl ending:"hello" &with:"llo" ?
|
||||
[
|
||||
consoleEx writeLine:"hello ends with llo".
|
||||
].
|
||||
console writeLine:s:" starts with ab: ":(s startingWith:"ab" literal).
|
||||
console writeLine:s:" starts with cd: ":(s startingWith:"cd" literal).
|
||||
|
||||
literalControl contain:"el" &in:"hello" ?
|
||||
[
|
||||
consoleEx writeLine:"hello contains el".
|
||||
].
|
||||
console writeLine:s:" ends with ab: ":(s endingWith:"ab" literal).
|
||||
console writeLine:s:" ends with cd: ":(s endingWith:"cd" literal).
|
||||
|
||||
console writeLine:s:" contains ab: ":(s containing:"ab" literal).
|
||||
console writeLine:s:" contains bc: ":(s containing:"bc" literal).
|
||||
console writeLine:s:" contains cd: ":(s containing:"cd" literal).
|
||||
console writeLine:s:" contains az: ":(s containing:"az" literal).
|
||||
|
||||
console writeLine:s:" index of az: ":(s indexOf:"az" &at:0 literal).
|
||||
console writeLine:s:" index of cd: ":(s indexOf:"cd" &at:0 literal).
|
||||
console writeLine:s:" index of bc: ":(s indexOf:"bc" &at:0 literal).
|
||||
console writeLine:s:" index of ab: ":(s indexOf:"ab" &at:0 literal).
|
||||
|
||||
console readChar.
|
||||
].
|
||||
|
|
|
|||
29
Task/String-matching/Emacs-Lisp/string-matching.l
Normal file
29
Task/String-matching/Emacs-Lisp/string-matching.l
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
(defun match (word str)
|
||||
(progn
|
||||
|
||||
(setq regex (format "^%s.*$" word) )
|
||||
|
||||
(if (string-match regex str)
|
||||
(insert (format "%s found in beginning of: %s\n" word str) )
|
||||
(insert (format "%s not found in beginning of: %s\n" word str) ))
|
||||
|
||||
(setq pos (string-match word str) )
|
||||
|
||||
(if pos
|
||||
(insert (format "%s found at position %d in: %s\n" word pos str) )
|
||||
(insert (format "%s not found in: %s\n" word str) ))
|
||||
|
||||
(setq regex (format "^.*%s$" word) )
|
||||
|
||||
(if (string-match regex str)
|
||||
(insert (format "%s found in end of: %s\n" word str) )
|
||||
(insert (format "%s not found in end of: %s\n" word str) ))))
|
||||
|
||||
(setq string "before center after")
|
||||
|
||||
(progn
|
||||
(match "center" string)
|
||||
(insert "\n")
|
||||
(match "before" string)
|
||||
(insert "\n")
|
||||
(match "after" string) )
|
||||
40
Task/String-matching/Fortran/string-matching-1.f
Normal file
40
Task/String-matching/Fortran/string-matching-1.f
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
SUBROUTINE STARTS(A,B) !Text A starts with text B?
|
||||
CHARACTER*(*) A,B
|
||||
IF (INDEX(A,B).EQ.1) THEN !Searches A to find B.
|
||||
WRITE (6,*) ">",A,"< starts with >",B,"<"
|
||||
ELSE
|
||||
WRITE (6,*) ">",A,"< does not start with >",B,"<"
|
||||
END IF
|
||||
END SUBROUTINE STARTS
|
||||
|
||||
SUBROUTINE HAS(A,B) !Text B appears somewhere in text A?
|
||||
CHARACTER*(*) A,B
|
||||
INTEGER L
|
||||
L = INDEX(A,B) !The first position in A where B matches.
|
||||
IF (L.LE.0) THEN
|
||||
WRITE (6,*) ">",A,"< does not contain >",B,"<"
|
||||
ELSE
|
||||
WRITE (6,*) ">",A,"< contains a >",B,"<, offset",L
|
||||
END IF
|
||||
END SUBROUTINE HAS
|
||||
|
||||
SUBROUTINE ENDS(A,B) !Text A ends with text B.
|
||||
CHARACTER*(*) A,B
|
||||
INTEGER L
|
||||
L = LEN(A) - LEN(B) !Find the tail end of A that B might match.
|
||||
IF (L.LT.0) THEN !Dare not use an OR, because of full evaluation risks.
|
||||
WRITE (6,*) ">",A,"< is too short to end with >",B,"<" !Might as well have a special message.
|
||||
ELSE IF (A(L + 1:L + LEN(B)).NE.B) THEN !Otherwise, it is safe to look.
|
||||
WRITE (6,*) ">",A,"< does not end with >",B,"<"
|
||||
ELSE
|
||||
WRITE (6,*) ">",A,"< ends with >",B,"<"
|
||||
END IF
|
||||
END SUBROUTINE ENDS
|
||||
|
||||
CALL STARTS("This","is")
|
||||
CALL STARTS("Theory","The")
|
||||
CALL HAS("Bananas","an")
|
||||
CALL ENDS("Banana","an")
|
||||
CALL ENDS("Banana","na")
|
||||
CALL ENDS("Brief","Much longer")
|
||||
END
|
||||
62
Task/String-matching/Fortran/string-matching-2.f
Normal file
62
Task/String-matching/Fortran/string-matching-2.f
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
!-----------------------------------------------------------------------
|
||||
!Main program string_matching
|
||||
!-----------------------------------------------------------------------
|
||||
program string_matching
|
||||
implicit none
|
||||
character(len=*), parameter :: fmt= '(I0)'
|
||||
write(*,fmt) starts("this","is")
|
||||
write(*,fmt) starts("theory","the")
|
||||
write(*,fmt) has("bananas","an")
|
||||
write(*,fmt) ends("banana","an")
|
||||
write(*,fmt) ends("banana","na")
|
||||
write(*,fmt) ends("brief","much longer")
|
||||
|
||||
contains
|
||||
! Determining if the first string starts with second string
|
||||
function starts(string1, string2) result(answer)
|
||||
implicit none
|
||||
character(len=*), intent(in) :: string1
|
||||
character(len=*), intent(in) :: string2
|
||||
integer :: answer
|
||||
answer = 0
|
||||
if(len(string2)>len(string1)) return
|
||||
if(string1(1:len(string2))==string2) answer = 1
|
||||
end function starts
|
||||
! Determining if the first string contains the second string at any location
|
||||
function has(string1, string2) result(answer)
|
||||
implicit none
|
||||
character(len=*), intent(in) :: string1
|
||||
character(len=*), intent(in) :: string2
|
||||
character(len=:),allocatable :: temp
|
||||
integer :: answer, add
|
||||
character(len=*), parameter :: fmt= '(A6,X,I0)'
|
||||
answer = 0
|
||||
add = 0
|
||||
if(len(string2)>len(string1)) return
|
||||
answer = index(string1, string2)
|
||||
if(answer==0) return
|
||||
! Print the location of the match for part 2
|
||||
write(*,fmt) " at ", answer
|
||||
! Handle multiple occurrences of a string for part 2.
|
||||
add = answer
|
||||
temp = string1(answer+1:)
|
||||
do while(answer>0)
|
||||
answer = index(temp, string2)
|
||||
add = add + answer
|
||||
if(answer>0) write(*,fmt) " at ", add
|
||||
! deallocate(temp)
|
||||
temp = string1(add+1:) ! auto reallocation
|
||||
enddo
|
||||
answer = 1
|
||||
end function has
|
||||
! Determining if the first string ends with the second string
|
||||
function ends(string1, string2) result(answer)
|
||||
implicit none
|
||||
character(len=*), intent(in) :: string1
|
||||
character(len=*), intent(in) :: string2
|
||||
integer :: answer
|
||||
answer = 0
|
||||
if(len(string2)>len(string1)) return
|
||||
if(string1(len(string1)-len(string2)+1:)==string2) answer = 1
|
||||
end function ends
|
||||
end program string_matching
|
||||
|
|
@ -5,13 +5,13 @@ my @subs = (
|
|||
sub R_ends_with ( Str $_, Str $s2 ) { ? m/ $s2 $ / },
|
||||
|
||||
# Index-based:
|
||||
sub I_contains ( Str $_, Str $s2 ) { .index( $s2)\ .defined },
|
||||
sub I_contains ( Str $_, Str $s2 ) { .index( $s2) .defined },
|
||||
sub I_starts_with ( Str $_, Str $s2 ) { my $m = .index( $s2); $m.defined and $m == 0 },
|
||||
sub I_ends_with ( Str $_, Str $s2 ) { my $m = .rindex($s2); $m.defined and $m == .chars - $s2.chars },
|
||||
|
||||
# Substr-based:
|
||||
sub S_starts_with ( Str $_, Str $s2 ) { .substr(0, $s2.chars) eq $s2 },
|
||||
sub S_ends_with ( Str $_, Str $s2 ) { .substr( -$s2.chars) eq $s2 },
|
||||
sub S_ends_with ( Str $_, Str $s2 ) { .substr( *-$s2.chars) eq $s2 },
|
||||
|
||||
# Optional tasks:
|
||||
sub R_find ( Str $_, Str $s2 ) { $/.from if /$s2/ },
|
||||
|
|
@ -26,7 +26,7 @@ my @str2s = < ab bc cd zz >;
|
|||
|
||||
say "'$str1' vs:".fmt('%15s '), @str2s.fmt('%-15s');
|
||||
for [1, 4, 6], [2, 5, 7], [0, 3], [8, 9] {
|
||||
say;
|
||||
say();
|
||||
for @subs[.list] -> $sub {
|
||||
say "{$sub.name}:".fmt('%15s '),
|
||||
@str2s.map({ ~$sub.($str1, $_) }).fmt('%-15s');
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
p 'abcd'.start_with?('ab') #returns true
|
||||
p 'abcd'.end_with?('ab') #returns false
|
||||
p 'abab'.include?('bb') #returns false
|
||||
p 'abab'.include?('ab') #returns true
|
||||
p 'abab'.index('bb') #returns nil
|
||||
p 'abab'.index('ab') #returns 0
|
||||
p 'abab'.index('ab', 1) #returns 2
|
||||
p 'abab'.rindex('ab') #returns 2
|
||||
p 'abcd'.start_with?('ab') #returns true
|
||||
p 'abcd'.end_with?('ab') #returns false
|
||||
p 'abab'.include?('bb') #returns false
|
||||
p 'abab'.include?('ab') #returns true
|
||||
p 'abab'['bb'] #returns nil
|
||||
p 'abab'['ab'] #returns "ab"
|
||||
p 'abab'.index('bb') #returns nil
|
||||
p 'abab'.index('ab') #returns 0
|
||||
p 'abab'.index('ab', 1) #returns 2
|
||||
p 'abab'.rindex('ab') #returns 2
|
||||
|
|
|
|||
22
Task/String-matching/Rust/string-matching.rust
Normal file
22
Task/String-matching/Rust/string-matching.rust
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
fn print_match(possible_match: Option<usize>) {
|
||||
match possible_match {
|
||||
Some(match_pos) => println!("Found match at pos {}", match_pos),
|
||||
None => println!("Did not find any matches")
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let s1 = "abcd";
|
||||
let s2 = "abab";
|
||||
let s3 = "ab";
|
||||
|
||||
// Determining if the first string starts with second string
|
||||
assert!(s1.starts_with(s3));
|
||||
// Determining if the first string contains the second string at any location
|
||||
assert!(s1.contains(s3));
|
||||
// Print the location of the match
|
||||
print_match(s1.find(s3)); // Found match at pos 0
|
||||
print_match(s1.find(s2)); // Did not find any matches
|
||||
// Determining if the first string ends with the second string
|
||||
assert!(s2.ends_with(s3));
|
||||
}
|
||||
43
Task/String-matching/VBScript/string-matching.vb
Normal file
43
Task/String-matching/VBScript/string-matching.vb
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
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