Anonymous user
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>▼
▲ procedure Parse_Commas is
Index_List : array(1..256) of Natural;
Source_String : String := "Hello,How,Are,You,Today";▼
▲ begin
while Index_List(Next_Index)
if Index_List(Next_Index)
▲ end if;
▲ Put(Source_String(Index_List(Next_Index - 1)..Index_List(Next_Index)-2) & ".");
▲ end loop;
▲ end Parse_Commas;
</lang>▼
=={{header|ALGOL 68}}==
<lang
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>
=={{header|AWK}}==
Line 113 ⟶ 107:
for(i=1; i <= NF; i++) printf $i ".";
print ""
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<stdio.h>
#include<stdlib.h>
Line 196 ⟶ 188:
return 0;
▲}</lang>
=={{header|C sharp|C#}}==
Line 284 ⟶ 275:
=={{header|Clojure}}==
<lang lisp>(apply str (interpose "." (seq (.split #"," "Hello,How,Are,You,Today"))))</lang>▼
▲(apply str (interpose "." (seq (.split #"," "Hello,How,Are,You,Today"))))
=={{header|D}}==
Line 293 ⟶ 282:
=={{header|E}}==
<lang
=={{header|Erlang}}==
Line 384 ⟶ 373:
=={{header|Groovy}}==
=={{header|Io}}==
Line 402 ⟶ 391:
Alternatively using the system library/script <tt>strings</tt>
<lang j> require 'strings'
',' splitstring s
+-----+---+---+---+-----+
Line 410 ⟶ 398:
'.' joinstring ',' splitstring s
Hello.How.Are.You.Today</lang>
<tt>splitstring</tt> and <tt>joinstring</tt> also work with longer "delimiters":
<lang j> '"'([ ,~ ,) '","' joinstring ',' splitstring s▼
"Hello","How","Are","You","Today"</lang>▼
▲ '"'([ ,~ ,) '","' joinstring ',' splitstring s
▲"Hello","How","Are","You","Today"
=={{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 "||]
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 logo>
=={{header|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>
Output:
Line 795 ⟶ 776:
=={{header|R}}==
junk <- strsplit(text, split=",")
print(paste(unlist(junk), collapse="."))</lang>
or the one liner
<lang R>paste(unlist(strsplit(text, split=",")), collapse=".")</lang>▼
▲paste(unlist(strsplit(text, split=",")), collapse=".")
Line 820 ⟶ 797:
=={{header|Slate}}==
<lang slate>('Hello,How,Are,You,Today' splitWith: $,) join &separator: '.'.</lang>▼
▲('Hello,How,Are,You,Today' splitWith: $,) join &separator: '.'.
=={{header|Smalltalk}}==
Line 864 ⟶ 839:
=={{header|tr}}==
=={{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)
Ins_Text("Hello,How,Are,You,Today")
Line 942 ⟶ 914:
}
Buf_Quit(OK)</lang>
|