CDE
This commit is contained in:
parent
518da4a923
commit
764da6cbbb
6144 changed files with 83610 additions and 11 deletions
10
Task/Empty-string/0DESCRIPTION
Normal file
10
Task/Empty-string/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
Languages may have features for dealing specifically with empty strings (those containing no characters).
|
||||
|
||||
The task is to:
|
||||
|
||||
* Demonstrate how to assign an empty string to a variable.
|
||||
|
||||
* Demonstrate how to check that a string is empty.
|
||||
* Demonstrate how to check that a string is not empty.
|
||||
|
||||
[[Category:String manipulation]]
|
||||
1
Task/Empty-string/ACL2/empty-string.acl2
Normal file
1
Task/Empty-string/ACL2/empty-string.acl2
Normal file
|
|
@ -0,0 +1 @@
|
|||
(= (length str) 0)
|
||||
16
Task/Empty-string/AWK/empty-string.awk
Normal file
16
Task/Empty-string/AWK/empty-string.awk
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#!/usr/bin/awk -f
|
||||
BEGIN {
|
||||
# Demonstrate how to assign an empty string to a variable.
|
||||
a="";
|
||||
b="XYZ";
|
||||
print "a = ",a;
|
||||
print "b = ",b;
|
||||
print "length(a)=",length(a);
|
||||
print "length(b)=",length(b);
|
||||
# Demonstrate how to check that a string is empty.
|
||||
print "Is a empty ?",length(a)==0;
|
||||
print "Is a not empty ?",length(a)!=0;
|
||||
# Demonstrate how to check that a string is not empty.
|
||||
print "Is b empty ?",length(b)==0;
|
||||
print "Is b not empty ?",length(b)!=0;
|
||||
}
|
||||
15
Task/Empty-string/Ada/empty-string.ada
Normal file
15
Task/Empty-string/Ada/empty-string.ada
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
procedure Empty_String is
|
||||
|
||||
function Is_Empty(S: String) return Boolean is
|
||||
begin
|
||||
return S = ""; -- test that S is empty
|
||||
end Is_Empty;
|
||||
|
||||
Empty: String := ""; -- Assign empty string
|
||||
XXXXX: String := "Not Empty";
|
||||
|
||||
begin
|
||||
if (not Is_Empty(Empty)) or Is_Empty(XXXXX) then
|
||||
raise Program_Error with "something went wrong very very badly!!!";
|
||||
end if;
|
||||
end Empty_String;
|
||||
20
Task/Empty-string/AutoHotkey/empty-string.ahk
Normal file
20
Task/Empty-string/AutoHotkey/empty-string.ahk
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
;; Traditional
|
||||
; Assign an empty string:
|
||||
var =
|
||||
; Check that a string is empty:
|
||||
If var =
|
||||
MsgBox the var is empty
|
||||
; Check that a string is not empty
|
||||
If var !=
|
||||
Msgbox the var is not empty
|
||||
|
||||
|
||||
;; Expression mode:
|
||||
; Assign an empty string:
|
||||
var := ""
|
||||
; Check that a string is empty:
|
||||
If (var = "")
|
||||
MsgBox the var is empty
|
||||
; Check that a string is not empty
|
||||
If (var != "")
|
||||
Msgbox the var is not empty
|
||||
4
Task/Empty-string/BASIC/empty-string.bas
Normal file
4
Task/Empty-string/BASIC/empty-string.bas
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
10 LET A$=""
|
||||
20 IF A$="" THEN PRINT "THE STRING IS EMPTY"
|
||||
30 IF A$<>"" THEN PRINT "THE STRING IS NOT EMPTY"
|
||||
40 END
|
||||
8
Task/Empty-string/BBC-BASIC/empty-string.bbc
Normal file
8
Task/Empty-string/BBC-BASIC/empty-string.bbc
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
REM assign an empty string to a variable:
|
||||
var$ = ""
|
||||
|
||||
REM Check that a string is empty:
|
||||
IF var$ = "" THEN PRINT "String is empty"
|
||||
|
||||
REM Check that a string is not empty:
|
||||
IF var$ <> "" THEN PRINT "String is not empty"
|
||||
9
Task/Empty-string/Bracmat/empty-string.bracmat
Normal file
9
Task/Empty-string/Bracmat/empty-string.bracmat
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
( :?a
|
||||
& (b=)
|
||||
& abra:?c
|
||||
& (d=cadabra)
|
||||
& !a: { a is empty string }
|
||||
& !b: { b is also empty string }
|
||||
& !c:~ { c is not an empty string }
|
||||
& !d:~ { neither is d an empty string }
|
||||
)
|
||||
11
Task/Empty-string/C++/empty-string.cpp
Normal file
11
Task/Empty-string/C++/empty-string.cpp
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#include <string>
|
||||
|
||||
// ...
|
||||
|
||||
std::string str; // a string object for an empty string
|
||||
|
||||
if (str.empty()) { ... } // to test if string is empty
|
||||
|
||||
// we could also use the following
|
||||
if (str.length() == 0) { ... }
|
||||
if (str == "") { ... }
|
||||
9
Task/Empty-string/C-sharp/empty-string.cs
Normal file
9
Task/Empty-string/C-sharp/empty-string.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
using System;
|
||||
|
||||
class Program {
|
||||
static void Main (string[] args) {
|
||||
string example = string.Empty;
|
||||
if (string.IsNullOrEmpty(example)) { }
|
||||
if (!string.IsNullOrEmpty(example)) { }
|
||||
}
|
||||
}
|
||||
10
Task/Empty-string/C/empty-string.c
Normal file
10
Task/Empty-string/C/empty-string.c
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
/* assign an empty string */
|
||||
const char *str = "";
|
||||
/* to test a null string */
|
||||
if (str) { ... }
|
||||
/* to test if string is empty */
|
||||
if (str[0] == '\0') { ... }
|
||||
/* or equivalently use strlen function */
|
||||
if (strlen(str) == 0) { ... }
|
||||
/* or compare to a known empty string, same thing. "== 0" means strings are equal */
|
||||
if (strcmp(str, "") == 0) { ... }
|
||||
6
Task/Empty-string/Clojure/empty-string.clj
Normal file
6
Task/Empty-string/Clojure/empty-string.clj
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
(def x "") ;x is "globally" declared to be the empty string
|
||||
(let [x ""]
|
||||
;x is bound to the empty string within the let
|
||||
)
|
||||
(= x "") ;true if x is the empty string
|
||||
(not= x "") ;true if x is not the empty string
|
||||
12
Task/Empty-string/CoffeeScript/empty-string.coffee
Normal file
12
Task/Empty-string/CoffeeScript/empty-string.coffee
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
isEmptyString = (s) ->
|
||||
# Returns true iff s is an empty string.
|
||||
# (This returns false for non-strings as well.)
|
||||
return true if s instanceof String and s.length == 0
|
||||
s == ''
|
||||
|
||||
empties = ["", '', new String()]
|
||||
non_empties = [new String('yo'), 'foo', {}]
|
||||
console.log (isEmptyString(v) for v in empties) # [true, true, true]
|
||||
console.log (isEmptyString(v) for v in non_empties) # [false, false, false]
|
||||
console.log (s = '') == "" # true
|
||||
console.log new String() == '' # false, due to underlying JavaScript's distinction between objects and primitives
|
||||
4
Task/Empty-string/Common-Lisp/empty-string.lisp
Normal file
4
Task/Empty-string/Common-Lisp/empty-string.lisp
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(defparameter *s* "") ;; Binds dynamic variable *S* to the empty string ""
|
||||
(let ((s "")) ;; Binds the lexical variable S to the empty string ""
|
||||
(= (length s) 0) ;; Check if the string is empty
|
||||
(> (length s) 0)) ;; Check if length of string is over 0 (that is: non-empty)
|
||||
27
Task/Empty-string/D/empty-string.d
Normal file
27
Task/Empty-string/D/empty-string.d
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import std.array;
|
||||
|
||||
bool isEmptyNotNull(in string s) pure nothrow @safe {
|
||||
return s is "";
|
||||
}
|
||||
|
||||
void main(){
|
||||
string s1 = null;
|
||||
string s2 = "";
|
||||
|
||||
// the content is the same
|
||||
assert(!s1.length);
|
||||
assert(!s2.length);
|
||||
assert(s1 == "" && s1 == null);
|
||||
assert(s2 == "" && s2 == null);
|
||||
assert(s1 == s2);
|
||||
|
||||
// but they don't point to the same memory region
|
||||
assert(s1 is null && s1 !is "");
|
||||
assert(s2 is "" && s2 !is null);
|
||||
assert(s1 !is s2);
|
||||
assert(s1.ptr == null);
|
||||
assert(*s2.ptr == '\0'); // D string literals are \0 terminated
|
||||
|
||||
assert(s1.empty);
|
||||
assert(s2.isEmptyNotNull());
|
||||
}
|
||||
11
Task/Empty-string/DWScript/empty-string.dwscript
Normal file
11
Task/Empty-string/DWScript/empty-string.dwscript
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
var s : String;
|
||||
|
||||
s := ''; // assign an empty string (can also use "")
|
||||
|
||||
if s = '' then
|
||||
PrintLn('empty');
|
||||
|
||||
s := 'hello';
|
||||
|
||||
if s <> '' then
|
||||
PrintLn('not empty');
|
||||
20
Task/Empty-string/Delphi/empty-string.delphi
Normal file
20
Task/Empty-string/Delphi/empty-string.delphi
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
program EmptyString;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses SysUtils;
|
||||
|
||||
function StringIsEmpty(const aString: string): Boolean;
|
||||
begin
|
||||
Result := aString = '';
|
||||
end;
|
||||
|
||||
var
|
||||
s: string;
|
||||
begin
|
||||
s := '';
|
||||
Writeln(StringIsEmpty(s)); // True
|
||||
|
||||
s := 'abc';
|
||||
Writeln(StringIsEmpty(s)); // False
|
||||
end.
|
||||
8
Task/Empty-string/Erlang/empty-string.erl
Normal file
8
Task/Empty-string/Erlang/empty-string.erl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
1> S = "". % erlang strings are actually lists, so the empty string is the same as the empty list [].
|
||||
[]
|
||||
2> length(S).
|
||||
0
|
||||
3> case S of [] -> empty; [H|T] -> not_empty end.
|
||||
empty
|
||||
4> case "aoeu" of [] -> empty; [H|T] -> not_empty end.
|
||||
not_empty
|
||||
15
Task/Empty-string/Euphoria/empty-string.euphoria
Normal file
15
Task/Empty-string/Euphoria/empty-string.euphoria
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
sequence s
|
||||
|
||||
-- assign an empty string
|
||||
s = ""
|
||||
|
||||
-- another way to assign an empty string
|
||||
s = {} -- "" and {} are equivalent
|
||||
|
||||
if not length(s) then
|
||||
-- string is empty
|
||||
end if
|
||||
|
||||
if length(s) then
|
||||
-- string is not empty
|
||||
end if
|
||||
3
Task/Empty-string/Forth/empty-string.fth
Normal file
3
Task/Empty-string/Forth/empty-string.fth
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
: empty? ( c-addr u -- ? ) nip 0= ;
|
||||
|
||||
s" " dup . empty? . \ 0 -1
|
||||
8
Task/Empty-string/Go/empty-string.go
Normal file
8
Task/Empty-string/Go/empty-string.go
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
// assign empty string to a variable
|
||||
s = ""
|
||||
|
||||
// check that string is empty
|
||||
s == ""
|
||||
|
||||
// check that string is not empty
|
||||
s > ""
|
||||
10
Task/Empty-string/Haskell/empty-string.hs
Normal file
10
Task/Empty-string/Haskell/empty-string.hs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import Control.Monad
|
||||
|
||||
-- In Haskell strings are just lists (of characters), so we can use the function
|
||||
-- 'null', which applies to all lists. We don't want to use the length, since
|
||||
-- Haskell allows infinite lists.
|
||||
|
||||
main = do
|
||||
let s = ""
|
||||
when (null s) (putStrLn "Empty.")
|
||||
when (not $ null s) (putStrLn "Not empty.")
|
||||
6
Task/Empty-string/Java/empty-string.java
Normal file
6
Task/Empty-string/Java/empty-string.java
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
String s = "";
|
||||
if(s != null && s.isEmpty()){//optionally, instead of "s.isEmpty()": "s.length() == 0" or "s.equals("")"
|
||||
System.out.println("s is empty");
|
||||
}else{
|
||||
System.out.println("s is not empty");
|
||||
}
|
||||
11
Task/Empty-string/Lua/empty-string.lua
Normal file
11
Task/Empty-string/Lua/empty-string.lua
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
str = "" -- create empty string
|
||||
|
||||
-- test for empty string
|
||||
if str == "" then
|
||||
print "The string is empty"
|
||||
end
|
||||
|
||||
-- test for nonempty string
|
||||
if str ~= "" then
|
||||
print "The string is not empty"
|
||||
end
|
||||
8
Task/Empty-string/MATLAB/empty-string.m
Normal file
8
Task/Empty-string/MATLAB/empty-string.m
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
% Demonstrate how to assign an empty string to a variable.
|
||||
str = '';
|
||||
% Demonstrate how to check that a string is empty.
|
||||
isempty(str)
|
||||
(length(str)==0)
|
||||
% Demonstrate how to check that a string is not empty.
|
||||
~isempty(str)
|
||||
(length(str)>0)
|
||||
16
Task/Empty-string/PHP/empty-string.php
Normal file
16
Task/Empty-string/PHP/empty-string.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
$str = ''; // assign an empty string to a variable
|
||||
|
||||
// check that a string is empty
|
||||
if (empty($str)) { ... }
|
||||
|
||||
// check that a string is not empty
|
||||
if (! empty($str)) { ... }
|
||||
|
||||
// we could also use the following
|
||||
if ($str == '') { ... }
|
||||
if ($str != '') { ... }
|
||||
|
||||
if (strlen($str) == 0) { ... }
|
||||
if (strlen($str) != 0) { ... }
|
||||
6
Task/Empty-string/Perl/empty-string-1.pl
Normal file
6
Task/Empty-string/Perl/empty-string-1.pl
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
if ($s eq "") { # Test for empty string
|
||||
print "The string is empty";
|
||||
}
|
||||
if ($s ne "") { # Test for non empty string
|
||||
print "The string is not empty";
|
||||
}
|
||||
14
Task/Empty-string/Perl/empty-string-2.pl
Normal file
14
Task/Empty-string/Perl/empty-string-2.pl
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
$s = "";
|
||||
if ($s) { ... } # false
|
||||
|
||||
# to tell if a string is false because it's empty, or it's plain not there (undefined)
|
||||
$s = undef;
|
||||
if (defined $s) { ... } # false; would be true on ""
|
||||
|
||||
# though, perl implicitly converts between strings and numbers, so this is also false
|
||||
$s = "0";
|
||||
if ($s) { ... } # false; also false on "000", "0.0", "\x0", "0 with text", etc
|
||||
|
||||
# but a string that converts to number 0 is not always false, though:
|
||||
$s = "0 but true";
|
||||
if ($s) { ... } # it's true! black magic!
|
||||
14
Task/Empty-string/PicoLisp/empty-string.l
Normal file
14
Task/Empty-string/PicoLisp/empty-string.l
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
# To assign a variable an empty string:
|
||||
(off String)
|
||||
(setq String "")
|
||||
(setq String NIL)
|
||||
|
||||
# To check for an empty string:
|
||||
(or String ..)
|
||||
(ifn String ..)
|
||||
(unless String ..)
|
||||
|
||||
# or a non-empty string:
|
||||
(and String ..)
|
||||
(if String ..)
|
||||
(when String ..)
|
||||
5
Task/Empty-string/Python/empty-string.py
Normal file
5
Task/Empty-string/Python/empty-string.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
s = ''
|
||||
if not s:
|
||||
print('String s is empty.')
|
||||
if s:
|
||||
print('String s is not empty.')
|
||||
9
Task/Empty-string/R/empty-string.r
Normal file
9
Task/Empty-string/R/empty-string.r
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
s <- ''
|
||||
|
||||
if (s == '') cat('Empty\n')
|
||||
#or
|
||||
if (nchar(s) == 0) cat('Empty\n')
|
||||
|
||||
if (s != '') cat('Not empty\n')
|
||||
#or
|
||||
if (nchar(s) > 0) cat('Not empty\n')
|
||||
40
Task/Empty-string/REXX/empty-string.rexx
Normal file
40
Task/Empty-string/REXX/empty-string.rexx
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/*REXX: how to assign an empty string & then check for empty/not-empty.*/
|
||||
|
||||
/*─────────────── 3 simple wats to assign an empty string to a variable.*/
|
||||
auk='' /*uses two single quotes or apostrophies. */
|
||||
ide="" /*uses two quotes, sometimes called a double quote.*/
|
||||
doe= /*... nothing at all. */
|
||||
|
||||
/*─────────────── assigning multiple null values to vars, 2 methods are:*/
|
||||
ram=0
|
||||
parse var ram . emu pug yak nit moa owl pas jay koi ern ewe fae gar hob
|
||||
/*where the value of zero is skipped, the rest set to null,*/
|
||||
/*which is the next value AFTER the value of RAM (nothing).*/
|
||||
|
||||
/*───or─── (with less clutter ─── or more, ... perception).*/
|
||||
parse value 0 with . ant ape ant imp fly tui paa elo dab cub bat ayu
|
||||
/*where the value of zero is skipped, the rest set to null,*/
|
||||
/*which is the next value AFTER the 0 (zero): nothing. */
|
||||
|
||||
/*─────────────── how to check that a string is empty, several methods: */
|
||||
if cat=='' then say "the feline is not here."
|
||||
if pig=="" then say 'no ham today'
|
||||
if length(gnu)==0 then say "the wildebeast is empty & hungry."
|
||||
if length(ips)=0 then say "checking with == instead of = is faster"
|
||||
if length(hub)<1 then method = "obtuse, don't do as I do ..."
|
||||
|
||||
nit='' /*assign an empty string to a lice egg.*/
|
||||
if cow==nit then say 'the cow has no milk today.'
|
||||
|
||||
/*─────────────── how to check that a string isn't empty, several ways: */
|
||||
if dog\=='' then say "the dogs are out!"
|
||||
/*most REXXes support the "not" character. */
|
||||
if fox¬=='' then say "and the fox is in the henhouse."
|
||||
if length(rat)>0 then say "the rat is singing" /*ugly way to test.*/
|
||||
|
||||
if elk=='' then nop; else say "long way for an elk to be tested."
|
||||
|
||||
if length(eel)\==0 then fish=eel /*fast compare, quick.*/
|
||||
if length(cod)\=0 then fish=cod /*not-so-fast compare.*/
|
||||
|
||||
/*────────────────────────── anyway, as they say: "choose your poison." */
|
||||
5
Task/Empty-string/Racket/empty-string.rkt
Normal file
5
Task/Empty-string/Racket/empty-string.rkt
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
#lang racket
|
||||
|
||||
(define empty-string "")
|
||||
(define (string-null? s) (string=? "" s))
|
||||
(define (string-not-null? s) (string<? "" s))
|
||||
3
Task/Empty-string/Ruby/empty-string-1.rb
Normal file
3
Task/Empty-string/Ruby/empty-string-1.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
s = ""
|
||||
s = String.new
|
||||
s = "any string"; s.clear
|
||||
8
Task/Empty-string/Ruby/empty-string-2.rb
Normal file
8
Task/Empty-string/Ruby/empty-string-2.rb
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
s == ""
|
||||
s.eql?("")
|
||||
s.empty?
|
||||
s.length == 0
|
||||
s[/\A\z/]
|
||||
|
||||
# also silly things like
|
||||
s.each_char.to_a.empty?
|
||||
3
Task/Empty-string/Ruby/empty-string-3.rb
Normal file
3
Task/Empty-string/Ruby/empty-string-3.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
s != ""
|
||||
s.length > 0
|
||||
s[/./m]
|
||||
1
Task/Empty-string/Ruby/empty-string-4.rb
Normal file
1
Task/Empty-string/Ruby/empty-string-4.rb
Normal file
|
|
@ -0,0 +1 @@
|
|||
if s then puts "not empty" end # This code is wrong!
|
||||
10
Task/Empty-string/Scala/empty-string.scala
Normal file
10
Task/Empty-string/Scala/empty-string.scala
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
// assign empty string to a variable
|
||||
val s=""
|
||||
// check that string is empty
|
||||
s.isEmpty // true
|
||||
s=="" // true
|
||||
s.size==0 // true
|
||||
// check that string is not empty
|
||||
s.nonEmpty // false
|
||||
s!="" // false
|
||||
s.size>0 // false
|
||||
3
Task/Empty-string/Scheme/empty-string.ss
Normal file
3
Task/Empty-string/Scheme/empty-string.ss
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
(define empty-string "")
|
||||
(define (string-null? s) (string=? "" s))
|
||||
(define (string-not-null? s) (string<? "" s))
|
||||
3
Task/Empty-string/Tcl/empty-string-1.tcl
Normal file
3
Task/Empty-string/Tcl/empty-string-1.tcl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
set s ""
|
||||
if {$s eq ""} {puts "s contains an empty string"}
|
||||
if {$s ne ""} {puts "s contains a non-empty string"}
|
||||
3
Task/Empty-string/Tcl/empty-string-2.tcl
Normal file
3
Task/Empty-string/Tcl/empty-string-2.tcl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
if {[string equal $s ""]} {puts "is empty"}
|
||||
if {[string length $s] == 0} {puts "is empty"}
|
||||
if {[string compare $s ""] != 0} {puts "is non-empty"}
|
||||
Loading…
Add table
Add a link
Reference in a new issue