Tokenize a string: Difference between revisions
Content added Content deleted
m (→{{header|Picat}}: code tags) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 15: | Line 15: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">V text = ‘Hello,How,Are,You,Today’ |
||
V tokens = text.split(‘,’) |
V tokens = text.split(‘,’) |
||
print(tokens.join(‘.’))</ |
print(tokens.join(‘.’))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 25: | Line 25: | ||
=={{header|360 Assembly}}== |
=={{header|360 Assembly}}== |
||
< |
<syntaxhighlight lang="360asm">* Tokenize a string - 08/06/2018 |
||
TOKSTR CSECT |
TOKSTR CSECT |
||
USING TOKSTR,R13 base register |
USING TOKSTR,R13 base register |
||
Line 107: | Line 107: | ||
PG DC CL80' ' buffer |
PG DC CL80' ' buffer |
||
YREGS |
YREGS |
||
END TOKSTR</ |
END TOKSTR</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 114: | Line 114: | ||
=={{header|8080 Assembly}}== |
=={{header|8080 Assembly}}== |
||
< |
<syntaxhighlight lang="8080asm">puts: equ 9 |
||
org 100h |
org 100h |
||
jmp demo |
jmp demo |
||
Line 161: | Line 161: | ||
period: db '. $' |
period: db '. $' |
||
hello: db 'Hello,How,Are,You,Today$' |
hello: db 'Hello,How,Are,You,Today$' |
||
parts: equ $</ |
parts: equ $</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hello. How. Are. You. Today.</pre> |
<pre>Hello. How. Are. You. Today.</pre> |
||
=={{header|8086 Assembly}}== |
=={{header|8086 Assembly}}== |
||
< |
<syntaxhighlight lang="asm"> cpu 8086 |
||
org 100h |
org 100h |
||
section .text |
section .text |
||
Line 203: | Line 203: | ||
hello: db 'Hello,How,Are,You,Today$' |
hello: db 'Hello,How,Are,You,Today$' |
||
section .bss |
section .bss |
||
parts: resw 10</ |
parts: resw 10</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hello. How. Are. You. Today. </pre> |
<pre>Hello. How. Are. You. Today. </pre> |
||
Line 209: | Line 209: | ||
=={{header|AArch64 Assembly}}== |
=={{header|AArch64 Assembly}}== |
||
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
||
<syntaxhighlight lang="aarch64 assembly"> |
|||
<lang AArch64 Assembly> |
|||
/* ARM assembly AARCH64 Raspberry PI 3B */ |
/* ARM assembly AARCH64 Raspberry PI 3B */ |
||
/* program strTokenize64.s */ |
/* program strTokenize64.s */ |
||
Line 349: | Line 349: | ||
/* for this file see task include a file in language AArch64 assembly */ |
/* for this file see task include a file in language AArch64 assembly */ |
||
.include "../includeARM64.inc" |
.include "../includeARM64.inc" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Output}} |
{{Output}} |
||
<pre> |
<pre> |
||
Line 361: | Line 361: | ||
=={{header|ACL2}}== |
=={{header|ACL2}}== |
||
< |
<syntaxhighlight lang="lisp">(defun split-at (xs delim) |
||
(if (or (endp xs) (eql (first xs) delim)) |
(if (or (endp xs) (eql (first xs) delim)) |
||
(mv nil (rest xs)) |
(mv nil (rest xs)) |
||
Line 389: | Line 389: | ||
(progn$ (cw (first strs)) |
(progn$ (cw (first strs)) |
||
(cw (coerce (list delim) 'string)) |
(cw (coerce (list delim) 'string)) |
||
(print-with (rest strs) delim))))</ |
(print-with (rest strs) delim))))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 398: | Line 398: | ||
The user must type in the monitor the following command after compilation and before running the program!<pre>SET EndProg=*</pre> |
The user must type in the monitor the following command after compilation and before running the program!<pre>SET EndProg=*</pre> |
||
{{libheader|Action! Tool Kit}} |
{{libheader|Action! Tool Kit}} |
||
< |
<syntaxhighlight lang="action!">CARD EndProg ;required for ALLOCATE.ACT |
||
INCLUDE "D2:ALLOCATE.ACT" ;from the Action! Tool Kit. You must type 'SET EndProg=*' from the monitor after compiling, but before running this program! |
INCLUDE "D2:ALLOCATE.ACT" ;from the Action! Tool Kit. You must type 'SET EndProg=*' from the monitor after compiling, but before running this program! |
||
Line 491: | Line 491: | ||
Clear(items,@count) |
Clear(items,@count) |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Tokenize_a_string.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Tokenize_a_string.png Screenshot from Atari 8-bit computer] |
||
Line 506: | Line 506: | ||
=={{header|ActionScript}}== |
=={{header|ActionScript}}== |
||
< |
<syntaxhighlight lang="actionscript">var hello:String = "Hello,How,Are,You,Today"; |
||
var tokens:Array = hello.split(","); |
var tokens:Array = hello.split(","); |
||
trace(tokens.join(".")); |
trace(tokens.join(".")); |
||
// Or as a one-liner |
// Or as a one-liner |
||
trace("Hello,How,Are,You,Today".split(",").join("."));</ |
trace("Hello,How,Are,You,Today".split(",").join("."));</syntaxhighlight> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO, Ada.Containers.Indefinite_Vectors, Ada.Strings.Fixed, Ada.Strings.Maps; |
||
use Ada.Text_IO, Ada.Containers, Ada.Strings, Ada.Strings.Fixed, Ada.Strings.Maps; |
use Ada.Text_IO, Ada.Containers, Ada.Strings, Ada.Strings.Fixed, Ada.Strings.Maps; |
||
Line 534: | Line 534: | ||
Put (S & "."); |
Put (S & "."); |
||
end loop; |
end loop; |
||
end Tokenize;</ |
end Tokenize;</syntaxhighlight> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
< |
<syntaxhighlight lang="algol68">main:( |
||
OP +:= = (REF FLEX[]STRING in out, STRING item)VOID:( |
OP +:= = (REF FLEX[]STRING in out, STRING item)VOID:( |
||
Line 585: | Line 585: | ||
printf(($g"."$, string split(beetles, ", "),$l$)); |
printf(($g"."$, string split(beetles, ", "),$l$)); |
||
printf(($g"."$, char split(beetles, ", "),$l$)) |
printf(($g"."$, char split(beetles, ", "),$l$)) |
||
)</ |
)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 603: | Line 603: | ||
Note: the "splitnumber" macro cannot separate a number converted to a string by the "XTOSTR" function, because this function "rounds" the number to the decimal position by default. |
Note: the "splitnumber" macro cannot separate a number converted to a string by the "XTOSTR" function, because this function "rounds" the number to the decimal position by default. |
||
<syntaxhighlight lang="hopper"> |
|||
<lang Hopper> |
|||
#include <hopper.h> |
#include <hopper.h> |
||
Line 668: | Line 668: | ||
back |
back |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre>Output: |
<pre>Output: |
||
Line 688: | Line 688: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
< |
<syntaxhighlight lang="apl"> '.',⍨¨ ','(≠⊆⊢)'abc,123,X' ⍝ [1] Do the split: ','(≠⊆⊢)'abc,123,X'; [2] append the periods: '.',⍨¨ |
||
abc. 123. X. ⍝ 3 strings (char vectors), each with a period at the end. |
abc. 123. X. ⍝ 3 strings (char vectors), each with a period at the end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
< |
<syntaxhighlight lang="applescript">on run |
||
intercalate(".", splitOn(",", "Hello,How,Are,You,Today")) |
intercalate(".", splitOn(",", "Hello,How,Are,You,Today")) |
||
end run |
end run |
||
Line 713: | Line 713: | ||
set my text item delimiters to dlm |
set my text item delimiters to dlm |
||
return strJoined |
return strJoined |
||
end intercalate</ |
end intercalate</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Hello.How.Are.You.Today</pre> |
<pre>Hello.How.Are.You.Today</pre> |
||
Line 719: | Line 719: | ||
Or, |
Or, |
||
< |
<syntaxhighlight lang="applescript">set my text item delimiters to "," |
||
set tokens to the text items of "Hello,How,Are,You,Today" |
set tokens to the text items of "Hello,How,Are,You,Today" |
||
set my text item delimiters to "." |
set my text item delimiters to "." |
||
log tokens as text</ |
log tokens as text</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 731: | Line 731: | ||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |
||
{{works with|as|Raspberry Pi}} |
{{works with|as|Raspberry Pi}} |
||
<syntaxhighlight lang="arm assembly"> |
|||
<lang ARM Assembly> |
|||
/* ARM assembly Raspberry PI */ |
/* ARM assembly Raspberry PI */ |
||
Line 895: | Line 895: | ||
bx lr |
bx lr |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">str: "Hello,How,Are,You,Today" |
||
print join.with:"." split.by:"," str</ |
print join.with:"." split.by:"," str</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 907: | Line 907: | ||
=={{header|Astro}}== |
=={{header|Astro}}== |
||
< |
<syntaxhighlight lang="python">let text = 'Hello,How,Are,You,Today' |
||
let tokens = text.split(||,||) |
let tokens = text.split(||,||) |
||
print tokens.join(with: '.')</ |
print tokens.join(with: '.')</syntaxhighlight> |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">string := "Hello,How,Are,You,Today" |
||
stringsplit, string, string, `, |
stringsplit, string, string, `, |
||
loop, % string0 |
loop, % string0 |
||
{ |
{ |
||
msgbox % string%A_Index% |
msgbox % string%A_Index% |
||
}</ |
}</syntaxhighlight> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
< |
<syntaxhighlight lang="awk">BEGIN { |
||
s = "Hello,How,Are,You,Today" |
s = "Hello,How,Are,You,Today" |
||
split(s, arr, ",") |
split(s, arr, ",") |
||
Line 928: | Line 928: | ||
} |
} |
||
print |
print |
||
}</ |
}</syntaxhighlight> |
||
A more ''idiomatic'' way for AWK is |
A more ''idiomatic'' way for AWK is |
||
< |
<syntaxhighlight lang="awk">BEGIN { FS = "," } |
||
{ |
{ |
||
for(i=1; i <= NF; i++) printf $i "."; |
for(i=1; i <= NF; i++) printf $i "."; |
||
print "" |
print "" |
||
}</ |
}</syntaxhighlight> |
||
which "tokenize" each line of input and this is achieved by using "," as field separator |
which "tokenize" each line of input and this is achieved by using "," as field separator |
||
Line 942: | Line 942: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
==={{header|Applesoft BASIC}}=== |
==={{header|Applesoft BASIC}}=== |
||
< |
<syntaxhighlight lang="applesoftbasic">100 T$ = "HELLO,HOW,ARE,YOU,TODAY" |
||
110 GOSUB 200"TOKENIZE |
110 GOSUB 200"TOKENIZE |
||
120 FOR I = 1 TO N |
120 FOR I = 1 TO N |
||
Line 961: | Line 961: | ||
290 A$(N) = A$(N) + C$ |
290 A$(N) = A$(N) + C$ |
||
300 NEXT TI |
300 NEXT TI |
||
310 RETURN</ |
310 RETURN</syntaxhighlight> |
||
==={{header|BaCon}}=== |
==={{header|BaCon}}=== |
||
BaCon includes extensive support for ''delimited strings''. |
BaCon includes extensive support for ''delimited strings''. |
||
< |
<syntaxhighlight lang="bacon">OPTION BASE 1 |
||
string$ = "Hello,How,Are,You,Today" |
string$ = "Hello,How,Are,You,Today" |
||
Line 976: | Line 976: | ||
' Or simply replace the delimiter |
' Or simply replace the delimiter |
||
PRINT DELIM$(string$, ",", ".")</ |
PRINT DELIM$(string$, ",", ".")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 984: | Line 984: | ||
==={{header|BASIC256}}=== |
==={{header|BASIC256}}=== |
||
< |
<syntaxhighlight lang="basic256">instring$ = "Hello,How,Are,You,Today" |
||
tokens$ = explode(instring$,",") |
tokens$ = explode(instring$,",") |
||
Line 990: | Line 990: | ||
print tokens$[i]; "."; |
print tokens$[i]; "."; |
||
next i |
next i |
||
end</ |
end</syntaxhighlight> |
||
==={{header|BBC BASIC}}=== |
==={{header|BBC BASIC}}=== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
< |
<syntaxhighlight lang="bbcbasic"> INSTALL @lib$+"STRINGLIB" |
||
text$ = "Hello,How,Are,You,Today" |
text$ = "Hello,How,Are,You,Today" |
||
Line 1,002: | Line 1,002: | ||
PRINT array$(i%) "." ; |
PRINT array$(i%) "." ; |
||
NEXT |
NEXT |
||
PRINT</ |
PRINT</syntaxhighlight> |
||
==={{header|Commodore BASIC}}=== |
==={{header|Commodore BASIC}}=== |
||
Based on the AppleSoft BASIC version. |
Based on the AppleSoft BASIC version. |
||
< |
<syntaxhighlight lang="commodorebasic"> |
||
10 REM TOKENIZE A STRING ... ROSETTACODE.ORG |
10 REM TOKENIZE A STRING ... ROSETTACODE.ORG |
||
20 T$ = "HELLO,HOW,ARE,YOU,TODAY" |
20 T$ = "HELLO,HOW,ARE,YOU,TODAY" |
||
Line 1,024: | Line 1,024: | ||
270 NEXT L |
270 NEXT L |
||
280 RETURN |
280 RETURN |
||
</syntaxhighlight> |
|||
</lang> |
|||
==={{header|FreeBASIC}}=== |
==={{header|FreeBASIC}}=== |
||
< |
<syntaxhighlight lang="freebasic">sub tokenize( instring as string, tokens() as string, sep as string ) |
||
redim tokens(0 to 0) as string |
redim tokens(0 to 0) as string |
||
dim as string*1 ch |
dim as string*1 ch |
||
Line 1,047: | Line 1,047: | ||
for i as uinteger = 0 to ubound(tokens) |
for i as uinteger = 0 to ubound(tokens) |
||
print tokens(i);"."; |
print tokens(i);"."; |
||
next i</ |
next i</syntaxhighlight> |
||
==={{header|Liberty BASIC}}=== |
==={{header|Liberty BASIC}}=== |
||
< |
<syntaxhighlight lang="lb">'Note that Liberty Basic's array usage can reach element #10 before having to DIM the array |
||
For i = 0 To 4 |
For i = 0 To 4 |
||
array$(i) = Word$("Hello,How,Are,You,Today", (i + 1), ",") |
array$(i) = Word$("Hello,How,Are,You,Today", (i + 1), ",") |
||
Line 1,056: | Line 1,056: | ||
Next i |
Next i |
||
Print Left$(array$, (Len(array$) - 1))</ |
Print Left$(array$, (Len(array$) - 1))</syntaxhighlight> |
||
==={{header|PowerBASIC}}=== |
==={{header|PowerBASIC}}=== |
||
Line 1,062: | Line 1,062: | ||
PowerBASIC has a few keywords that make parsing strings trivial: <code>PARSE</code>, <code>PARSE$</code>, and <code>PARSECOUNT</code>. (<code>PARSE$</code>, not shown here, is for extracting tokens one at a time, while <code>PARSE</code> extracts all tokens at once into an array. <code>PARSECOUNT</code> returns the number of tokens found.) |
PowerBASIC has a few keywords that make parsing strings trivial: <code>PARSE</code>, <code>PARSE$</code>, and <code>PARSECOUNT</code>. (<code>PARSE$</code>, not shown here, is for extracting tokens one at a time, while <code>PARSE</code> extracts all tokens at once into an array. <code>PARSECOUNT</code> returns the number of tokens found.) |
||
< |
<syntaxhighlight lang="powerbasic">FUNCTION PBMAIN () AS LONG |
||
DIM parseMe AS STRING |
DIM parseMe AS STRING |
||
parseMe = "Hello,How,Are,You,Today" |
parseMe = "Hello,How,Are,You,Today" |
||
Line 1,076: | Line 1,076: | ||
MSGBOX outP |
MSGBOX outP |
||
END FUNCTION</ |
END FUNCTION</syntaxhighlight> |
||
==={{header|PureBasic}}=== |
==={{header|PureBasic}}=== |
||
'''As described |
'''As described |
||
< |
<syntaxhighlight lang="purebasic">NewList MyStrings.s() |
||
For i=1 To 5 |
For i=1 To 5 |
||
Line 1,090: | Line 1,090: | ||
ForEach MyStrings() |
ForEach MyStrings() |
||
Print(MyStrings()+".") |
Print(MyStrings()+".") |
||
Next</ |
Next</syntaxhighlight> |
||
'''Still, easier would be |
'''Still, easier would be |
||
< |
<syntaxhighlight lang="purebasic">Print(ReplaceString("Hello,How,Are,You,Today",",","."))</syntaxhighlight> |
||
==={{header|QBasic}}=== |
==={{header|QBasic}}=== |
||
< |
<syntaxhighlight lang="qbasic">DIM parseMe AS STRING |
||
parseMe = "Hello,How,Are,You,Today" |
parseMe = "Hello,How,Are,You,Today" |
||
Line 1,140: | Line 1,140: | ||
PRINT "."; parsed(L0); |
PRINT "."; parsed(L0); |
||
NEXT |
NEXT |
||
END IF</ |
END IF</syntaxhighlight> |
||
==={{header|Run BASIC}}=== |
==={{header|Run BASIC}}=== |
||
< |
<syntaxhighlight lang="runbasic">text$ = "Hello,How,Are,You,Today" |
||
FOR i = 1 to 5 |
FOR i = 1 to 5 |
||
textArray$(i) = word$(text$,i,",") |
textArray$(i) = word$(text$,i,",") |
||
print textArray$(i);" "; |
print textArray$(i);" "; |
||
NEXT</ |
NEXT</syntaxhighlight> |
||
==={{header|VBScript}}=== |
==={{header|VBScript}}=== |
||
====One liner==== |
====One liner==== |
||
< |
<syntaxhighlight lang="vb">WScript.Echo Join(Split("Hello,How,Are,You,Today", ","), ".")</syntaxhighlight> |
||
In fact, the Visual Basic solution (below) could have done the same, as Join() is available. |
In fact, the Visual Basic solution (below) could have done the same, as Join() is available. |
||
Line 1,160: | Line 1,160: | ||
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. |
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. |
||
< |
<syntaxhighlight lang="vb">Sub Main() |
||
Dim parseMe As String, parsed As Variant |
Dim parseMe As String, parsed As Variant |
||
parseMe = "Hello,How,Are,You,Today" |
parseMe = "Hello,How,Are,You,Today" |
||
Line 1,173: | Line 1,173: | ||
MsgBox outP |
MsgBox outP |
||
End Sub</ |
End Sub</syntaxhighlight> |
||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
< |
<syntaxhighlight lang="dos">@echo off |
||
setlocal enabledelayedexpansion |
setlocal enabledelayedexpansion |
||
call :tokenize %1 res |
call :tokenize %1 res |
||
Line 1,187: | Line 1,187: | ||
for %%i in (%str%) do set %2=!%2!.%%i |
for %%i in (%str%) do set %2=!%2!.%%i |
||
set %2=!%2:~1! |
set %2=!%2:~1! |
||
goto :eof</ |
goto :eof</syntaxhighlight> |
||
''Demo'' |
''Demo'' |
||
Line 1,195: | Line 1,195: | ||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
Solution that employs string pattern matching to spot the commas |
Solution that employs string pattern matching to spot the commas |
||
< |
<syntaxhighlight lang="bracmat">( "Hello,How,Are,You,Today":?String |
||
& :?ReverseList |
& :?ReverseList |
||
& whl |
& whl |
||
Line 1,207: | Line 1,207: | ||
) |
) |
||
& out$!List |
& out$!List |
||
)</ |
)</syntaxhighlight> |
||
Solution that starts by evaluating the input and employs the circumstance that the comma is a list constructing binary operator and that the string does not contain any other characters that are interpreted as operators on evaluation. |
Solution that starts by evaluating the input and employs the circumstance that the comma is a list constructing binary operator and that the string does not contain any other characters that are interpreted as operators on evaluation. |
||
< |
<syntaxhighlight lang="bracmat">( get$("Hello,How,Are,You,Today",MEM):?CommaseparatedList |
||
& :?ReverseList |
& :?ReverseList |
||
& whl |
& whl |
||
Line 1,221: | Line 1,221: | ||
) |
) |
||
& out$!List |
& out$!List |
||
)</ |
)</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
Line 1,230: | Line 1,230: | ||
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()''. |
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()''. |
||
< |
<syntaxhighlight lang="c">#include<string.h> |
||
#include<stdio.h> |
#include<stdio.h> |
||
#include<stdlib.h> |
#include<stdlib.h> |
||
Line 1,251: | Line 1,251: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
Another way to accomplish the task without the built-in string functions is to temporarily modify the separator character. This method does not need any additional memory, but requires the input string to be writeable. |
Another way to accomplish the task without the built-in string functions is to temporarily modify the separator character. This method does not need any additional memory, but requires the input string to be writeable. |
||
< |
<syntaxhighlight lang="c">#include<stdio.h> |
||
typedef void (*callbackfunc)(const char *); |
typedef void (*callbackfunc)(const char *); |
||
Line 1,279: | Line 1,279: | ||
tokenize(array, ',', doprint); |
tokenize(array, ',', doprint); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp">string str = "Hello,How,Are,You,Today"; |
||
// or Regex.Split ( "Hello,How,Are,You,Today", "," ); |
// or Regex.Split ( "Hello,How,Are,You,Today", "," ); |
||
// (Regex is in System.Text.RegularExpressions namespace) |
// (Regex is in System.Text.RegularExpressions namespace) |
||
string[] strings = str.Split(','); |
string[] strings = str.Split(','); |
||
Console.WriteLine(String.Join(".", strings)); |
Console.WriteLine(String.Join(".", strings)); |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 1,294: | Line 1,294: | ||
std::getline() is typically used to tokenize strings on a single-character delimiter |
std::getline() is typically used to tokenize strings on a single-character delimiter |
||
< |
<syntaxhighlight lang="cpp">#include <string> |
||
#include <sstream> |
#include <sstream> |
||
#include <vector> |
#include <vector> |
||
Line 1,309: | Line 1,309: | ||
copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, ".")); |
copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, ".")); |
||
std::cout << '\n'; |
std::cout << '\n'; |
||
}</ |
}</syntaxhighlight> |
||
{{works with|C++98}} |
{{works with|C++98}} |
||
C++ allows the user to redefine what is considered whitespace. If the delimiter is whitespace, tokenization becomes effortless. |
C++ allows the user to redefine what is considered whitespace. If the delimiter is whitespace, tokenization becomes effortless. |
||
< |
<syntaxhighlight lang="cpp">#include <string> |
||
#include <locale> |
#include <locale> |
||
#include <sstream> |
#include <sstream> |
||
Line 1,338: | Line 1,338: | ||
copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, ".")); |
copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, ".")); |
||
std::cout << '\n'; |
std::cout << '\n'; |
||
}</ |
}</syntaxhighlight> |
||
{{works with|C++98}} |
{{works with|C++98}} |
||
Line 1,344: | Line 1,344: | ||
The boost library has multiple options for easy tokenization. |
The boost library has multiple options for easy tokenization. |
||
< |
<syntaxhighlight lang="cpp">#include <string> |
||
#include <vector> |
#include <vector> |
||
#include <iterator> |
#include <iterator> |
||
Line 1,357: | Line 1,357: | ||
copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, ".")) |
copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, ".")) |
||
std::cout << '\n'; |
std::cout << '\n'; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Ceylon}}== |
=={{header|Ceylon}}== |
||
{{works with|Ceylon 1.2}} |
{{works with|Ceylon 1.2}} |
||
< |
<syntaxhighlight lang="ceylon">shared void tokenizeAString() { |
||
value input = "Hello,How,Are,You,Today"; |
value input = "Hello,How,Are,You,Today"; |
||
value tokens = input.split(','.equals); |
value tokens = input.split(','.equals); |
||
print(".".join(tokens)); |
print(".".join(tokens)); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|CFEngine}}== |
=={{header|CFEngine}}== |
||
< |
<syntaxhighlight lang="cfengine">bundle agent main |
||
{ |
{ |
||
reports: |
reports: |
||
"${with}" with => join(".", splitstring("Hello,How,Are,You,Today", ",", 99)); |
"${with}" with => join(".", splitstring("Hello,How,Are,You,Today", ",", 99)); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>cf-agent -KIf ./tokenize-a-string.cf |
<pre>cf-agent -KIf ./tokenize-a-string.cf |
||
Line 1,382: | Line 1,382: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Using native Clojure functions and Java Interop: |
Using native Clojure functions and Java Interop: |
||
< |
<syntaxhighlight lang="clojure">(apply str (interpose "." (.split #"," "Hello,How,Are,You,Today")))</syntaxhighlight> |
||
Using the clojure.string library: |
Using the clojure.string library: |
||
< |
<syntaxhighlight lang="clojure">(clojure.string/join "." (clojure.string/split "Hello,How,Are,You,Today" #","))</syntaxhighlight> |
||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
< |
<syntaxhighlight lang="clu">% This iterator splits the string on a given character, |
||
% and returns each substring in order. |
% and returns each substring in order. |
||
tokenize = iter (s: string, c: char) yields (string) |
tokenize = iter (s: string, c: char) yields (string) |
||
Line 1,410: | Line 1,410: | ||
stream$putl(po, part || ".") |
stream$putl(po, part || ".") |
||
end |
end |
||
end start_up</ |
end start_up</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hello. |
<pre>Hello. |
||
Line 1,421: | Line 1,421: | ||
This can be made to handle more complex cases; UNSTRING allows multiple delimiters, capture of which delimiter was used for each field, a POINTER for starting position (set on ending), along with match TALLYING. |
This can be made to handle more complex cases; UNSTRING allows multiple delimiters, capture of which delimiter was used for each field, a POINTER for starting position (set on ending), along with match TALLYING. |
||
<syntaxhighlight lang="cobol"> |
|||
<lang COBOL> |
|||
identification division. |
identification division. |
||
program-id. tokenize. |
program-id. tokenize. |
||
Line 1,452: | Line 1,452: | ||
goback. |
goback. |
||
end program tokenize. |
end program tokenize. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,462: | Line 1,462: | ||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |
||
< |
<syntaxhighlight lang="coffeescript"> |
||
arr = "Hello,How,Are,You,Today".split "," |
arr = "Hello,How,Are,You,Today".split "," |
||
console.log arr.join "." |
console.log arr.join "." |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|ColdFusion}}== |
=={{header|ColdFusion}}== |
||
=== Classic tag based CFML === |
=== Classic tag based CFML === |
||
< |
<syntaxhighlight lang="cfm"> |
||
<cfoutput> |
<cfoutput> |
||
<cfset wordListTag = "Hello,How,Are,You,Today"> |
<cfset wordListTag = "Hello,How,Are,You,Today"> |
||
#Replace( wordListTag, ",", ".", "all" )# |
#Replace( wordListTag, ",", ".", "all" )# |
||
</cfoutput> |
</cfoutput> |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Output}} |
{{Output}} |
||
<pre> |
<pre> |
||
Line 1,481: | Line 1,481: | ||
=== Script Based CFML === |
=== Script Based CFML === |
||
< |
<syntaxhighlight lang="cfm"><cfscript> |
||
wordList = "Hello,How,Are,You,Today"; |
wordList = "Hello,How,Are,You,Today"; |
||
splitList = replace( wordList, ",", ".", "all" ); |
splitList = replace( wordList, ",", ".", "all" ); |
||
writeOutput( splitList ); |
writeOutput( splitList ); |
||
</cfscript></ |
</cfscript></syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
<pre> |
<pre> |
||
Line 1,495: | Line 1,495: | ||
There are libraries out there that handle splitting (e.g., [http://www.cliki.net/SPLIT-SEQUENCE SPLIT-SEQUENCE], and the more-general [http://weitz.de/cl-ppcre/ CL-PPCRE]), but this is a simple one-off, too. When the words are written with write-with-periods, there is no final period after the last word. |
There are libraries out there that handle splitting (e.g., [http://www.cliki.net/SPLIT-SEQUENCE SPLIT-SEQUENCE], and the more-general [http://weitz.de/cl-ppcre/ CL-PPCRE]), but this is a simple one-off, too. When the words are written with write-with-periods, there is no final period after the last word. |
||
< |
<syntaxhighlight lang="lisp">(defun comma-split (string) |
||
(loop for start = 0 then (1+ finish) |
(loop for start = 0 then (1+ finish) |
||
for finish = (position #\, string :start start) |
for finish = (position #\, string :start start) |
||
Line 1,502: | Line 1,502: | ||
(defun write-with-periods (strings) |
(defun write-with-periods (strings) |
||
(format t "~{~A~^.~}" strings))</ |
(format t "~{~A~^.~}" strings))</syntaxhighlight> |
||
=={{header|Cowgol}}== |
=={{header|Cowgol}}== |
||
< |
<syntaxhighlight lang="cowgol">include "cowgol.coh"; |
||
include "strings.coh"; |
include "strings.coh"; |
||
Line 1,544: | Line 1,544: | ||
print(".\n"); |
print(".\n"); |
||
i := i + 1; |
i := i + 1; |
||
end loop;</ |
end loop;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hello. |
<pre>Hello. |
||
Line 1,553: | Line 1,553: | ||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
< |
<syntaxhighlight lang="crystal">puts "Hello,How,Are,You,Today".split(',').join('.')</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hello.How.Are.You.Today</pre> |
<pre>Hello.How.Are.You.Today</pre> |
||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">void main() { |
||
import std.stdio, std.string; |
import std.stdio, std.string; |
||
"Hello,How,Are,You,Today".split(',').join('.').writeln; |
"Hello,How,Are,You,Today".split(',').join('.').writeln; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hello.How.Are.You.Today</pre> |
<pre>Hello.How.Are.You.Today</pre> |
||
Line 1,569: | Line 1,569: | ||
=== Using String.split === |
=== Using String.split === |
||
{{libheader| System.SysUtils}} |
{{libheader| System.SysUtils}} |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
program Tokenize_a_string; |
program Tokenize_a_string; |
||
Line 1,587: | Line 1,587: | ||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=== Using TStringList === |
=== Using TStringList === |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
program TokenizeString; |
program TokenizeString; |
||
Line 1,623: | Line 1,623: | ||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
The result is: |
The result is: |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
Hello |
Hello |
||
How |
How |
||
Line 1,633: | Line 1,633: | ||
You |
You |
||
Today |
Today |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Dyalect}}== |
=={{header|Dyalect}}== |
||
< |
<syntaxhighlight lang="dyalect">var str = "Hello,How,Are,You,Today" |
||
var strings = str.Split(',') |
var strings = str.Split(',') |
||
print(values: strings, separator: ".")</ |
print(values: strings, separator: ".")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,646: | Line 1,646: | ||
=={{header|Déjà Vu}}== |
=={{header|Déjà Vu}}== |
||
< |
<syntaxhighlight lang="dejavu">!print join "." split "Hello,How,Are,You,Today" ","</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hello.How.Are.You.Today</pre> |
<pre>Hello.How.Are.You.Today</pre> |
||
=={{header|E}}== |
=={{header|E}}== |
||
< |
<syntaxhighlight lang="e">".".rjoin("Hello,How,Are,You,Today".split(","))</syntaxhighlight> |
||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 4.x: |
ELENA 4.x: |
||
< |
<syntaxhighlight lang="elena">import system'routines; |
||
import extensions; |
import extensions; |
||
Line 1,666: | Line 1,666: | ||
console.print(s,".") |
console.print(s,".") |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
< |
<syntaxhighlight lang="elixir"> |
||
tokens = String.split("Hello,How,Are,You,Today", ",") |
tokens = String.split("Hello,How,Are,You,Today", ",") |
||
IO.puts Enum.join(tokens, ".") |
IO.puts Enum.join(tokens, ".") |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
< |
<syntaxhighlight lang="erlang">-module(tok). |
||
-export([start/0]). |
-export([start/0]). |
||
Line 1,681: | Line 1,681: | ||
Lst = string:tokens("Hello,How,Are,You,Today",","), |
Lst = string:tokens("Hello,How,Are,You,Today",","), |
||
io:fwrite("~s~n", [string:join(Lst,".")]), |
io:fwrite("~s~n", [string:join(Lst,".")]), |
||
ok.</ |
ok.</syntaxhighlight> |
||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
< |
<syntaxhighlight lang="euphoria">function split(sequence s, integer c) |
||
sequence out |
sequence out |
||
integer first, delim |
integer first, delim |
||
Line 1,705: | Line 1,705: | ||
for i = 1 to length(s) do |
for i = 1 to length(s) do |
||
puts(1, s[i] & ',') |
puts(1, s[i] & ',') |
||
end for</ |
end for</syntaxhighlight> |
||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp">System.String.Join(".", "Hello,How,Are,You,Today".Split(','))</syntaxhighlight> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">"Hello,How,Are,You,Today" "," split "." join print</syntaxhighlight> |
||
=={{header|Falcon}}== |
=={{header|Falcon}}== |
||
'''VBA/Python programmer's approach to this solution, not sure if it's the most falconic way''' |
'''VBA/Python programmer's approach to this solution, not sure if it's the most falconic way''' |
||
< |
<syntaxhighlight lang="falcon"> |
||
/* created by Aykayayciti Earl Lamont Montgomery |
/* created by Aykayayciti Earl Lamont Montgomery |
||
April 9th, 2018 */ |
April 9th, 2018 */ |
||
Line 1,729: | Line 1,729: | ||
> b |
> b |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,740: | Line 1,740: | ||
A string can be split on a given character, returning a list of the intervening strings. |
A string can be split on a given character, returning a list of the intervening strings. |
||
< |
<syntaxhighlight lang="fantom"> |
||
class Main |
class Main |
||
{ |
{ |
||
Line 1,753: | Line 1,753: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Fennel}}== |
=={{header|Fennel}}== |
||
{{trans|Lua}} |
{{trans|Lua}} |
||
< |
<syntaxhighlight lang="fennel">(fn string.split [self sep] |
||
(let [pattern (string.format "([^%s]+)" sep) |
(let [pattern (string.format "([^%s]+)" sep) |
||
fields {}] |
fields {}] |
||
Line 1,764: | Line 1,764: | ||
(let [str "Hello,How,Are,You,Today"] |
(let [str "Hello,How,Are,You,Today"] |
||
(print (table.concat (str:split ",") ".")))</ |
(print (table.concat (str:split ",") ".")))</syntaxhighlight> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
There is no standard string split routine, but it is easily written. The results are saved temporarily to the dictionary. |
There is no standard string split routine, but it is easily written. The results are saved temporarily to the dictionary. |
||
< |
<syntaxhighlight lang="forth">: split ( str len separator len -- tokens count ) |
||
here >r 2swap |
here >r 2swap |
||
begin |
begin |
||
Line 1,786: | Line 1,786: | ||
1 ?do dup 2@ type ." ." cell+ cell+ loop 2@ type ; |
1 ?do dup 2@ type ." ." cell+ cell+ loop 2@ type ; |
||
s" Hello,How,Are,You,Today" s" ," split .tokens \ Hello.How.Are.You.Today</ |
s" Hello,How,Are,You,Today" s" ," split .tokens \ Hello.How.Are.You.Today</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|90 and later}} |
{{works with|Fortran|90 and later}} |
||
< |
<syntaxhighlight lang="fortran">PROGRAM Example |
||
CHARACTER(23) :: str = "Hello,How,Are,You,Today" |
CHARACTER(23) :: str = "Hello,How,Are,You,Today" |
||
Line 1,812: | Line 1,812: | ||
END DO |
END DO |
||
END PROGRAM Example</ |
END PROGRAM Example</syntaxhighlight> |
||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
< |
<syntaxhighlight lang="frink"> |
||
println[join[".", split[",", "Hello,How,Are,You,Today"]]] |
println[join[".", split[",", "Hello,How,Are,You,Today"]]] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Gambas}}== |
=={{header|Gambas}}== |
||
'''[https://gambas-playground.proko.eu/?gist=218e240236cdf1419a405abfed906ed3 Click this link to run this code]''' |
'''[https://gambas-playground.proko.eu/?gist=218e240236cdf1419a405abfed906ed3 Click this link to run this code]''' |
||
< |
<syntaxhighlight lang="gambas">Public Sub Main() |
||
Dim sString As String[] = Split("Hello,How,Are,You,Today") |
Dim sString As String[] = Split("Hello,How,Are,You,Today") |
||
Print sString.Join(".") |
Print sString.Join(".") |
||
End</ |
End</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,833: | Line 1,833: | ||
=={{header|GAP}}== |
=={{header|GAP}}== |
||
< |
<syntaxhighlight lang="gap">SplitString("Hello,How,Are,You,Today", ","); |
||
# [ "Hello", "How", "Are", "You", "Today" ] |
# [ "Hello", "How", "Are", "You", "Today" ] |
||
JoinStringsWithSeparator(last, "."); |
JoinStringsWithSeparator(last, "."); |
||
# "Hello.How.Are.You.Today"</ |
# "Hello.How.Are.You.Today"</syntaxhighlight> |
||
=={{header|Genie}}== |
=={{header|Genie}}== |
||
< |
<syntaxhighlight lang="genie">[indent=4] |
||
init |
init |
||
Line 1,846: | Line 1,846: | ||
words:array of string[] = str.split(",") |
words:array of string[] = str.split(",") |
||
joined:string = string.joinv(".", words) |
joined:string = string.joinv(".", words) |
||
print joined</ |
print joined</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,854: | Line 1,854: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,864: | Line 1,864: | ||
s := "Hello,How,Are,You,Today" |
s := "Hello,How,Are,You,Today" |
||
fmt.Println(strings.Join(strings.Split(s, ","), ".")) |
fmt.Println(strings.Join(strings.Split(s, ","), ".")) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
< |
<syntaxhighlight lang="groovy">println 'Hello,How,Are,You,Today'.split(',').join('.')</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
'''Using Data.Text''' |
'''Using Data.Text''' |
||
< |
<syntaxhighlight lang="haskell">{-# OPTIONS_GHC -XOverloadedStrings #-} |
||
import Data.Text (splitOn,intercalate) |
import Data.Text (splitOn,intercalate) |
||
import qualified Data.Text.IO as T (putStrLn) |
import qualified Data.Text.IO as T (putStrLn) |
||
main = T.putStrLn . intercalate "." $ splitOn "," "Hello,How,Are,You,Today"</ |
main = T.putStrLn . intercalate "." $ splitOn "," "Hello,How,Are,You,Today"</syntaxhighlight> |
||
Output: Hello.How.Are.You.Today |
Output: Hello.How.Are.You.Today |
||
Line 1,884: | Line 1,884: | ||
The necessary operations are unfortunately not in the standard library (yet), but simple to write: |
The necessary operations are unfortunately not in the standard library (yet), but simple to write: |
||
< |
<syntaxhighlight lang="haskell">splitBy :: (a -> Bool) -> [a] -> [[a]] |
||
splitBy _ [] = [] |
splitBy _ [] = [] |
||
splitBy f list = first : splitBy f (dropWhile f rest) where |
splitBy f list = first : splitBy f (dropWhile f rest) where |
||
Line 1,899: | Line 1,899: | ||
-- using regular expression to split: |
-- using regular expression to split: |
||
import Text.Regex |
import Text.Regex |
||
putStrLn $ joinWith "." $ splitRegex (mkRegex ",") $ "Hello,How,Are,You,Today"</ |
putStrLn $ joinWith "." $ splitRegex (mkRegex ",") $ "Hello,How,Are,You,Today"</syntaxhighlight> |
||
Tokenizing can also be realized by using unfoldr and break: |
Tokenizing can also be realized by using unfoldr and break: |
||
< |
<syntaxhighlight lang="haskell">*Main> mapM_ putStrLn $ takeWhile (not.null) $ unfoldr (Just . second(drop 1). break (==',')) "Hello,How,Are,You,Today" |
||
Hello |
Hello |
||
How |
How |
||
Are |
Are |
||
You |
You |
||
Today</ |
Today</syntaxhighlight> |
||
* You need to import the modules Data.List and Control.Arrow |
* You need to import the modules Data.List and Control.Arrow |
||
Line 1,913: | Line 1,913: | ||
=={{header|HicEst}}== |
=={{header|HicEst}}== |
||
< |
<syntaxhighlight lang="hicest">CHARACTER string="Hello,How,Are,You,Today", list |
||
nWords = INDEX(string, ',', 256) + 1 |
nWords = INDEX(string, ',', 256) + 1 |
||
Line 1,925: | Line 1,925: | ||
DO i = 1, nWords |
DO i = 1, nWords |
||
WRITE(APPend) TRIM(CHAR(i, maxWordLength, list)), '.' |
WRITE(APPend) TRIM(CHAR(i, maxWordLength, list)), '.' |
||
ENDDO</ |
ENDDO</syntaxhighlight> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
< |
<syntaxhighlight lang="icon">procedure main() |
||
A := [] |
A := [] |
||
"Hello,How,Are,You,Today" ? { |
"Hello,How,Are,You,Today" ? { |
||
Line 1,936: | Line 1,936: | ||
every writes(!A,".") |
every writes(!A,".") |
||
write() |
write() |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,946: | Line 1,946: | ||
A Unicon-specific solution is: |
A Unicon-specific solution is: |
||
< |
<syntaxhighlight lang="unicon">import util |
||
procedure main() |
procedure main() |
||
Line 1,952: | Line 1,952: | ||
every writes(!A,".") |
every writes(!A,".") |
||
write() |
write() |
||
end</ |
end</syntaxhighlight> |
||
One wonders what the expected output should be with the input string ",,,,". |
One wonders what the expected output should be with the input string ",,,,". |
||
=={{header|Io}}== |
=={{header|Io}}== |
||
< |
<syntaxhighlight lang="io">"Hello,How,Are,You,Today" split(",") join(".") println</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang="j"> s=: 'Hello,How,Are,You,Today' |
||
] t=: <;._1 ',',s |
] t=: <;._1 ',',s |
||
+-----+---+---+---+-----+ |
+-----+---+---+---+-----+ |
||
Line 1,969: | Line 1,969: | ||
'.' (I.','=s)}s NB. two steps combined |
'.' (I.','=s)}s NB. two steps combined |
||
Hello.How.Are.You.Today</ |
Hello.How.Are.You.Today</syntaxhighlight> |
||
Alternatively using the system library/script <tt>strings</tt> |
Alternatively using the system library/script <tt>strings</tt> |
||
< |
<syntaxhighlight lang="j"> require 'strings' |
||
',' splitstring s |
',' splitstring s |
||
+-----+---+---+---+-----+ |
+-----+---+---+---+-----+ |
||
Line 1,979: | Line 1,979: | ||
'.' joinstring ',' splitstring s |
'.' joinstring ',' splitstring s |
||
Hello.How.Are.You.Today</ |
Hello.How.Are.You.Today</syntaxhighlight> |
||
<tt>splitstring</tt> and <tt>joinstring</tt> also work with longer "delimiters": |
<tt>splitstring</tt> and <tt>joinstring</tt> also work with longer "delimiters": |
||
< |
<syntaxhighlight lang="j"> '"'([ ,~ ,) '","' joinstring ',' splitstring s |
||
"Hello","How","Are","You","Today"</ |
"Hello","How","Are","You","Today"</syntaxhighlight> |
||
But, of course, this could be solved with simple string replacement: |
But, of course, this could be solved with simple string replacement: |
||
< |
<syntaxhighlight lang="j"> rplc&',.' s |
||
Hello.How.Are.You.Today</ |
Hello.How.Are.You.Today</syntaxhighlight> |
||
The task asks us to ''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.'' but for many purposes the original string is an adequate data structure. Note also that given a string, a list of "word start" indices and "word length" integers can be logically equivalent to having an "array of words" -- and, depending on implementation details may be a superior or inferior choice to some other representation. But, in current definition of this task, the concept of "word length" plays no useful role. |
The task asks us to ''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.'' but for many purposes the original string is an adequate data structure. Note also that given a string, a list of "word start" indices and "word length" integers can be logically equivalent to having an "array of words" -- and, depending on implementation details may be a superior or inferior choice to some other representation. But, in current definition of this task, the concept of "word length" plays no useful role. |
||
Line 1,994: | Line 1,994: | ||
Note also that J provides several built-in concepts of parsing: split on leading delimiter, split on trailing delimiter, split J language words. Also, it's sometimes more efficient to append to a string than to prepend to it. So a common practice for parsing on an embedded delimiter is to append a copy of the delimiter to the string and then use the appended result: |
Note also that J provides several built-in concepts of parsing: split on leading delimiter, split on trailing delimiter, split J language words. Also, it's sometimes more efficient to append to a string than to prepend to it. So a common practice for parsing on an embedded delimiter is to append a copy of the delimiter to the string and then use the appended result: |
||
< |
<syntaxhighlight lang="j"> fn;._2 string,','</syntaxhighlight> |
||
Here '''fn''' is applied to each ',' delimited substring and the results are assembled into an array. |
Here '''fn''' is applied to each ',' delimited substring and the results are assembled into an array. |
||
Or, factoring out the names: |
Or, factoring out the names: |
||
< |
<syntaxhighlight lang="j"> fn ((;._2)(@(,&','))) string</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 2,010: | Line 2,010: | ||
{{works with|Java|1.8+}} |
{{works with|Java|1.8+}} |
||
< |
<syntaxhighlight lang="java5">String toTokenize = "Hello,How,Are,You,Today"; |
||
System.out.println(String.join(".", toTokenize.split(",")));</ |
System.out.println(String.join(".", toTokenize.split(",")));</syntaxhighlight> |
||
{{works with|Java|1.4+}} |
{{works with|Java|1.4+}} |
||
< |
<syntaxhighlight lang="java5">String toTokenize = "Hello,How,Are,You,Today"; |
||
String words[] = toTokenize.split(",");//splits on one comma, multiple commas yield multiple splits |
String words[] = toTokenize.split(",");//splits on one comma, multiple commas yield multiple splits |
||
Line 2,020: | Line 2,020: | ||
for(int i=0; i<words.length; i++) { |
for(int i=0; i<words.length; i++) { |
||
System.out.print(words[i] + "."); |
System.out.print(words[i] + "."); |
||
}</ |
}</syntaxhighlight> |
||
The other way is to use StringTokenizer. It will skip any empty tokens. So if two commas are given in line, there will be an empty string in the array given by the split function, but no empty string with the StringTokenizer object. This method takes more code to use, but allows you to get tokens incrementally instead of all at once. |
The other way is to use StringTokenizer. It will skip any empty tokens. So if two commas are given in line, there will be an empty string in the array given by the split function, but no empty string with the StringTokenizer object. This method takes more code to use, but allows you to get tokens incrementally instead of all at once. |
||
{{works with|Java|1.0+}} |
{{works with|Java|1.0+}} |
||
< |
<syntaxhighlight lang="java5">String toTokenize = "Hello,How,Are,You,Today"; |
||
StringTokenizer tokenizer = new StringTokenizer(toTokenize, ","); |
StringTokenizer tokenizer = new StringTokenizer(toTokenize, ","); |
||
while(tokenizer.hasMoreTokens()) { |
while(tokenizer.hasMoreTokens()) { |
||
System.out.print(tokenizer.nextToken() + "."); |
System.out.print(tokenizer.nextToken() + "."); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
{{works with|Firefox|2.0}} |
{{works with|Firefox|2.0}} |
||
< |
<syntaxhighlight lang="javascript">alert( "Hello,How,Are,You,Today".split(",").join(".") );</syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
< |
<syntaxhighlight lang="jq">split(",") | join(".")</syntaxhighlight>Example:<syntaxhighlight lang="sh">$ jq -r 'split(",") | join(".")' |
||
"Hello,How,Are,You,Today" |
"Hello,How,Are,You,Today" |
||
Hello.How.Are.You.Today</ |
Hello.How.Are.You.Today</syntaxhighlight> |
||
=={{header|Jsish}}== |
=={{header|Jsish}}== |
||
Being in the ECMAScript family, Jsi is blessed with many easy to use character, string and array manipulation routines. |
Being in the ECMAScript family, Jsi is blessed with many easy to use character, string and array manipulation routines. |
||
< |
<syntaxhighlight lang="javascript">puts('Hello,How,Are,You,Today'.split(',').join('.'))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hello.How.Are.You.Today</pre> |
<pre>Hello.How.Are.You.Today</pre> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
s = "Hello,How,Are,You,Today" |
s = "Hello,How,Are,You,Today" |
||
a = split(s, ",") |
a = split(s, ",") |
||
Line 2,058: | Line 2,058: | ||
println("Splits into ", a) |
println("Splits into ", a) |
||
println("Reconstitutes to \"", t, "\"") |
println("Reconstitutes to \"", t, "\"") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,068: | Line 2,068: | ||
=={{header|K}}== |
=={{header|K}}== |
||
< |
<syntaxhighlight lang="k">words: "," \: "Hello,How,Are,You,Today" |
||
"." /: words</ |
"." /: words</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,077: | Line 2,077: | ||
=={{header|Klingphix}}== |
=={{header|Klingphix}}== |
||
< |
<syntaxhighlight lang="klingphix">( "Hello,How,Are,You,Today" "," ) split len [ get print "." print ] for |
||
nl "End " input</ |
nl "End " input</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hello.How.Are.You.Today. |
<pre>Hello.How.Are.You.Today. |
||
Line 2,086: | Line 2,086: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{works with|Kotlin|1.0b4}} |
{{works with|Kotlin|1.0b4}} |
||
< |
<syntaxhighlight lang="scala">fun main(args: Array<String>) { |
||
val input = "Hello,How,Are,You,Today" |
val input = "Hello,How,Are,You,Today" |
||
println(input.split(',').joinToString(".")) |
println(input.split(',').joinToString(".")) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hello.How.Are.You.Today</pre> |
<pre>Hello.How.Are.You.Today</pre> |
||
=={{header|Ksh}}== |
=={{header|Ksh}}== |
||
< |
<syntaxhighlight lang="ksh"> |
||
#!/bin/ksh |
#!/bin/ksh |
||
Line 2,125: | Line 2,125: | ||
###### |
###### |
||
_tokenize "${string}" "${inputdelim}" "${outputdelim}"</ |
_tokenize "${string}" "${inputdelim}" "${outputdelim}"</syntaxhighlight> |
||
{{out}}<pre>Hello.How.Are.You.Today</pre> |
{{out}}<pre>Hello.How.Are.You.Today</pre> |
||
Line 2,133: | Line 2,133: | ||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
{S.replace , by . in Hello,How,Are,You,Today}. |
{S.replace , by . in Hello,How,Are,You,Today}. |
||
-> Hello.How.Are.You.Today. |
-> Hello.How.Are.You.Today. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lang5}}== |
=={{header|Lang5}}== |
||
< |
<syntaxhighlight lang="lang5">'Hello,How,Are,You,Today ', split '. join .</syntaxhighlight> |
||
=={{header|LDPL}}== |
=={{header|LDPL}}== |
||
< |
<syntaxhighlight lang="ldpl"> |
||
DATA: |
DATA: |
||
explode/words is text vector |
explode/words is text vector |
||
Line 2,194: | Line 2,194: | ||
add 1 and i in i |
add 1 and i in i |
||
repeat |
repeat |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|LFE}}== |
=={{header|LFE}}== |
||
< |
<syntaxhighlight lang="lisp"> |
||
> (set split (string:tokens "Hello,How,Are,You,Today" ",")) |
> (set split (string:tokens "Hello,How,Are,You,Today" ",")) |
||
("Hello" "How" "Are" "You" "Today") |
("Hello" "How" "Are" "You" "Today") |
||
> (string:join split ".") |
> (string:join split ".") |
||
"Hello.How.Are.You.Today" |
"Hello.How.Are.You.Today" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lingo}}== |
=={{header|Lingo}}== |
||
< |
<syntaxhighlight lang="lingo">input = "Hello,How,Are,You,Today" |
||
_player.itemDelimiter = "," |
_player.itemDelimiter = "," |
||
output = "" |
output = "" |
||
Line 2,214: | Line 2,214: | ||
delete the last char of output |
delete the last char of output |
||
put output |
put output |
||
-- "Hello.How.Are.You.Today"</ |
-- "Hello.How.Are.You.Today"</syntaxhighlight> |
||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
{{works with|UCB Logo}} |
{{works with|UCB Logo}} |
||
< |
<syntaxhighlight lang="logo">to split :str :sep |
||
output parse map [ifelse ? = :sep ["| |] [?]] :str |
output parse map [ifelse ? = :sep ["| |] [?]] :str |
||
end</ |
end</syntaxhighlight> |
||
This form is more robust, doing the right thing if there are embedded spaces. |
This form is more robust, doing the right thing if there are embedded spaces. |
||
< |
<syntaxhighlight lang="logo">to split :str :by [:acc []] [:w "||] |
||
if empty? :str [output lput :w :acc] |
if empty? :str [output lput :w :acc] |
||
ifelse equal? first :str :by ~ |
ifelse equal? first :str :by ~ |
||
[output (split butfirst :str :by lput :w :acc)] ~ |
[output (split butfirst :str :by lput :w :acc)] ~ |
||
[output (split butfirst :str :by :acc lput first :str :w)] |
[output (split butfirst :str :by :acc lput first :str :w)] |
||
end</ |
end</syntaxhighlight> |
||
< |
<syntaxhighlight lang="logo">? show split "Hello,How,Are,You,Today ", |
||
[Hello How Are You Today]</ |
[Hello How Are You Today]</syntaxhighlight> |
||
=={{header|Logtalk}}== |
=={{header|Logtalk}}== |
||
Using Logtalk built-in support for Definite Clause Grammars (DCGs) and representing the strings as atoms for readbility: |
Using Logtalk built-in support for Definite Clause Grammars (DCGs) and representing the strings as atoms for readbility: |
||
< |
<syntaxhighlight lang="logtalk"> |
||
:- object(spliting). |
:- object(spliting). |
||
Line 2,258: | Line 2,258: | ||
:- end_object. |
:- end_object. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,268: | Line 2,268: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Split function callously stolen from the lua-users wiki |
Split function callously stolen from the lua-users wiki |
||
< |
<syntaxhighlight lang="lua">function string:split (sep) |
||
local sep, fields = sep or ":", {} |
local sep, fields = sep or ":", {} |
||
local pattern = string.format("([^%s]+)", sep) |
local pattern = string.format("([^%s]+)", sep) |
||
Line 2,276: | Line 2,276: | ||
local str = "Hello,How,Are,You,Today" |
local str = "Hello,How,Are,You,Today" |
||
print(table.concat(str:split(","), "."))</ |
print(table.concat(str:split(","), "."))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hello.How.Are.You.Today</pre> |
<pre>Hello.How.Are.You.Today</pre> |
||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
<lang M2000 Interpreter> |
|||
Module CheckIt { |
Module CheckIt { |
||
Function Tokenize$(s){ |
Function Tokenize$(s){ |
||
Line 2,294: | Line 2,294: | ||
} |
} |
||
Checkit |
Checkit |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|M4}}== |
=={{header|M4}}== |
||
< |
<syntaxhighlight lang="m4">define(`s',`Hello,How,Are,You,Today') |
||
define(`set',`define(`$1[$2]',`$3')') |
define(`set',`define(`$1[$2]',`$3')') |
||
define(`get',`defn($1[$2])') |
define(`get',`defn($1[$2])') |
||
Line 2,307: | Line 2,307: | ||
define(`show', |
define(`show', |
||
`ifelse(eval(j<n),1,`get(a,j).`'define(`j',incr(j))`'show')') |
`ifelse(eval(j<n),1,`get(a,j).`'define(`j',incr(j))`'show')') |
||
show</ |
show</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,315: | Line 2,315: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">StringTools:-Join(StringTools:-Split("Hello,How,Are,You,Today", ","),".");</syntaxhighlight> |
||
{{Out|Output}} |
{{Out|Output}} |
||
<pre>"Hello.How.Are.You.Today"</pre> |
<pre>"Hello.How.Are.You.Today"</pre> |
||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">StringJoin@StringSplit["Hello,How,Are,You,Today", "," -> "."]</syntaxhighlight> |
||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
<syntaxhighlight lang="matlab"> |
|||
<lang Matlab> |
|||
s=strsplit('Hello,How,Are,You,Today',',') |
s=strsplit('Hello,How,Are,You,Today',',') |
||
fprintf(1,'%s.',s{:}) |
fprintf(1,'%s.',s{:}) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,334: | Line 2,334: | ||
=={{header|Maxima}}== |
=={{header|Maxima}}== |
||
< |
<syntaxhighlight lang="maxima">l: split("Hello,How,Are,You,Today", ",")$ |
||
printf(true, "~{~a~^.~}~%", l)$</ |
printf(true, "~{~a~^.~}~%", l)$</syntaxhighlight> |
||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
< |
<syntaxhighlight lang="maxscript">output = "" |
||
for word in (filterString "Hello,How,Are,You,Today" ",") do |
for word in (filterString "Hello,How,Are,You,Today" ",") do |
||
( |
( |
||
output += (word + ".") |
output += (word + ".") |
||
) |
) |
||
format "%\n" output</ |
format "%\n" output</syntaxhighlight> |
||
=={{header|Mercury}}== |
=={{header|Mercury}}== |
||
<lang> |
<syntaxhighlight lang="text"> |
||
:- module string_tokenize. |
:- module string_tokenize. |
||
:- interface. |
:- interface. |
||
Line 2,359: | Line 2,359: | ||
Tokens = string.split_at_char((','), "Hello,How,Are,You,Today"), |
Tokens = string.split_at_char((','), "Hello,How,Are,You,Today"), |
||
io.write_list(Tokens, ".", io.write_string, !IO), |
io.write_list(Tokens, ".", io.write_string, !IO), |
||
io.nl(!IO).</ |
io.nl(!IO).</syntaxhighlight> |
||
=={{header|min}}== |
=={{header|min}}== |
||
{{works with|min|0.19.3}} |
{{works with|min|0.19.3}} |
||
< |
<syntaxhighlight lang="min">"Hello,How,Are,You,Today" "," split "." join print</syntaxhighlight> |
||
=={{header|MiniScript}}== |
=={{header|MiniScript}}== |
||
< |
<syntaxhighlight lang="miniscript">tokens = "Hello,How,Are,You,Today".split(",") |
||
print tokens.join(".")</ |
print tokens.join(".")</syntaxhighlight> |
||
=={{header|MMIX}}== |
=={{header|MMIX}}== |
||
< |
<syntaxhighlight lang="mmix">sep IS ',' |
||
EOS IS 0 |
EOS IS 0 |
||
NL IS 10 |
NL IS 10 |
||
Line 2,423: | Line 2,423: | ||
LDBU t,tp |
LDBU t,tp |
||
PBNZ t,2B % UNTIL EOB(uffer) |
PBNZ t,2B % UNTIL EOB(uffer) |
||
TRAP 0,Halt,0</ |
TRAP 0,Halt,0</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,435: | Line 2,435: | ||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
< |
<syntaxhighlight lang="modula3">MODULE Tokenize EXPORTS Main; |
||
IMPORT IO, TextConv; |
IMPORT IO, TextConv; |
||
Line 2,452: | Line 2,452: | ||
END; |
END; |
||
IO.Put("\n"); |
IO.Put("\n"); |
||
END Tokenize.</ |
END Tokenize.</syntaxhighlight> |
||
=={{header|MUMPS}}== |
=={{header|MUMPS}}== |
||
< |
<syntaxhighlight lang="mumps">TOKENS |
||
NEW I,J,INP |
NEW I,J,INP |
||
SET INP="Hello,how,are,you,today" |
SET INP="Hello,how,are,you,today" |
||
Line 2,461: | Line 2,461: | ||
NEW J FOR J=1:1:I WRITE INP(J) WRITE:J'=I "." |
NEW J FOR J=1:1:I WRITE INP(J) WRITE:J'=I "." |
||
KILL I,J,INP // Kill is optional. "New" variables automatically are killed on "Quit" |
KILL I,J,INP // Kill is optional. "New" variables automatically are killed on "Quit" |
||
QUIT</ |
QUIT</syntaxhighlight> |
||
In use: |
In use: |
||
Line 2,468: | Line 2,468: | ||
=={{header|Nanoquery}}== |
=={{header|Nanoquery}}== |
||
< |
<syntaxhighlight lang="nanoquery">for word in "Hello,How,Are,You,Today".split(",") |
||
print word + "." |
print word + "." |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hello.How.Are.You.Today.</pre> |
<pre>Hello.How.Are.You.Today.</pre> |
||
=={{header|Nemerle}}== |
=={{header|Nemerle}}== |
||
< |
<syntaxhighlight lang="nemerle">using System; |
||
using System.Console; |
using System.Console; |
||
using Nemerle.Utility.NString; |
using Nemerle.Utility.NString; |
||
Line 2,488: | Line 2,488: | ||
// a quick in place list comprehension takes care of that |
// a quick in place list comprehension takes care of that |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
< |
<syntaxhighlight lang="netrexx">/*NetRexx program ***************************************************** |
||
* 20.08.2012 Walter Pachl derived from REXX Version 3 |
* 20.08.2012 Walter Pachl derived from REXX Version 3 |
||
**********************************************************************/ |
**********************************************************************/ |
||
Line 2,502: | Line 2,502: | ||
Say ss.word(i)'.' |
Say ss.word(i)'.' |
||
End |
End |
||
Say 'End-of-list.'</ |
Say 'End-of-list.'</syntaxhighlight> |
||
Output as in REXX version |
Output as in REXX version |
||
=={{header|NewLISP}}== |
=={{header|NewLISP}}== |
||
< |
<syntaxhighlight lang="newlisp">(print (join (parse "Hello,How,Are,You,Today" ",") "."))</syntaxhighlight> |
||
=={{header|Nial}}== |
=={{header|Nial}}== |
||
Line 2,514: | Line 2,514: | ||
Define Array with input string: |
Define Array with input string: |
||
< |
<syntaxhighlight lang="nial"> s := 'Hello,How,Are,You,Today' |
||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
|H|e|l|l|o|,|H|o|w|,|A|r|e|,|Y|o|u|,|T|o|d|a|y| |
|H|e|l|l|o|,|H|o|w|,|A|r|e|,|Y|o|u|,|T|o|d|a|y| |
||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+</ |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+</syntaxhighlight> |
||
Split string at the commas: |
Split string at the commas: |
||
< |
<syntaxhighlight lang="nial"> t := s eachall = `, cut s |
||
+-----------+-------+-------+-------+-----------+ |
+-----------+-------+-------+-------+-----------+ |
||
|+-+-+-+-+-+|+-+-+-+|+-+-+-+|+-+-+-+|+-+-+-+-+-+| |
|+-+-+-+-+-+|+-+-+-+|+-+-+-+|+-+-+-+|+-+-+-+-+-+| |
||
||H|e|l|l|o|||H|o|w|||A|r|e|||Y|o|u|||T|o|d|a|y|| |
||H|e|l|l|o|||H|o|w|||A|r|e|||Y|o|u|||T|o|d|a|y|| |
||
|+-+-+-+-+-+|+-+-+-+|+-+-+-+|+-+-+-+|+-+-+-+-+-+| |
|+-+-+-+-+-+|+-+-+-+|+-+-+-+|+-+-+-+|+-+-+-+-+-+| |
||
+-----------+-------+-------+-------+-----------+</ |
+-----------+-------+-------+-------+-----------+</syntaxhighlight> |
||
Join string with <code>.</code> and remove last <code>.</code> |
Join string with <code>.</code> and remove last <code>.</code> |
||
< |
<syntaxhighlight lang="nial"> u := front content (cart t `.) |
||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
|H|e|l|l|o|.|H|o|w|.|A|r|e|.|Y|o|u|.|T|o|d|a|y| |
|H|e|l|l|o|.|H|o|w|.|A|r|e|.|Y|o|u|.|T|o|d|a|y| |
||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+</ |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+</syntaxhighlight> |
||
Less cluttered display, using <code>set "sketch;set "nodecor</code> display switches. |
Less cluttered display, using <code>set "sketch;set "nodecor</code> display switches. |
||
< |
<syntaxhighlight lang="nial"> s:='Hello,How,Are,You,Today' |
||
Hello,How,Are,You,Today |
Hello,How,Are,You,Today |
||
t:= s eachall = `, cut s |
t:= s eachall = `, cut s |
||
Line 2,544: | Line 2,544: | ||
+-----+---+---+---+-----+ |
+-----+---+---+---+-----+ |
||
u:=front content (cart t `.) |
u:=front content (cart t `.) |
||
Hello.How.Are.You.Today</ |
Hello.How.Are.You.Today</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import strutils |
||
let text = "Hello,How,Are,You,Today" |
let text = "Hello,How,Are,You,Today" |
||
let tokens = text.split(',') |
let tokens = text.split(',') |
||
echo tokens.join(".")</ |
echo tokens.join(".")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,557: | Line 2,557: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang="objeck"> |
||
class Parse { |
class Parse { |
||
function : Main(args : String[]) ~ Nil { |
function : Main(args : String[]) ~ Nil { |
||
Line 2,565: | Line 2,565: | ||
}; |
}; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Objective-C}}== |
=={{header|Objective-C}}== |
||
Line 2,572: | Line 2,572: | ||
{{works with|Cocoa}} |
{{works with|Cocoa}} |
||
< |
<syntaxhighlight lang="objc">NSString *text = @"Hello,How,Are,You,Today"; |
||
NSArray *tokens = [text componentsSeparatedByString:@","]; |
NSArray *tokens = [text componentsSeparatedByString:@","]; |
||
NSString *result = [tokens componentsJoinedByString:@"."]; |
NSString *result = [tokens componentsJoinedByString:@"."]; |
||
NSLog(result);</ |
NSLog(result);</syntaxhighlight> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
To split on a single-character separator: |
To split on a single-character separator: |
||
< |
<syntaxhighlight lang="ocaml">let words = String.split_on_char ',' "Hello,How,Are,You,Today" in |
||
String.concat "." words |
String.concat "." words |
||
</syntaxhighlight> |
|||
</lang> |
|||
The function split_on_char has been introduced in OCaml 4.04. In previous versions, it could be implemented by: |
The function split_on_char has been introduced in OCaml 4.04. In previous versions, it could be implemented by: |
||
< |
<syntaxhighlight lang="ocaml">let split_on_char sep s = |
||
let r = ref [] in |
let r = ref [] in |
||
let j = ref (String.length s) in |
let j = ref (String.length s) in |
||
Line 2,594: | Line 2,594: | ||
end |
end |
||
done; |
done; |
||
String.sub s 0 !j :: !r</ |
String.sub s 0 !j :: !r</syntaxhighlight> |
||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">"Hello,How,Are,You,Today" wordsWith(',') println</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,606: | Line 2,606: | ||
=={{header|ooRexx}}== |
=={{header|ooRexx}}== |
||
< |
<syntaxhighlight lang="oorexx">text='Hello,How,Are,You,Today' |
||
do while text \= '' |
do while text \= '' |
||
parse var text word1 ',' text |
parse var text word1 ',' text |
||
call charout 'STDOUT:',word1'.' |
call charout 'STDOUT:',word1'.' |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hello.How.Are.You.Today.</pre> |
<pre>Hello.How.Are.You.Today.</pre> |
||
=={{header|OpenEdge/Progress}}== |
=={{header|OpenEdge/Progress}}== |
||
< |
<syntaxhighlight lang="progress">FUNCTION tokenizeString RETURNS CHAR ( |
||
i_c AS CHAR |
i_c AS CHAR |
||
): |
): |
||
Line 2,638: | Line 2,638: | ||
MESSAGE |
MESSAGE |
||
tokenizeString( "Hello,How,Are,You,Today" ) |
tokenizeString( "Hello,How,Are,You,Today" ) |
||
VIEW-AS ALERT-BOX.</ |
VIEW-AS ALERT-BOX.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,651: | Line 2,651: | ||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
< |
<syntaxhighlight lang="oz">for T in {String.tokens "Hello,How,Are,You,Today" &,} do |
||
{System.printInfo T#"."} |
{System.printInfo T#"."} |
||
end</ |
end</syntaxhighlight> |
||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
Line 2,663: | Line 2,663: | ||
{{Works with|PARI/GP|2.7.4 and above}} |
{{Works with|PARI/GP|2.7.4 and above}} |
||
< |
<syntaxhighlight lang="parigp"> |
||
\\ Tokenize a string str according to 1 character delimiter d. Return a list of tokens. |
\\ Tokenize a string str according to 1 character delimiter d. Return a list of tokens. |
||
\\ Using ssubstr() from http://rosettacode.org/wiki/Substring#PARI.2FGP |
\\ Using ssubstr() from http://rosettacode.org/wiki/Substring#PARI.2FGP |
||
Line 2,681: | Line 2,681: | ||
print("3.",tokenize(",Hello,,How,Are,You,Today",",")); |
print("3.",tokenize(",Hello,,How,Are,You,Today",",")); |
||
} |
} |
||
</ |
</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
Line 2,699: | Line 2,699: | ||
{{Works with|PARI/GP|2.7.4 and above}} |
{{Works with|PARI/GP|2.7.4 and above}} |
||
< |
<syntaxhighlight lang="parigp"> |
||
\\ Tokenize a string str according to 1 character delimiter d. Return a list of tokens. |
\\ Tokenize a string str according to 1 character delimiter d. Return a list of tokens. |
||
\\ Using ssubstr() from http://rosettacode.org/wiki/Substring#PARI.2FGP |
\\ Using ssubstr() from http://rosettacode.org/wiki/Substring#PARI.2FGP |
||
Line 2,728: | Line 2,728: | ||
print("7. 0 pp: ", stok("",",")); |
print("7. 0 pp: ", stok("",",")); |
||
} |
} |
||
</ |
</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
Line 2,744: | Line 2,744: | ||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
{{works with|Free_Pascal}} |
{{works with|Free_Pascal}} |
||
< |
<syntaxhighlight lang="pascal">program TokenizeString; |
||
{$mode objfpc}{$H+} |
{$mode objfpc}{$H+} |
||
Line 2,768: | Line 2,768: | ||
Tokens.Free; |
Tokens.Free; |
||
end; |
end; |
||
end.</ |
end.</syntaxhighlight> |
||
The result is: |
The result is: |
||
Line 2,775: | Line 2,775: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">print join('.', split /,/, 'Hello,How,Are,You,Today'), "\n";</syntaxhighlight> |
||
CLI one-liner form: |
CLI one-liner form: |
||
< |
<syntaxhighlight lang="perl">echo "Hello,How,Are,You,Today" | perl -aplF/,/ -e '$" = "."; $_ = "@F";'</syntaxhighlight> |
||
which is a compact way of telling Perl to do |
which is a compact way of telling Perl to do |
||
< |
<syntaxhighlight lang="perl">BEGIN { $/ = "\n"; $\ = "\n"; } |
||
LINE: while (defined($_ = <ARGV>)) { |
LINE: while (defined($_ = <ARGV>)) { |
||
chomp $_; |
chomp $_; |
||
Line 2,788: | Line 2,788: | ||
continue { |
continue { |
||
die "-p destination: $!\n" unless print $_; |
die "-p destination: $!\n" unless print $_; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Hello,How,Are,You,Today"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">","</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"."</span><span style="color: #0000FF;">)</span> |
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Hello,How,Are,You,Today"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">","</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"."</span><span style="color: #0000FF;">)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 2,800: | Line 2,800: | ||
=={{header|Phixmonti}}== |
=={{header|Phixmonti}}== |
||
< |
<syntaxhighlight lang="phixmonti">/# "Hello,How,Are,You,Today" "," "." subst print #/ |
||
"Hello,How,Are,You,Today" "," " " subst split len for get print "." print endfor</ |
"Hello,How,Are,You,Today" "," " " subst split len for get print "." print endfor</syntaxhighlight> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
{{works with|PHP|5.x}} |
{{works with|PHP|5.x}} |
||
< |
<syntaxhighlight lang="php"><?php |
||
$str = 'Hello,How,Are,You,Today'; |
$str = 'Hello,How,Are,You,Today'; |
||
echo implode('.', explode(',', $str)); |
echo implode('.', explode(',', $str)); |
||
?></ |
?></syntaxhighlight> |
||
=={{header|Picat}}== |
=={{header|Picat}}== |
||
Using the built-in functions <code>split/2</code> and <code>join/2</code>. |
Using the built-in functions <code>split/2</code> and <code>join/2</code>. |
||
< |
<syntaxhighlight lang="picat">import util. |
||
go => |
go => |
||
Line 2,822: | Line 2,822: | ||
% As a one liner: |
% As a one liner: |
||
S.split(",").join(".").println().</ |
S.split(",").join(".").println().</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,830: | Line 2,830: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(mapcar pack |
||
(split (chop "Hello,How,Are,You,Today") ",") )</ |
(split (chop "Hello,How,Are,You,Today") ",") )</syntaxhighlight> |
||
=={{header|Pike}}== |
=={{header|Pike}}== |
||
< |
<syntaxhighlight lang="pike">("Hello,How,Are,You,Today" / ",") * ".";</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang="pli">tok: Proc Options(main); |
||
declare s character (100) initial ('Hello,How,Are,You,Today'); |
declare s character (100) initial ('Hello,How,Are,You,Today'); |
||
declare n fixed binary (31); |
declare n fixed binary (31); |
||
Line 2,859: | Line 2,859: | ||
put skip list (string(table)); |
put skip list (string(table)); |
||
end; |
end; |
||
end;</ |
end;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hello.How.Are.You.Today</pre> |
<pre>Hello.How.Are.You.Today</pre> |
||
=={{header|PL/M}}== |
=={{header|PL/M}}== |
||
< |
<syntaxhighlight lang="plm">100H: |
||
/* CP/M CALLS */ |
/* CP/M CALLS */ |
||
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS; |
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS; |
||
Line 2,903: | Line 2,903: | ||
CALL EXIT; |
CALL EXIT; |
||
EOF;</ |
EOF;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>HELLO. HOW. ARE. YOU. TODAY. </pre> |
<pre>HELLO. HOW. ARE. YOU. TODAY. </pre> |
||
=={{header|Plain English}}== |
=={{header|Plain English}}== |
||
< |
<syntaxhighlight lang="plainenglish">To run: |
||
Start up. |
Start up. |
||
Split "Hello,How,Are,You,Today" into some string things given the comma byte. |
Split "Hello,How,Are,You,Today" into some string things given the comma byte. |
||
Line 2,924: | Line 2,924: | ||
If the string thing's next is not nil, append the byte to the string. |
If the string thing's next is not nil, append the byte to the string. |
||
Put the string thing's next into the string thing. |
Put the string thing's next into the string thing. |
||
Repeat.</ |
Repeat.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,937: | Line 2,937: | ||
First show the use of sysparse_string to break up a string and make a list of strings. |
First show the use of sysparse_string to break up a string and make a list of strings. |
||
< |
<syntaxhighlight lang="pop11">;;; Make a list of strings from a string using space as separator |
||
lvars list; |
lvars list; |
||
sysparse_string('the cat sat on the mat') -> list; |
sysparse_string('the cat sat on the mat') -> list; |
||
;;; print the list of strings |
;;; print the list of strings |
||
list => |
list => |
||
** [the cat sat on the mat]</ |
** [the cat sat on the mat]</syntaxhighlight> |
||
By giving it an extra parameter 'true' we can make it recognize numbers and produce a list of strings and numbers |
By giving it an extra parameter 'true' we can make it recognize numbers and produce a list of strings and numbers |
||
< |
<syntaxhighlight lang="pop11">lvars list; |
||
sysparse_string('one 1 two 2 three 3 four 4', true) -> list; |
sysparse_string('one 1 two 2 three 3 four 4', true) -> list; |
||
;;; print the list of strings and numbers |
;;; print the list of strings and numbers |
||
Line 2,955: | Line 2,955: | ||
** <true> |
** <true> |
||
isinteger(list(2))=> |
isinteger(list(2))=> |
||
** <true></ |
** <true></syntaxhighlight> |
||
Now show some uses of the built in procedure sys_parse_string, which allows more options: |
Now show some uses of the built in procedure sys_parse_string, which allows more options: |
||
< |
<syntaxhighlight lang="pop11">;;; Make pop-11 print strings with quotes |
||
true -> pop_pr_quotes; |
true -> pop_pr_quotes; |
||
;;; |
;;; |
||
Line 2,973: | Line 2,973: | ||
;;; print the list of strings |
;;; print the list of strings |
||
strings => |
strings => |
||
** ['Hello' 'How' 'Are' 'You' 'Today']</ |
** ['Hello' 'How' 'Are' 'You' 'Today']</syntaxhighlight> |
||
If {% ... %} were used instead of [% ... %] the result would be |
If {% ... %} were used instead of [% ... %] the result would be |
||
a vector (i.e. array) of strings rather than a list of strings. |
a vector (i.e. array) of strings rather than a list of strings. |
||
< |
<syntaxhighlight lang="pop11">{% sys_parse_string(str, `,`) %} -> strings; |
||
;;; print the vector |
;;; print the vector |
||
strings => |
strings => |
||
** {'Hello' 'How' 'Are' 'You' 'Today'}</ |
** {'Hello' 'How' 'Are' 'You' 'Today'}</syntaxhighlight> |
||
It is also possible to give sys_parse_string a 'conversion' procedure, which is applied to each of the tokens. |
It is also possible to give sys_parse_string a 'conversion' procedure, which is applied to each of the tokens. |
||
E.g. it could be used to produce a vector of numbers, using the conversion procedure 'strnumber', which converts a string to a number: |
E.g. it could be used to produce a vector of numbers, using the conversion procedure 'strnumber', which converts a string to a number: |
||
< |
<syntaxhighlight lang="pop11">lvars numbers; |
||
{% sys_parse_string('100 101 102 103 99.9 99.999', strnumber) %} -> numbers; |
{% sys_parse_string('100 101 102 103 99.9 99.999', strnumber) %} -> numbers; |
||
;;; the result is a vector containing integers and floats, |
;;; the result is a vector containing integers and floats, |
||
;;; which can be printed thus: |
;;; which can be printed thus: |
||
numbers => |
numbers => |
||
** {100 101 102 103 99.9 99.999}</ |
** {100 101 102 103 99.9 99.999}</syntaxhighlight> |
||
Using lower level pop-11 facilities to tokenise the string: |
Using lower level pop-11 facilities to tokenise the string: |
||
< |
<syntaxhighlight lang="pop11">;;; Declare and initialize variables |
||
lvars str='Hello,How,Are,You,Today'; |
lvars str='Hello,How,Are,You,Today'; |
||
;;; Iterate over string |
;;; Iterate over string |
||
Line 3,012: | Line 3,012: | ||
endif; |
endif; |
||
;;; Reverse the list |
;;; Reverse the list |
||
rev(ls) -> ls;</ |
rev(ls) -> ls;</syntaxhighlight> |
||
Since the task requires to use array we convert list to array |
Since the task requires to use array we convert list to array |
||
< |
<syntaxhighlight lang="pop11">;;; Put list elements and lenght on the stack |
||
destlist(ls); |
destlist(ls); |
||
;;; Build a vector from them |
;;; Build a vector from them |
||
Line 3,024: | Line 3,024: | ||
printf(ar(i), '%s.'); |
printf(ar(i), '%s.'); |
||
endfor; |
endfor; |
||
printf('\n');</ |
printf('\n');</syntaxhighlight> |
||
We could use list directly for printing: |
We could use list directly for printing: |
||
< |
<syntaxhighlight lang="pop11">for i in ls do |
||
printf(i, '%s.'); |
printf(i, '%s.'); |
||
endfor;</ |
endfor;</syntaxhighlight> |
||
so the conversion to vector is purely to satisfy task formulation. |
so the conversion to vector is purely to satisfy task formulation. |
||
Line 3,036: | Line 3,036: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
{{works with|PowerShell|1}} |
{{works with|PowerShell|1}} |
||
< |
<syntaxhighlight lang="powershell">$words = "Hello,How,Are,You,Today".Split(',') |
||
[string]::Join('.', $words)</ |
[string]::Join('.', $words)</syntaxhighlight> |
||
{{works with|PowerShell|2}} |
{{works with|PowerShell|2}} |
||
< |
<syntaxhighlight lang="powershell">$words = "Hello,How,Are,You,Today" -split ',' |
||
$words -join '.'</ |
$words -join '.'</syntaxhighlight> |
||
{{works with|PowerShell|2}} |
{{works with|PowerShell|2}} |
||
The StringSplitOptions enumeration weeds out the return of empty elements. |
The StringSplitOptions enumeration weeds out the return of empty elements. |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
"Hello,How,Are,You,Today", ",,Hello,,Goodbye,," | ForEach-Object {($_.Split(',',[StringSplitOptions]::RemoveEmptyEntries)) -join "."} |
"Hello,How,Are,You,Today", ",,Hello,,Goodbye,," | ForEach-Object {($_.Split(',',[StringSplitOptions]::RemoveEmptyEntries)) -join "."} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 3,056: | Line 3,056: | ||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
{{works with|SWI Prolog}} |
{{works with|SWI Prolog}} |
||
< |
<syntaxhighlight lang="prolog">splitup(Sep,[token(B)|BL]) --> splitup(Sep,B,BL). |
||
splitup(Sep,[A|AL],B) --> [A], {\+ [A] = Sep }, splitup(Sep,AL,B). |
splitup(Sep,[A|AL],B) --> [A], {\+ [A] = Sep }, splitup(Sep,AL,B). |
||
splitup(Sep,[],[B|BL]) --> Sep, splitup(Sep,B,BL). |
splitup(Sep,[],[B|BL]) --> Sep, splitup(Sep,B,BL). |
||
Line 3,064: | Line 3,064: | ||
phrase(splitup(".",Tokens),Backtogether), |
phrase(splitup(".",Tokens),Backtogether), |
||
string_to_list(ABack,Backtogether), |
string_to_list(ABack,Backtogether), |
||
writeln(ABack).</ |
writeln(ABack).</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,076: | Line 3,076: | ||
this can be accomplished in a few lines in the top level: |
this can be accomplished in a few lines in the top level: |
||
< |
<syntaxhighlight lang="prolog"> |
||
?- split_string("Hello,How,Are,You,Today", ",", "", Split), |
?- split_string("Hello,How,Are,You,Today", ",", "", Split), |
||
| atomics_to_string(Split, ".", PeriodSeparated), |
| atomics_to_string(Split, ".", PeriodSeparated), |
||
| writeln(PeriodSeparated). |
| writeln(PeriodSeparated). |
||
Hello.How.Are.You.Today |
Hello.How.Are.You.Today |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{works with|Python|2.5}}{{works with|Python|3.0}} |
{{works with|Python|2.5}}{{works with|Python|3.0}} |
||
< |
<syntaxhighlight lang="python">text = "Hello,How,Are,You,Today" |
||
tokens = text.split(',') |
tokens = text.split(',') |
||
print ('.'.join(tokens))</ |
print ('.'.join(tokens))</syntaxhighlight> |
||
Or if interpretation of the task description means you don't need to keep an intermediate array: |
Or if interpretation of the task description means you don't need to keep an intermediate array: |
||
< |
<syntaxhighlight lang="python">print ('.'.join('Hello,How,Are,You,Today'.split(',')))</syntaxhighlight> |
||
=={{header|Q}}== |
=={{header|Q}}== |
||
< |
<syntaxhighlight lang="q">words: "," vs "Hello,How,Are,You,Today" |
||
"." sv words</ |
"." sv words</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,102: | Line 3,102: | ||
=={{header|QB64}}== |
=={{header|QB64}}== |
||
''CBTJD'': 2020/03/12 |
''CBTJD'': 2020/03/12 |
||
< |
<syntaxhighlight lang="vb">a$ = "Hello,How,Are,You,Today" ' | Initialize original string. |
||
FOR na = 1 TO LEN(a$) ' | Start loop to count number of commas. |
FOR na = 1 TO LEN(a$) ' | Start loop to count number of commas. |
||
IF MID$(a$, na, 1) = "," THEN nc = nc + 1 ' | For each comma, increment nc. |
IF MID$(a$, na, 1) = "," THEN nc = nc + 1 ' | For each comma, increment nc. |
||
Line 3,120: | Line 3,120: | ||
PRINT LEFT$(tf$, LEN(tf$) - 1) ' | Print all but the last period of tf$. |
PRINT LEFT$(tf$, LEN(tf$) - 1) ' | Print all but the last period of tf$. |
||
END ' | Program end. |
END ' | Program end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Alternative method using word$ function:''' |
'''Alternative method using word$ function:''' |
||
---- |
---- |
||
''CBTJD'': 2020/03/12 |
''CBTJD'': 2020/03/12 |
||
< |
<syntaxhighlight lang="vb">a$ = "Hello,How,Are,You,Today" ' | Initialize original string. |
||
DIM t$(LEN(a$) / 2) ' | Create an overestimated sized array. |
DIM t$(LEN(a$) / 2) ' | Create an overestimated sized array. |
||
FOR nd = 1 TO LEN(a$) ' | Start loop to find each comma. |
FOR nd = 1 TO LEN(a$) ' | Start loop to find each comma. |
||
Line 3,153: | Line 3,153: | ||
DONE: ' | Label for bail destination of word count error check. |
DONE: ' | Label for bail destination of word count error check. |
||
END FUNCTION ' | End of function. |
END FUNCTION ' | End of function. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery"> [ [] [] rot |
||
witheach |
witheach |
||
[ dup char , = iff |
[ dup char , = iff |
||
Line 3,166: | Line 3,166: | ||
[ witheach [ echo$ say "." ] ] is display ( [ --> ) |
[ witheach [ echo$ say "." ] ] is display ( [ --> ) |
||
$ "Hello,How,Are,You,Today" tokenise display</ |
$ "Hello,How,Are,You,Today" tokenise display</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 3,173: | Line 3,173: | ||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<syntaxhighlight lang="r">text <- "Hello,How,Are,You,Today" |
||
junk <- strsplit(text, split=",") |
junk <- strsplit(text, split=",") |
||
print(paste(unlist(junk), collapse="."))</ |
print(paste(unlist(junk), collapse="."))</syntaxhighlight> |
||
or the one liner |
or the one liner |
||
< |
<syntaxhighlight lang="r">paste(unlist(strsplit(text, split=",")), collapse=".")</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
(string-join (string-split "Hello,How,Are,You,Today" ",") ".") |
(string-join (string-split "Hello,How,Are,You,Today" ",") ".") |
||
;; -> "Hello.How.Are.You.Today" |
;; -> "Hello.How.Are.You.Today" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
{{works with|Rakudo|#22 "Thousand Oaks"}} |
{{works with|Rakudo|#22 "Thousand Oaks"}} |
||
<lang |
<syntaxhighlight lang="raku" line>'Hello,How,Are,You,Today'.split(',').join('.').say;</syntaxhighlight> |
||
Or with function calls: |
Or with function calls: |
||
<lang |
<syntaxhighlight lang="raku" line>say join '.', split ',', 'Hello,How,Are,You,Today';</syntaxhighlight> |
||
=={{header|Raven}}== |
=={{header|Raven}}== |
||
< |
<syntaxhighlight lang="raven">'Hello,How,Are,You,Today' ',' split '.' join print</syntaxhighlight> |
||
=={{header|REBOL}}== |
=={{header|REBOL}}== |
||
< |
<syntaxhighlight lang="rebol">print ["Original:" original: "Hello,How,Are,You,Today"] |
||
tokens: parse original "," |
tokens: parse original "," |
||
dotted: "" repeat i tokens [append dotted rejoin [i "."]] |
dotted: "" repeat i tokens [append dotted rejoin [i "."]] |
||
print ["Dotted: " dotted]</ |
print ["Dotted: " dotted]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,214: | Line 3,214: | ||
=={{header|Red}}== |
=={{header|Red}}== |
||
< |
<syntaxhighlight lang="red">str: "Hello,How,Are,You,Today" |
||
>> tokens: split str "," |
>> tokens: split str "," |
||
>> probe tokens |
>> probe tokens |
||
Line 3,221: | Line 3,221: | ||
>> periods: replace/all form tokens " " "." ;The word FORM converts the list series to a string removing quotes. |
>> periods: replace/all form tokens " " "." ;The word FORM converts the list series to a string removing quotes. |
||
>> print periods ;then REPLACE/ALL spaces with period |
>> print periods ;then REPLACE/ALL spaces with period |
||
Hello.How.Are.You.Today</ |
Hello.How.Are.You.Today</syntaxhighlight> |
||
=={{header|Retro}}== |
=={{header|Retro}}== |
||
<syntaxhighlight lang="retro">{{ |
|||
<lang Retro>{{ |
|||
: char ( -$ ) " " ; |
: char ( -$ ) " " ; |
||
: tokenize ( $-$$ ) |
: tokenize ( $-$$ ) |
||
Line 3,236: | Line 3,236: | ||
[ tokenize action dup 1 <> ] while drop |
[ tokenize action dup 1 <> ] while drop |
||
^buffer'get drop ; |
^buffer'get drop ; |
||
}}</ |
}}</syntaxhighlight> |
||
This will suffice to split a string into an array of substrings. It is used like this: |
This will suffice to split a string into an array of substrings. It is used like this: |
||
< |
<syntaxhighlight lang="retro">create strings 100 allot |
||
"Hello,How,Are,You,Today" ', strings split</ |
"Hello,How,Are,You,Today" ', strings split</syntaxhighlight> |
||
Since the buffer' vocabulary creates a zero-terminated buffer, we can display it using the each@ combinator and a simple quote: |
Since the buffer' vocabulary creates a zero-terminated buffer, we can display it using the each@ combinator and a simple quote: |
||
< |
<syntaxhighlight lang="retro">strings [ @ "%s." puts ] ^types'STRING each@</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===version 1=== |
===version 1=== |
||
This REXX version doesn't append a period to the last word in the list. |
This REXX version doesn't append a period to the last word in the list. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program separates a string of comma─delimited words, and echoes them ──► terminal*/ |
||
original = 'Hello,How,Are,You,Today' /*some words separated by commas (,). */ |
original = 'Hello,How,Are,You,Today' /*some words separated by commas (,). */ |
||
say 'The input string:' original /*display original string ──► terminal.*/ |
say 'The input string:' original /*display original string ──► terminal.*/ |
||
Line 3,262: | Line 3,262: | ||
say @.j || left(., j\==#) /*maybe append a period (.) to a word. */ |
say @.j || left(., j\==#) /*maybe append a period (.) to a word. */ |
||
end /*j*/ /* [↑] don't append a period if last. */ |
end /*j*/ /* [↑] don't append a period if last. */ |
||
say center(' End─of─list ', 40, "═") /*display a (EOL) trailer for the list.*/</ |
say center(' End─of─list ', 40, "═") /*display a (EOL) trailer for the list.*/</syntaxhighlight> |
||
{{out|output|text= when using the internal default input:}} |
{{out|output|text= when using the internal default input:}} |
||
<pre> |
<pre> |
||
Line 3,280: | Line 3,280: | ||
Hello,Betty Sue,How,Are,You,Today |
Hello,Betty Sue,How,Are,You,Today |
||
< |
<syntaxhighlight lang="rexx">/*REXX program to separate a string of comma-delimited words and echo */ |
||
sss='Hello,How,Are,You,Today' |
sss='Hello,How,Are,You,Today' |
||
say 'input string='sss |
say 'input string='sss |
||
Line 3,291: | Line 3,291: | ||
say word(ss,i)dot |
say word(ss,i)dot |
||
End |
End |
||
say 'End-of-list.'</ |
say 'End-of-list.'</syntaxhighlight> |
||
'''output''' is similar to REXX version 1. |
'''output''' is similar to REXX version 1. |
||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
see substr("Hello,How,Are,You,Today", ",", ".") |
see substr("Hello,How,Are,You,Today", ",", ".") |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">puts "Hello,How,Are,You,Today".split(',').join('.')</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">fn main() { |
||
let s = "Hello,How,Are,You,Today"; |
let s = "Hello,How,Are,You,Today"; |
||
let tokens: Vec<&str> = s.split(",").collect(); |
let tokens: Vec<&str> = s.split(",").collect(); |
||
println!("{}", tokens.join(".")); |
println!("{}", tokens.join(".")); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|S-lang}}== |
=={{header|S-lang}}== |
||
< |
<syntaxhighlight lang="s-lang">variable a = strchop("Hello,How,Are,You,Today", ',', 0); |
||
print(strjoin(a, "."));</ |
print(strjoin(a, "."));</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,317: | Line 3,317: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">println("Hello,How,Are,You,Today" split "," mkString ".")</syntaxhighlight> |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
{{works with|Guile}} |
{{works with|Guile}} |
||
< |
<syntaxhighlight lang="scheme">(use-modules (ice-9 regex)) |
||
(define s "Hello,How,Are,You,Today") |
(define s "Hello,How,Are,You,Today") |
||
(define words (map match:substring (list-matches "[^,]+" s))) |
(define words (map match:substring (list-matches "[^,]+" s))) |
||
Line 3,328: | Line 3,328: | ||
(display (list-ref words n)) |
(display (list-ref words n)) |
||
(if (< n (- (length words) 1)) |
(if (< n (- (length words) 1)) |
||
(display ".")))</ |
(display ".")))</syntaxhighlight> |
||
(with SRFI 13) |
(with SRFI 13) |
||
< |
<syntaxhighlight lang="scheme">(define s "Hello,How,Are,You,Today") |
||
(define words (string-tokenize s (char-set-complement (char-set #\,)))) |
(define words (string-tokenize s (char-set-complement (char-set #\,)))) |
||
(define t (string-join words "."))</ |
(define t (string-join words "."))</syntaxhighlight> |
||
{{works with|Gauche Scheme}} |
{{works with|Gauche Scheme}} |
||
< |
<syntaxhighlight lang="scheme">(print |
||
(string-join |
(string-join |
||
(string-split "Hello,How,Are,You,Today" #\,) |
(string-split "Hello,How,Are,You,Today" #\,) |
||
".")) </ |
".")) </syntaxhighlight> |
||
{{output}} |
{{output}} |
||
<pre> |
<pre> |
||
Line 3,346: | Line 3,346: | ||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">var array string: tokens is 0 times ""; |
||
tokens := split("Hello,How,Are,You,Today", ",");</ |
tokens := split("Hello,How,Are,You,Today", ",");</syntaxhighlight> |
||
=={{header|Self}}== |
=={{header|Self}}== |
||
< |
<syntaxhighlight lang="self">| s = 'Hello,How,Are,You,Today' | |
||
((s splitOn: ',') joinUsing: '.') printLine. |
((s splitOn: ',') joinUsing: '.') printLine. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">'Hello,How,Are,You,Today'.split(',').join('.').say;</syntaxhighlight> |
||
=={{header|Simula}}== |
=={{header|Simula}}== |
||
< |
<syntaxhighlight lang="simula">BEGIN |
||
CLASS TEXTARRAY(N); INTEGER N; |
CLASS TEXTARRAY(N); INTEGER N; |
||
Line 3,408: | Line 3,408: | ||
END. |
END. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>HELLO.HOW.ARE.YOU.TODAY.</pre> |
<pre>HELLO.HOW.ARE.YOU.TODAY.</pre> |
||
=={{header|Slate}}== |
=={{header|Slate}}== |
||
< |
<syntaxhighlight lang="slate">('Hello,How,Are,You,Today' splitWith: $,) join &separator: '.'.</syntaxhighlight> |
||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
< |
<syntaxhighlight lang="smalltalk">|array | |
||
array := 'Hello,How,Are,You,Today' subStrings: $,. |
array := 'Hello,How,Are,You,Today' subStrings: $,. |
||
array fold: [:concatenation :string | concatenation, '.', string ]</ |
array fold: [:concatenation :string | concatenation, '.', string ]</syntaxhighlight> |
||
Some implementations also have a ''join:'' convenience method that allows the following shorter solution: |
Some implementations also have a ''join:'' convenience method that allows the following shorter solution: |
||
< |
<syntaxhighlight lang="smalltalk">('Hello,How,Are,You,Today' subStrings: $,) join: '.'</syntaxhighlight> |
||
The solution displaying a trailing period would be: |
The solution displaying a trailing period would be: |
||
< |
<syntaxhighlight lang="smalltalk">|array | |
||
array := 'Hello,How,Are,You,Today' subStrings: $,. |
array := 'Hello,How,Are,You,Today' subStrings: $,. |
||
array inject: '' into: [:concatenation :string | concatenation, string, '.' ]</ |
array inject: '' into: [:concatenation :string | concatenation, string, '.' ]</syntaxhighlight> |
||
=={{header|SNOBOL4}}== |
=={{header|SNOBOL4}}== |
||
Line 3,434: | Line 3,434: | ||
For this task, it's convenient to define Perl-style split( ) and join( ) functions. |
For this task, it's convenient to define Perl-style split( ) and join( ) functions. |
||
< |
<syntaxhighlight lang="snobol4"> define('split(chs,str)i,j,t,w2') :(split_end) |
||
split t = table() |
split t = table() |
||
sp1 str pos(0) (break(chs) | rem) $ t<i = i + 1> |
sp1 str pos(0) (break(chs) | rem) $ t<i = i + 1> |
||
Line 3,450: | Line 3,450: | ||
* # Test and display |
* # Test and display |
||
output = join('.',split(',','Hello,How,Are,You,Today')) |
output = join('.',split(',','Hello,How,Are,You,Today')) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,458: | Line 3,458: | ||
=={{header|Standard ML}}== |
=={{header|Standard ML}}== |
||
< |
<syntaxhighlight lang="sml">val splitter = String.tokens (fn c => c = #","); |
||
val main = (String.concatWith ".") o splitter;</ |
val main = (String.concatWith ".") o splitter;</syntaxhighlight> |
||
Test: |
Test: |
||
< |
<syntaxhighlight lang="sml">- main "Hello,How,Are,You,Today" |
||
val it = "Hello.How.Are.You.Today" : string</ |
val it = "Hello.How.Are.You.Today" : string</syntaxhighlight> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
{{works with|Swift|3.x}} |
{{works with|Swift|3.x}} |
||
< |
<syntaxhighlight lang="swift">let text = "Hello,How,Are,You,Today" |
||
let tokens = text.components(separatedBy: ",") // for single or multi-character separator |
let tokens = text.components(separatedBy: ",") // for single or multi-character separator |
||
print(tokens) |
print(tokens) |
||
let result = tokens.joined(separator: ".") |
let result = tokens.joined(separator: ".") |
||
print(result)</ |
print(result)</syntaxhighlight> |
||
{{works with|Swift|2.x}} |
{{works with|Swift|2.x}} |
||
< |
<syntaxhighlight lang="swift">let text = "Hello,How,Are,You,Today" |
||
let tokens = text.characters.split(",").map{String($0)} // for single-character separator |
let tokens = text.characters.split(",").map{String($0)} // for single-character separator |
||
print(tokens) |
print(tokens) |
||
let result = tokens.joinWithSeparator(".") |
let result = tokens.joinWithSeparator(".") |
||
print(result)</ |
print(result)</syntaxhighlight> |
||
{{works with|Swift|1.x}} |
{{works with|Swift|1.x}} |
||
< |
<syntaxhighlight lang="swift">let text = "Hello,How,Are,You,Today" |
||
let tokens = split(text, { $0 == "," }) // for single-character separator |
let tokens = split(text, { $0 == "," }) // for single-character separator |
||
println(tokens) |
println(tokens) |
||
let result = ".".join(tokens) |
let result = ".".join(tokens) |
||
println(result)</ |
println(result)</syntaxhighlight> |
||
For multi-character separators:< |
For multi-character separators:<syntaxhighlight lang="swift">import Foundation |
||
let text = "Hello,How,Are,You,Today" |
let text = "Hello,How,Are,You,Today" |
||
let tokens = text.componentsSeparatedByString(",") |
let tokens = text.componentsSeparatedByString(",") |
||
print(tokens)</ |
print(tokens)</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
Generating a list form a string by splitting on a comma: |
Generating a list form a string by splitting on a comma: |
||
< |
<syntaxhighlight lang="tcl">split $string ","</syntaxhighlight> |
||
Joining the elements of a list by a period: |
Joining the elements of a list by a period: |
||
< |
<syntaxhighlight lang="tcl">join $list "."</syntaxhighlight> |
||
Thus the whole thing would look like this: |
Thus the whole thing would look like this: |
||
< |
<syntaxhighlight lang="tcl">puts [join [split "Hello,How,Are,You,Today" ","] "."]</syntaxhighlight> |
||
If you'd like to retain the list in a variable with the name "words", it would only be marginally more complex: |
If you'd like to retain the list in a variable with the name "words", it would only be marginally more complex: |
||
< |
<syntaxhighlight lang="tcl">puts [join [set words [split "Hello,How,Are,You,Today" ","]] "."]</syntaxhighlight> |
||
(In general, the <tt>regexp</tt> command is also used in Tcl for tokenization of strings, but this example does not need that level of complexity.) |
(In general, the <tt>regexp</tt> command is also used in Tcl for tokenization of strings, but this example does not need that level of complexity.) |
||
Line 3,513: | Line 3,513: | ||
<code>tr</code> knows nothing about arrays, so this solution only changes each comma to a period. |
<code>tr</code> knows nothing about arrays, so this solution only changes each comma to a period. |
||
< |
<syntaxhighlight lang="bash">echo 'Hello,How,Are,You,Today' | tr ',' '.'</syntaxhighlight> |
||
=={{header|TUSCRIPT}}== |
=={{header|TUSCRIPT}}== |
||
< |
<syntaxhighlight lang="tuscript"> |
||
$$ MODE TUSCRIPT |
$$ MODE TUSCRIPT |
||
SET string="Hello,How,Are,You,Today" |
SET string="Hello,How,Are,You,Today" |
||
SET string=SPLIT (string,":,:") |
SET string=SPLIT (string,":,:") |
||
SET string=JOIN (string,".") |
SET string=JOIN (string,".") |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|TXR}}== |
=={{header|TXR}}== |
||
Line 3,528: | Line 3,528: | ||
sequences of non-commas. |
sequences of non-commas. |
||
< |
<syntaxhighlight lang="txr">@(next :list "Hello,How,Are,You,Today") |
||
@(coll)@{token /[^,]+/}@(end) |
@(coll)@{token /[^,]+/}@(end) |
||
@(output) |
@(output) |
||
@(rep)@token.@(last)@token@(end) |
@(rep)@token.@(last)@token@(end) |
||
@(end)</ |
@(end)</syntaxhighlight> |
||
Different approach. Collect tokens, each of |
Different approach. Collect tokens, each of |
||
Line 3,538: | Line 3,538: | ||
before a comma, or else extends to the end of the line. |
before a comma, or else extends to the end of the line. |
||
< |
<syntaxhighlight lang="txr">@(next :list "Hello,How,Are,You,Today") |
||
@(coll)@(maybe)@token,@(or)@token@(end)@(end) |
@(coll)@(maybe)@token,@(or)@token@(end)@(end) |
||
@(output) |
@(output) |
||
@(rep)@token.@(last)@token@(end) |
@(rep)@token.@(last)@token@(end) |
||
@(end)</ |
@(end)</syntaxhighlight> |
||
Using TXR Lisp: |
Using TXR Lisp: |
||
< |
<syntaxhighlight lang="bash">txr -p '(cat-str (split-str "Hello,How,Are,You,Today" ",") ".")' |
||
Hello.How.Are.You.Today</ |
Hello.How.Are.You.Today</syntaxhighlight> |
||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
{{works with|Bourne Shell}} |
{{works with|Bourne Shell}} |
||
< |
<syntaxhighlight lang="bash">string='Hello,How,Are,You,Today' |
||
(IFS=, |
(IFS=, |
||
printf '%s.' $string |
printf '%s.' $string |
||
echo)</ |
echo)</syntaxhighlight> |
||
---- |
---- |
||
{{works with|Bourne Again SHell}} |
{{works with|Bourne Again SHell}} |
||
{{works with|Public Domain Korn SHell|5.2.14}} |
{{works with|Public Domain Korn SHell|5.2.14}} |
||
< |
<syntaxhighlight lang="bash">#! /bin/bash |
||
stripchar-l () |
stripchar-l () |
||
#removes the specified character from the left side of the string |
#removes the specified character from the left side of the string |
||
Line 3,617: | Line 3,617: | ||
join "$( split "$list" "$input_delimiter" )" \ |
join "$( split "$list" "$input_delimiter" )" \ |
||
"$contains_a_space" "$output_delimiter"; |
"$contains_a_space" "$output_delimiter"; |
||
}</ |
}</syntaxhighlight> |
||
''Example'' |
''Example'' |
||
< |
<syntaxhighlight lang="bash"> strtokenize "Hello,How,Are,You,Today" "," "." |
||
Hello.How.Are.You.Today </ |
Hello.How.Are.You.Today </syntaxhighlight> |
||
---- |
---- |
||
Line 3,630: | Line 3,630: | ||
{{works with|ksh93}} |
{{works with|ksh93}} |
||
{{works with|zsh}} |
{{works with|zsh}} |
||
<syntaxhighlight lang="sh"> |
|||
<lang sh> |
|||
string1="Hello,How,Are,You,Today" |
string1="Hello,How,Are,You,Today" |
||
elements_quantity=$(echo $string1|tr "," "\n"|wc -l) |
elements_quantity=$(echo $string1|tr "," "\n"|wc -l) |
||
Line 3,643: | Line 3,643: | ||
# or to cheat |
# or to cheat |
||
echo "Hello,How,Are,You,Today"|tr "," "."</ |
echo "Hello,How,Are,You,Today"|tr "," "."</syntaxhighlight> |
||
=={{header|UnixPipes}}== |
=={{header|UnixPipes}}== |
||
{{works with|Bourne Shell}} |
{{works with|Bourne Shell}} |
||
< |
<syntaxhighlight lang="bash">token() { |
||
(IFS=, read -r A B; echo "$A".; test -n "$B" && (echo "$B" | token)) |
(IFS=, read -r A B; echo "$A".; test -n "$B" && (echo "$B" | token)) |
||
} |
} |
||
echo "Hello,How,Are,You" | token</ |
echo "Hello,How,Are,You" | token</syntaxhighlight> |
||
=={{header|Ursa}}== |
=={{header|Ursa}}== |
||
< |
<syntaxhighlight lang="ursa">decl string text |
||
set text "Hello,How,Are,You,Today" |
set text "Hello,How,Are,You,Today" |
||
decl string<> tokens |
decl string<> tokens |
||
Line 3,661: | Line 3,661: | ||
out tokens<i> "." console |
out tokens<i> "." console |
||
end for |
end for |
||
out endl console</ |
out endl console</syntaxhighlight> |
||
=={{header|Ursala}}== |
=={{header|Ursala}}== |
||
Line 3,669: | Line 3,669: | ||
second order function parameterized by the delimiter. Character |
second order function parameterized by the delimiter. Character |
||
literals are preceded by a backquote. |
literals are preceded by a backquote. |
||
< |
<syntaxhighlight lang="ursala">#import std |
||
token_list = sep`, 'Hello,How,Are,You,Today' |
token_list = sep`, 'Hello,How,Are,You,Today' |
||
Line 3,675: | Line 3,675: | ||
#cast %s |
#cast %s |
||
main = mat`. token_list</ |
main = mat`. token_list</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,682: | Line 3,682: | ||
=={{header|Vala}}== |
=={{header|Vala}}== |
||
< |
<syntaxhighlight lang="vala">void main() { |
||
string s = "Hello,How,Are,You,Today"; |
string s = "Hello,How,Are,You,Today"; |
||
print(@"$(string.joinv(".", s.split(",")))"); |
print(@"$(string.joinv(".", s.split(",")))"); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hello.How.Are.You.Today</pre> |
<pre>Hello.How.Are.You.Today</pre> |
||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
< |
<syntaxhighlight lang="vb">Sub Main() |
||
Dim temp() As String |
Dim temp() As String |
||
temp = Tokenize("Hello,How,Are,You,Today", ",") |
temp = Tokenize("Hello,How,Are,You,Today", ",") |
||
Line 3,702: | Line 3,702: | ||
Private Sub Display(arr() As String, sep As String) |
Private Sub Display(arr() As String, sep As String) |
||
Debug.Print Join(arr, sep) |
Debug.Print Join(arr, sep) |
||
End Sub</ |
End Sub</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Hello How Are You Today</pre> |
<pre>Hello How Are You Today</pre> |
||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
<syntaxhighlight lang="vb"> |
|||
<lang vb> |
|||
s = "Hello,How,Are,You,Today" |
s = "Hello,How,Are,You,Today" |
||
WScript.StdOut.Write Join(Split(s,","),".") |
WScript.StdOut.Write Join(Split(s,","),".") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre>Hello.How.Are.You.Today</pre> |
<pre>Hello.How.Are.You.Today</pre> |
||
Line 3,720: | Line 3,720: | ||
The contents of each text register is then displayed to user, separated by a period. |
The contents of each text register is then displayed to user, separated by a period. |
||
< |
<syntaxhighlight lang="vedit">Buf_Switch(Buf_Free) |
||
Ins_Text("Hello,How,Are,You,Today") |
Ins_Text("Hello,How,Are,You,Today") |
||
Line 3,739: | Line 3,739: | ||
} |
} |
||
Buf_Quit(OK)</ |
Buf_Quit(OK)</syntaxhighlight> |
||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
< |
<syntaxhighlight lang="go">// Tokenize a string, in V |
||
// Tectonics: v run tokenize-a-string.v |
// Tectonics: v run tokenize-a-string.v |
||
module main |
module main |
||
Line 3,749: | Line 3,749: | ||
pub fn main() { |
pub fn main() { |
||
println("Hello,How,Are,You,Today".split(',').join('.')) |
println("Hello,How,Are,You,Today".split(',').join('.')) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>prompt$ v run rosetta/tokenize-a-string.v |
<pre>prompt$ v run rosetta/tokenize-a-string.v |
||
Line 3,756: | Line 3,756: | ||
=={{header|WinBatch}}== |
=={{header|WinBatch}}== |
||
< |
<syntaxhighlight lang="winbatch">text = 'Hello,How,Are,You,Today' |
||
result = '' |
result = '' |
||
BoxOpen('WinBatch Tokenizing Example', '') |
BoxOpen('WinBatch Tokenizing Example', '') |
||
Line 3,764: | Line 3,764: | ||
next |
next |
||
display(10, 'End of Program', 'Dialog and program will close momentarily.') |
display(10, 'End of Program', 'Dialog and program will close momentarily.') |
||
BoxShut()</ |
BoxShut()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,770: | Line 3,770: | ||
=={{header|Wortel}}== |
=={{header|Wortel}}== |
||
< |
<syntaxhighlight lang="wortel">@join "." @split "," "Hello,How,Are,You,Today"</syntaxhighlight> |
||
Returns |
Returns |
||
<pre>"Hello.How.Are.You.Today"</pre> |
<pre>"Hello.How.Are.You.Today"</pre> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
< |
<syntaxhighlight lang="ecmascript">var s = "Hello,How,Are,You,Today" |
||
var t = s.split(",").join(".") + "." |
var t = s.split(",").join(".") + "." |
||
System.print(t)</ |
System.print(t)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,785: | Line 3,785: | ||
=={{header|XPath 2.0}}== |
=={{header|XPath 2.0}}== |
||
< |
<syntaxhighlight lang="xpath">string-join(tokenize("Hello,How,Are,You,Today", ","), ".")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,791: | Line 3,791: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">string 0; |
||
include c:\cxpl\codes; |
include c:\cxpl\codes; |
||
int I, J, K, Char; |
int I, J, K, Char; |
||
Line 3,810: | Line 3,810: | ||
for K:= 4 downto 0 do [Text(0, addr Array(K,0)); ChOut(0, ^.)]; |
for K:= 4 downto 0 do [Text(0, addr Array(K,0)); ChOut(0, ^.)]; |
||
CrLf(0); |
CrLf(0); |
||
]</ |
]</syntaxhighlight> |
||
The 'addr' operator is used to fetch the 32-bit address of Array rather |
The 'addr' operator is used to fetch the 32-bit address of Array rather |
||
Line 3,821: | Line 3,821: | ||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
< |
<syntaxhighlight lang="yabasic">dim s$(1) |
||
n = token("Hello. How are you today?", s$(), ".? ") |
n = token("Hello. How are you today?", s$(), ".? ") |
||
Line 3,829: | Line 3,829: | ||
if i < n print "."; |
if i < n print "."; |
||
next |
next |
||
print</ |
print</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">"Hello,How,Are,You,Today".split(",").concat(".").println(); |
||
Hello.How.Are.You.Today</ |
Hello.How.Are.You.Today</syntaxhighlight> |
||
=={{header|Zoea}}== |
=={{header|Zoea}}== |
||
<syntaxhighlight lang="zoea"> |
|||
<lang Zoea> |
|||
program: tokenize_a_string |
program: tokenize_a_string |
||
input: "Hello,How,Are,You,Today" |
input: "Hello,How,Are,You,Today" |
||
output: "Hello.How.Are.You.Today" |
output: "Hello.How.Are.You.Today" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Zoea Visual}}== |
=={{header|Zoea Visual}}== |
||
Line 3,846: | Line 3,846: | ||
=={{header|Zsh}}== |
=={{header|Zsh}}== |
||
< |
<syntaxhighlight lang="zsh">str='Hello,How,Are,You,Today' |
||
tokens=(${(s:,:)str}) |
tokens=(${(s:,:)str}) |
||
print ${(j:.:)tokens}</ |
print ${(j:.:)tokens}</syntaxhighlight> |
||
Or, using SH_SPLIT_WORD: |
Or, using SH_SPLIT_WORD: |
||
< |
<syntaxhighlight lang="zsh">str='Hello,How,Are,You,Today' |
||
IFS=, echo ${(j:.:)${=str}}</ |
IFS=, echo ${(j:.:)${=str}}</syntaxhighlight> |
||
{{omit from|PARI/GP|No real capacity for string manipulation}} |
{{omit from|PARI/GP|No real capacity for string manipulation}} |