Tokenize a string: Difference between revisions

m
Fixed lang tags.
m (Fixed lang tags.)
Line 3:
 
=={{header|ActionScript}}==
<lang actionscript>var hello:String = "Hello,How,Are,You,Today";
var hello:String = "Hello,How,Are,You,Today";
var tokens:Array = hello.split(",");
trace(tokens.join("."));
 
// Or as a one-liner
trace("Hello,How,Are,You,Today".split(",").join("."));</lang>
</lang>
 
=={{header|Ada}}==
<lang ada>with Ada.Strings.Fixed; use Ada.Strings.Fixed;
<lang ada>
with Ada.Strings.FixedText_Io; use Ada.Strings.FixedText_Io;
 
with Ada.Text_Io; use Ada.Text_Io;
procedure Parse_Commas is
var hello Source_String : String := "Hello,How,Are,You,Today";
procedure Parse_Commas is
Index_List : array(1..256) of Natural;
Source_String : String := "Hello,How,Are,You,Today";
Index_ListNext_Index : array(1..256)Natural of:= Natural1;
begin
Index_List(Next_Index : Natural) := 1;
begin
while Index_List(Next_Index) :=< 1;Source_String'Last loop
while Index_List( Next_Index) <:= Next_Index Source_String'Last+ loop1;
Index_List(Next_Index) := Next_Index1 + Index(Source_String(Index_List(Next_Index - 1)..Source_String'Last), ",");
if Index_List(Next_Index) := 1 +then Index(Source_String(Index_List(Next_Index - 1)..Source_String'Last), ",");
if Index_List(Next_Index) := 1Source_String'Last then+ 2;
end if;
Index_List(Next_Index) := Source_String'Last + 2;
Put(Source_String(Index_List(Next_Index - 1)..Index_List(Next_Index)-2) & ".");
end if;
end loop;
Put(Source_String(Index_List(Next_Index - 1)..Index_List(Next_Index)-2) & ".");
end Parse_Commas;</lang>
end loop;
end Parse_Commas;
</lang>
=={{header|ALGOL 68}}==
<lang algolalgol68>main:(
 
OP +:= = (REF FLEX[]STRING in out, STRING item)VOID:(
Line 88 ⟶ 84:
</pre>
=={{header|AutoHotkey}}==
<lang AutoHotkey>string := "Hello,How,Are,You,Today"
string := "Hello,How,Are,You,Today"
stringsplit, string, string, `,
loop, % string0
{
msgbox % string%A_Index%
}</lang>
}
</lang>
=={{header|AWK}}==
 
Line 113 ⟶ 107:
for(i=1; i <= NF; i++) printf $i ".";
print ""
}</lang ada>
}
</lang>
 
which "tokenize" each line of input and this is achieved by using "," as field separator
Line 174 ⟶ 167:
This example uses the ''strtok()'' function to separate the tokens. This function is destructive (replacing token separators with '\0'), so we have to make a copy of the string (using ''strdup()'') before tokenizing. ''strdup()'' is not part of [[ANSI C]], but is available on most platforms. It can easily be implemented with a combination of ''strlen()'', ''malloc()'', and ''strcpy()''.
 
<lang c>#include<string.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
Line 196 ⟶ 188:
 
return 0;
}</lang>
}
</lang>
 
=={{header|C sharp|C#}}==
Line 284 ⟶ 275:
 
=={{header|Clojure}}==
<lang lisp>(apply str (interpose "." (seq (.split #"," "Hello,How,Are,You,Today"))))</lang>
<lang Clojure>
(apply str (interpose "." (seq (.split #"," "Hello,How,Are,You,Today"))))
</lang>
 
=={{header|D}}==
Line 293 ⟶ 282:
 
=={{header|E}}==
<lang De>".".rjoin("Hello,How,Are,You,Today".split(","))</lang>
 
=={{header|Erlang}}==
Line 384 ⟶ 373:
 
=={{header|Groovy}}==
<lang groovy>println 'Hello,How,Are,You,Today'.split(',').join('.')</lang>
 
=={{header|Io}}==
Line 402 ⟶ 391:
 
Alternatively using the system library/script <tt>strings</tt>
<lang j> require 'strings'
require 'strings'
',' splitstring s
+-----+---+---+---+-----+
Line 410 ⟶ 398:
 
'.' joinstring ',' splitstring s
Hello.How.Are.You.Today</lang>
</lang>
 
<tt>splitstring</tt> and <tt>joinstring</tt> also work with longer "delimiters":
<lang j> '"'([ ,~ ,) '","' joinstring ',' splitstring s
<lang j>
"Hello","How","Are","You","Today"</lang>
'"'([ ,~ ,) '","' joinstring ',' splitstring s
"Hello","How","Are","You","Today"
</lang>
 
=={{header|Java}}==
Line 450 ⟶ 435:
 
This form is more robust, doing the right thing if there are embedded spaces.
<lang logo>to split :str :by [:acc []] [:w "||]
to split :str :by [:acc []] [:w "||]
if empty? :str [output lput :w :acc]
ifelse equal? first :str :by ~
[output (split butfirst :str :by lput :w :acc)] ~
[output (split butfirst :str :by :acc lput first :str :w)]
end</lang>
</lang>
 
<lang logo> ? show split "Hello,How,Are,You,Today ",
[Hello How Are You Today]</lang>
 
=={{header|M4}}==
string<lang := "M4>define(`s',`Hello,How,Are,You,Today"')
<lang M4>
define(`s',`Hello,How,Are,You,Today')
define(`set',`define(`$1[$2]',`$3')')
define(`get',`defn($1[$2])')
Line 474 ⟶ 456:
define(`show',
`ifelse(eval(j<n),1,`get(a,j).`'define(`j',incr(j))`'show')')
show</lang>
</lang>
 
Output:
Line 795 ⟶ 776:
 
=={{header|R}}==
<lang R>text Source_String : String :=<- "Hello,How,Are,You,Today";
<lang R>
text <- "Hello,How,Are,You,Today"
junk <- strsplit(text, split=",")
print(paste(unlist(junk), collapse="."))</lang>
</lang>
 
or the one liner
 
<lang R>paste(unlist(strsplit(text, split=",")), collapse=".")</lang>
<lang R>
paste(unlist(strsplit(text, split=",")), collapse=".")
</lang>
 
 
Line 820 ⟶ 797:
 
=={{header|Slate}}==
<lang slate>('Hello,How,Are,You,Today' splitWith: $,) join &separator: '.'.</lang>
<lang slate>
('Hello,How,Are,You,Today' splitWith: $,) join &separator: '.'.
</lang>
 
=={{header|Smalltalk}}==
Line 864 ⟶ 839:
 
=={{header|tr}}==
define(`s<lang tr>echo ',`Hello,How,Are,You,Today') | tr ',' '.'</lang>
<lang tr>
echo 'Hello,How,Are,You,Today' | tr ',' '.'
</lang>
 
=={{header|UnixPipes}}==
Line 922 ⟶ 895:
The contents of each text register is then displayed to user, separated by a period.
 
<lang vedit>Buf_Switch(Buf_Free)
Buf_Switch(Buf_Free)
Ins_Text("Hello,How,Are,You,Today")
 
Line 942 ⟶ 914:
}
 
Buf_Quit(OK)</lang>
</lang>
Anonymous user