Tokenize a string: Difference between revisions

alphabetized list; numerous minor edits
m (→‎{{header|Bracmat}}: removed some newlines)
(alphabetized list; numerous minor edits)
Line 1:
'{{task|String manipulation}}
Separate the string "Hello,How,Are,You,Today" by commas into an array (or list) so that each element of it stores a different word. Display the words to the 'user', in the simplest manner possible, separated by a period. To simplify, you may display a trailing period.
 
Line 29:
end loop;
end Parse_Commas;</lang>
 
=={{header|ALGOL 68}}==
<lang algol68>main:(
Line 52 ⟶ 53:
out
);
 
PROC char split = (REF STRING beetles, STRING chars)[]STRING: (
""" Split beetles where character is found in chars """;
Line 79 ⟶ 80:
printf(($g"."$, char split(beetles, ", "),$l$))
)</lang>
Output:<pre>
John Lennon.Paul McCartney.George Harrison.Ringo Starr.
John.Lennon..Paul.McCartney..George.Harrison..Ringo.Starr.
 
</pre>
=={{header|AutoHotkey}}==
<lang AutoHotkey>string := "Hello,How,Are,You,Today"
Line 90 ⟶ 91:
msgbox % string%A_Index%
}</lang>
 
=={{header|AWK}}==
 
Line 110 ⟶ 112:
 
which "tokenize" each line of input and this is achieved by using "," as field separator
 
=={{header|Batch File}}==
<lang dos>@echo off
setlocal enabledelayedexpansion
call :tokenize %1 res
echo %res%
goto :eof
 
:tokenize
set str=%~1
:loop
for %%i in (%str%) do set %2=!%2!.%%i
set %2=!%2:~1!
goto :eof</lang>
 
''Demo''
<lang dos>>tokenize.cmd "Hello,How,Are,You,Today"
Hello.How.Are.You.Today</lang>
 
=={{header|BASIC}}==
Line 177 ⟶ 161:
NEXT
END IF</lang>
 
=={{header|Batch File}}==
<lang dos>@echo off
setlocal enabledelayedexpansion
call :tokenize %1 res
echo %res%
goto :eof
 
:tokenize
set str=%~1
:loop
for %%i in (%str%) do set %2=!%2!.%%i
set %2=!%2:~1!
goto :eof</lang>
 
''Demo''
>tokenize.cmd "Hello,How,Are,You,Today"
Hello.How.Are.You.Today
 
=={{header|BBC BASIC}}==
Line 388 ⟶ 390:
}</lang>
Output:
<pre>Hello.How.Are.You.Today</pre>
 
=={{header|Delphi}}==
Line 562 ⟶ 564:
fmt.Println(strings.Join(strings.Split(s, ","), "."))
}</lang>
 
=={{header|Groovy}}==
<lang groovy>println 'Hello,How,Are,You,Today'.split(',').join('.')</lang>
 
=={{header|Haskell}}==
Line 593 ⟶ 598:
* You need to import the modules Data.List and Control.Arrow
 
As special cases, splitting / joining by white space and by newlines are provided by the Prelude functions <ttcode>words</ttcode> / <ttcode>unwords</ttcode> and <ttcode>lines</ttcode> / <ttcode>unlines</ttcode>, respectively.
 
=={{header|Groovy}}==
<lang groovy>println 'Hello,How,Are,You,Today'.split(',').join('.')</lang>
 
=={{header|HicEst}}==
Line 622 ⟶ 624:
 
Output:
<pre> ->ss
Hello.How.Are.You.
->
</pre>
 
=={{header|Io}}==
Line 737 ⟶ 738:
print( tokens[i] )
end</lang>
 
 
=={{header|Mathematica}}==
<lang Mathematica>Row[Riffle[StringSplit["Hello,How,Are,You,Today", ","], "."]]</lang>
 
=={{header|M4}}==
Line 756 ⟶ 753:
 
Output:
Hello.How.Are.You.Today.
<pre>
 
Hello.How.Are.You.Today.
=={{header|Mathematica}}==
</pre>
<lang Mathematica>Row[Riffle[StringSplit["Hello,How,Are,You,Today", ","], "."]]</lang>
 
=={{header|MATLAB}} / {{header|Octave}}==
Line 860 ⟶ 858:
TRAP 0,Halt,0</lang>
Output:
<pre> ~/MIX/MMIX/Progs> mmix tokenizing
Hello
How
Are
You
Today</pre>
 
=={{header|Modula-3}}==
Line 895 ⟶ 893:
QUIT</lang>
 
In use:<pre>USER>D TOKENS^ROSETTA
USER>D TOKENS^ROSETTA
Hello.how.are.you.today</pre>
Hello.how.are.you.today
 
=={{header|Nemerle}}==
Line 913 ⟶ 912:
}
}</lang>
 
=={{header|Objective-C}}==
{{works with|GNUstep}}
 
{{works with|Cocoa}}
 
<lang objc>NSString *text = @"Hello,How,Are,You,Today";
NSArray *tokens = [text componentsSeparatedByString:@","];
NSString *result = [tokens componentsJoinedByString:@"."];
NSLog(result);</lang>
 
=={{header|Objeck}}==
Line 937 ⟶ 926:
}
</lang>
 
=={{header|Objective-C}}==
{{works with|GNUstep}}
 
{{works with|Cocoa}}
 
<lang objc>NSString *text = @"Hello,How,Are,You,Today";
NSArray *tokens = [text componentsSeparatedByString:@","];
NSString *result = [tokens componentsJoinedByString:@"."];
NSLog(result);</lang>
 
=={{header|OCaml}}==
Line 1,020 ⟶ 1,019:
VIEW-AS ALERT-BOX.</lang>
Output:
---------------------------
<pre>
Message
---------------------------
---------------------------
Message
Hello.How.Are.You.Today
---------------------------
---------------------------
Hello.How.Are.You.Today
OK
---------------------------
---------------------------
OK
---------------------------
</pre>
 
=={{header|Oz}}==
Line 1,056 ⟶ 1,053:
 
The result is:
Hello
<lang pascal>
How
Hello
Are
How
You
Are
Today
You
Today
</lang>
'''Credits''': [http://code.google.com/p/lazsolutions/ silvioprog], 15:10, 8 January 2012 (UTC)
 
Line 1,071 ⟶ 1,066:
{{works with|Rakudo|#22 "Thousand Oaks"}}
<lang perl6>'Hello,How,Are,You,Today'.split(',').join('.').say;</lang>
 
=={{header|PicoLisp}}==
<lang PicoLisp>(mapcar pack
(split (chop "Hello,How,Are,You,Today") ",") )</lang>
=={{header|Pike}}==
<lang pike>("Hello,How,Are,You,Today" / ",") * ".";</lang>
 
=={{header|PHP}}==
Line 1,085 ⟶ 1,074:
echo implode('.', explode(',', $str));
?></lang>
 
=={{header|PicoLisp}}==
<lang PicoLisp>(mapcar pack
(split (chop "Hello,How,Are,You,Today") ",") )</lang>
 
=={{header|Pike}}==
<lang pike>("Hello,How,Are,You,Today" / ",") * ".";</lang>
 
=={{header|PL/I}}==
Line 1,257 ⟶ 1,253:
writeln(ABack).</lang>
Output:
<pre> ?- start.
Hello.How.Are.You.Today</pre>
 
=={{header|PureBasic}}==
Line 1,295 ⟶ 1,291:
 
<lang R>paste(unlist(strsplit(text, split=",")), collapse=".")</lang>
 
 
=={{header|Raven}}==
Line 1,307 ⟶ 1,302:
 
Output:
Original: Hello,How,Are,You,Today
 
<pre>Original Dotted: Hello,.How,.Are,.You,.Today.
Dotted: Hello.How.Are.You.Today.</pre>
 
 
=={{header|Retro}}==
Line 1,355 ⟶ 1,348:
say 'End-of-list.' /*Display a trailer for the list*/</lang>
Output:
input string=Hello,How,Are,You,Today
<pre>
input string=Hello,How,Are,You,Today
Words in the string:
 
Hello.
Words in the string:
How.
Hello.
Are.
How.
You.
Are.
Today.
You.
End-of-list.
Today.
End-of-list.
</pre>
 
===version 2===
Line 1,449 ⟶ 1,440:
 
Output:
<pre> Hello.How.Are.You.Today</pre>
 
=={{header|Standard ML}}==
Line 1,479 ⟶ 1,470:
 
<lang bash>echo 'Hello,How,Are,You,Today' | tr ',' '.'</lang>
 
=={{header|TUSCRIPT}}==
<lang tuscript>
$$ MODE TUSCRIPT
SET string="Hello,How,Are,You,Today"
SET string=SPLIT (string,":,:")
SET string=JOIN (string,".")
</lang>
 
=={{header|TXR}}==
Line 1,505 ⟶ 1,504:
<lang txr>@(bind result @(cat-str (split-str "Hello,How,Are,You,Today" ",") "."))</lang>
 
<pre> $ txr tok.txr
result="Hello.How.Are.You.Today"</pre>
 
=={{header|TUSCRIPT}}==
<lang tuscript>
$$ MODE TUSCRIPT
SET string="Hello,How,Are,You,Today"
SET string=SPLIT (string,":,:")
SET string=JOIN (string,".")
</lang>
 
=={{header|UnixPipes}}==
{{works with|Bourne Shell}}
<lang bash>token() {
(IFS=, read -r A B; echo "$A".; test -n "$B" && (echo "$B" | token))
}
 
echo "Hello,How,Are,You" | token</lang>
 
=={{header|UNIX Shell}}==
Line 1,598 ⟶ 1,581:
<lang bash> strtokenize "Hello,How,Are,You,Today" "," "."
Hello.How.Are.You.Today </lang>
 
=={{header|UnixPipes}}==
{{works with|Bourne Shell}}
<lang bash>token() {
(IFS=, read -r A B; echo "$A".; test -n "$B" && (echo "$B" | token))
}
 
echo "Hello,How,Are,You" | token</lang>
 
=={{header|Ursala}}==
Line 1,612 ⟶ 1,603:
 
main = mat`. token_list</lang>
Output:
output:
<pre> 'Hello.How.Are.You.Today'</pre>
 
=={{header|Vala}}==
<lang vala>// declare test string
// declare test string
string s = "Hello,How,Are,You,Today";
// create array of strings, could use var words instead if desired
string[] words = s.split(",");
// create string by joining array of strings with .
string joined = string.joinv(".", words);</lang>
</lang>
 
=={{header|VBScript}}==
====One liner====
<lang vb>wscriptWScript.echoEcho Join( Split( "Hello,How,Are,You,Today", "," ), "." )</lang>
 
In fact, the following Visual Basic solution (below) could have done the same, as Join() is available.
 
=={{header|Visual Basic}}==
{{trans|PowerBASIC}}
 
Unlike PowerBASIC, there is no need to know beforehand how many tokens are in the string -- <code>Split</code> automagically builds the array for you.
 
<lang vb>Sub Main()
Dim parseMe As String, parsed As Variant
parseMe = "Hello,How,Are,You,Today"
 
parsed = Split(parseMe, ",")
 
Dim L0 As Long, outP As String
outP = parsed(0)
For L0 = 1 To UBound(parsed)
outP = outP & "." & parsed(L0)
Next
 
MsgBox outP
End Sub</lang>
 
=={{header|Vedit macro language}}==
Line 1,677 ⟶ 1,646:
 
Buf_Quit(OK)</lang>
 
=={{header|Visual Basic}}==
{{trans|PowerBASIC}}
 
Unlike PowerBASIC, there is no need to know beforehand how many tokens are in the string -- <code>Split</code> automagically builds the array for you.
 
<lang vb>Sub Main()
Dim parseMe As String, parsed As Variant
parseMe = "Hello,How,Are,You,Today"
 
parsed = Split(parseMe, ",")
 
Dim L0 As Long, outP As String
outP = parsed(0)
For L0 = 1 To UBound(parsed)
outP = outP & "." & parsed(L0)
Next
 
MsgBox outP
End Sub</lang>
 
=={{header|Zsh}}==
Line 1,687 ⟶ 1,676:
<lang zsh>str='Hello,How,Are,You,Today'
IFS=, echo ${(j:.:)${=str}}</lang>
 
 
{{omit from|PARI/GP|No real capacity for string manipulation}}
1,150

edits