Tokenize a string: Difference between revisions
Content deleted Content added
Added Fennel. |
→Nu: add |
||
(35 intermediate revisions by 23 users not shown) | |||
Line 15:
{{trans|Python}}
<
V tokens = text.split(‘,’)
print(tokens.join(‘.’))</
{{out}}
Line 25:
=={{header|360 Assembly}}==
<
TOKSTR CSECT
USING TOKSTR,R13 base register
Line 107:
PG DC CL80' ' buffer
YREGS
END TOKSTR</
{{out}}
<pre>
Line 114:
=={{header|8080 Assembly}}==
<
org 100h
jmp demo
Line 161:
period: db '. $'
hello: db 'Hello,How,Are,You,Today$'
parts: equ $</
{{out}}
<pre>Hello. How. Are. You. Today.</pre>
=={{header|8086 Assembly}}==
<
org 100h
section .text
Line 203:
hello: db 'Hello,How,Are,You,Today$'
section .bss
parts: resw 10</
{{out}}
<pre>Hello. How. Are. You. Today. </pre>
Line 209:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program strTokenize64.s */
Line 349:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
{{Output}}
<pre>
Line 361:
=={{header|ACL2}}==
<
(if (or (endp xs) (eql (first xs) delim))
(mv nil (rest xs))
Line 389:
(progn$ (cw (first strs))
(cw (coerce (list delim) 'string))
(print-with (rest strs) delim))))</
{{out}}
Line 398:
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}}
<
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:
Clear(items,@count)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Tokenize_a_string.png Screenshot from Atari 8-bit computer]
Line 506:
=={{header|ActionScript}}==
<
var tokens:Array = hello.split(",");
trace(tokens.join("."));
// Or as a one-liner
trace("Hello,How,Are,You,Today".split(",").join("."));</
=={{header|Ada}}==
<
use Ada.Text_IO, Ada.Containers, Ada.Strings, Ada.Strings.Fixed, Ada.Strings.Maps;
Line 534:
Put (S & ".");
end loop;
end Tokenize;</
=={{header|ALGOL 68}}==
<
OP +:= = (REF FLEX[]STRING in out, STRING item)VOID:(
Line 585:
printf(($g"."$, string split(beetles, ", "),$l$));
printf(($g"."$, char split(beetles, ", "),$l$))
)</
{{out}}
<pre>
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.
<syntaxhighlight lang="hopper">
#include <hopper.h>
Line 668:
back
</syntaxhighlight>
{{Out}}
<pre>Output:
Line 688:
=={{header|APL}}==
<
abc. 123. X. ⍝ 3 strings (char vectors), each with a period at the end.
</syntaxhighlight>
=={{header|AppleScript}}==
<
intercalate(".", splitOn(",", "Hello,How,Are,You,Today"))
end run
Line 713:
set my text item delimiters to dlm
return strJoined
end intercalate</
{{Out}}
<pre>Hello.How.Are.You.Today</pre>
Line 719:
Or,
<
set tokens to the text items of "Hello,How,Are,You,Today"
set my text item delimiters to "."
log tokens as text</
{{Out}}
Line 731:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
Line 895:
bx lr
</syntaxhighlight>
=={{header|Arturo}}==
<
print join.with:"." split.by:"," str</
{{out}}
Line 907:
=={{header|Astro}}==
<
let tokens = text.split(||,||)
print tokens.join(with: '.')</
=={{header|AutoHotkey}}==
<
stringsplit, string, string, `,
loop, % string0
{
msgbox % string%A_Index%
}</
=={{header|AWK}}==
<
s = "Hello,How,Are,You,Today"
split(s, arr, ",")
Line 928:
}
print
}</
A more ''idiomatic'' way for AWK is
<
{
for(i=1; i <= NF; i++) printf $i ".";
print ""
}</
which "tokenize" each line of input and this is achieved by using "," as field separator
Line 942:
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
<
110 GOSUB 200"TOKENIZE
120 FOR I = 1 TO N
Line 961:
290 A$(N) = A$(N) + C$
300 NEXT TI
310 RETURN</
==={{header|BaCon}}===
BaCon includes extensive support for ''delimited strings''.
<
string$ = "Hello,How,Are,You,Today"
Line 976:
' Or simply replace the delimiter
PRINT DELIM$(string$, ",", ".")</
{{out}}
Line 984:
==={{header|BASIC256}}===
<
tokens$ = explode(instring$,",")
Line 990:
print tokens$[i]; ".";
next i
end</
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<
text$ = "Hello,How,Are,You,Today"
Line 1,002:
PRINT array$(i%) "." ;
NEXT
PRINT</
==={{header|Chipmunk Basic}}===
Solutions [[#Applesoft BASIC|Applesoft BASIC]] and [[#Commodore BASIC|Commodore BASIC]] work without changes.
==={{header|Commodore BASIC}}===
Based on the AppleSoft BASIC version.
<
20 T$ = "HELLO,HOW,ARE,YOU,TODAY"
30 GOSUB 200, TOKENIZE
Line 1,023 ⟶ 1,025:
260 N = N + 1
270 NEXT L
280 RETURN</syntaxhighlight>
==={{header|FreeBASIC}}===
<
redim tokens(0 to 0) as string
dim as string*1 ch
Line 1,047 ⟶ 1,049:
for i as uinteger = 0 to ubound(tokens)
print tokens(i);".";
next i</
==={{header|Liberty BASIC}}===
<
For i = 0 To 4
array$(i) = Word$("Hello,How,Are,You,Today", (i + 1), ",")
Line 1,056 ⟶ 1,058:
Next i
Print Left$(array$, (Len(array$) - 1))</
==={{header|
The [[#Commodore BASIC|Commodore BASIC]] solution works without any changes.
==={{header|PowerBASIC}}===
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.)
<
DIM parseMe AS STRING
parseMe = "Hello,How,Are,You,Today"
Line 1,076 ⟶ 1,080:
MSGBOX outP
END FUNCTION</
==={{header|PureBasic}}===
'''As described
<
For i=1 To 5
Line 1,090 ⟶ 1,094:
ForEach MyStrings()
Print(MyStrings()+".")
Next</
'''Still, easier would be
<
==={{header|QBasic}}===
<
parseMe = "Hello,How,Are,You,Today"
Line 1,140 ⟶ 1,144:
PRINT "."; parsed(L0);
NEXT
END IF</
==={{header|Run BASIC}}===
<
FOR i = 1 to 5
textArray$(i) = word$(text$,i,",")
print textArray$(i);" ";
NEXT</
==={{header|VBScript}}===
====One liner====
<
In fact, the Visual Basic solution (below) could have done the same, as Join() is available.
Line 1,160 ⟶ 1,164:
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.
<
Dim parseMe As String, parsed As Variant
parseMe = "Hello,How,Are,You,Today"
Line 1,173 ⟶ 1,177:
MsgBox outP
End Sub</
=={{header|Batch File}}==
<
setlocal enabledelayedexpansion
call :tokenize %1 res
Line 1,187 ⟶ 1,191:
for %%i in (%str%) do set %2=!%2!.%%i
set %2=!%2:~1!
goto :eof</
''Demo''
>tokenize.cmd "Hello,How,Are,You,Today"
Hello.How.Are.You.Today
=={{header|BQN}}==
Uses a splitting idiom from bqncrate.
<syntaxhighlight lang="bqn">Split ← (+`׬)⊸-∘= ⊔ ⊢
∾⟜'.'⊸∾´ ',' Split "Hello,How,Are,You,Today"</syntaxhighlight>
{{out}}
<pre>"Hello.How.Are.You.Today"</pre>
=={{header|Bracmat}}==
Solution that employs string pattern matching to spot the commas
<
& :?ReverseList
& whl
Line 1,207 ⟶ 1,219:
)
& out$!List
)</
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.
<
& :?ReverseList
& whl
Line 1,221 ⟶ 1,233:
)
& out$!List
)</
=={{header|C}}==
Line 1,230 ⟶ 1,242:
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()''.
<
#include<stdio.h>
#include<stdlib.h>
Line 1,251 ⟶ 1,263:
return 0;
}</
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.
<
typedef void (*callbackfunc)(const char *);
Line 1,279 ⟶ 1,291:
tokenize(array, ',', doprint);
return 0;
}</
=={{header|C sharp|C#}}==
<
// or Regex.Split ( "Hello,How,Are,You,Today", "," );
// (Regex is in System.Text.RegularExpressions namespace)
string[] strings = str.Split(',');
Console.WriteLine(String.Join(".", strings));
</syntaxhighlight>
=={{header|C++}}==
Line 1,294 ⟶ 1,306:
std::getline() is typically used to tokenize strings on a single-character delimiter
<
#include <sstream>
#include <vector>
Line 1,309 ⟶ 1,321:
copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, "."));
std::cout << '\n';
}</
{{works with|C++98}}
C++ allows the user to redefine what is considered whitespace. If the delimiter is whitespace, tokenization becomes effortless.
<
#include <locale>
#include <sstream>
Line 1,338 ⟶ 1,350:
copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, "."));
std::cout << '\n';
}</
{{works with|C++98}}
Line 1,344 ⟶ 1,356:
The boost library has multiple options for easy tokenization.
<
#include <vector>
#include <iterator>
Line 1,357 ⟶ 1,369:
copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, "."))
std::cout << '\n';
}</
{{works with|C++23}}
C++20 and C++23 drastically improve the ergonomics of simple manipulation of ranges.
<syntaxhighlight lang="cpp">#include <string>
#include <ranges>
#include <iostream>
int main() {
std::string s = "Hello,How,Are,You,Today";
s = s // Assign the final string back to the string variable
| std::views::split(',') // Produce a range of the comma separated words
| std::views::join_with('.') // Concatenate the words into a single range of characters
| std::ranges::to<std::string>(); // Convert the range of characters into a regular string
std::cout << s;
}</syntaxhighlight>
=={{header|Ceylon}}==
{{works with|Ceylon 1.2}}
<
value input = "Hello,How,Are,You,Today";
value tokens = input.split(','.equals);
print(".".join(tokens));
}</
=={{header|CFEngine}}==
<
{
reports:
"${with}" with => join(".", splitstring("Hello,How,Are,You,Today", ",", 99));
}
</syntaxhighlight>
{{out}}
<pre>cf-agent -KIf ./tokenize-a-string.cf
Line 1,382 ⟶ 1,409:
=={{header|Clojure}}==
Using native Clojure functions and Java Interop:
<
Using the clojure.string library:
<
=={{header|CLU}}==
<
% and returns each substring in order.
tokenize = iter (s: string, c: char) yields (string)
Line 1,410 ⟶ 1,437:
stream$putl(po, part || ".")
end
end start_up</
{{out}}
<pre>Hello.
Line 1,421 ⟶ 1,448:
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">
identification division.
program-id. tokenize.
Line 1,452 ⟶ 1,479:
goback.
end program tokenize.
</syntaxhighlight>
{{out}}
Line 1,462 ⟶ 1,489:
=={{header|CoffeeScript}}==
<
arr = "Hello,How,Are,You,Today".split ","
console.log arr.join "."
</syntaxhighlight>
=={{header|ColdFusion}}==
=== Classic tag based CFML ===
<
<cfoutput>
<cfset wordListTag = "Hello,How,Are,You,Today">
#Replace( wordListTag, ",", ".", "all" )#
</cfoutput>
</syntaxhighlight>
{{Output}}
<pre>
Line 1,481 ⟶ 1,508:
=== Script Based CFML ===
<
wordList = "Hello,How,Are,You,Today";
splitList = replace( wordList, ",", ".", "all" );
writeOutput( splitList );
</cfscript></
{{Output}}
<pre>
Line 1,495 ⟶ 1,522:
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.
<
(loop for start = 0 then (1+ finish)
for finish = (position #\, string :start start)
Line 1,502 ⟶ 1,529:
(defun write-with-periods (strings)
(format t "~{~A~^.~}" strings))</
=={{header|Cowgol}}==
<
include "strings.coh";
Line 1,544 ⟶ 1,571:
print(".\n");
i := i + 1;
end loop;</
{{out}}
<pre>Hello.
Line 1,553 ⟶ 1,580:
=={{header|Crystal}}==
<
{{out}}
<pre>Hello.How.Are.You.Today</pre>
=={{header|D}}==
<
import std.stdio, std.string;
"Hello,How,Are,You,Today".split(',').join('.').writeln;
}</
{{out}}
<pre>Hello.How.Are.You.Today</pre>
Line 1,569 ⟶ 1,596:
=== Using String.split ===
{{libheader| System.SysUtils}}
<syntaxhighlight lang="delphi">
program Tokenize_a_string;
Line 1,587 ⟶ 1,614:
end.
</syntaxhighlight>
=== Using TStringList ===
<syntaxhighlight lang="delphi">
program TokenizeString;
Line 1,623 ⟶ 1,650:
end.
</syntaxhighlight>
The result is:
<syntaxhighlight lang="delphi">
Hello
How
Line 1,633 ⟶ 1,660:
You
Today
</syntaxhighlight>
=={{header|dt}}==
<syntaxhighlight lang="dt">"Hello,How,Are,You,Today" "," split "." join pl</syntaxhighlight>
=={{header|Dyalect}}==
<syntaxhighlight lang="dyalect">var str = "Hello,How,Are,You,Today"
var strings = str.Split(',')
print(values: strings, separator: ".")</
{{out}}
<pre>Hello.How.Are.You.Today</pre>
=={{header|Déjà Vu}}==
<
{{out}}
<pre>Hello.How.Are.You.Today</pre>
=={{header|E}}==
<
=={{header|EasyLang}}==
<syntaxhighlight lang=text>
s$ = "Hello,How,Are,You,Today"
a$[] = strsplit s$ ","
for s$ in a$[]
write s$ & "."
.
</syntaxhighlight>
=={{header|ed}}==
It's a cheating, but still. Replacing commas by periods fulfills the requirements. If not for the period requirement, the code would split commas into newlines for readability
<syntaxhighlight lang="sed">
# by Artyom Bologov
H
,p
s/,/./g
,p
Q
</syntaxhighlight>
{{out}}
<pre>$ cat tokenize.ed | ed -lEGs tokenize.input
Newline appended
Hello,How,Are,You,Today
Hello.How.Are.You.Today</pre>
=={{header|Elena}}==
ELENA
<
import extensions;
public program()
{
string.splitBy
{
console.print(s,".")
}
}</
=={{header|Elixir}}==
<
tokens = String.split("Hello,How,Are,You,Today", ",")
IO.puts Enum.join(tokens, ".")
</syntaxhighlight>
=={{header|EMal}}==
<syntaxhighlight lang="emal">
text value = "Hello,How,Are,You,Today"
List tokens = value.split(",")
writeLine(tokens.join("."))
# single line version
writeLine("Hello,How,Are,You,Today".split(",").join("."))
</syntaxhighlight>
{{out}}
<pre>
Hello.How.Are.You.Today
Hello.How.Are.You.Today
</pre>
=={{header|Erlang}}==
<
-export([start/0]).
Line 1,681 ⟶ 1,751:
Lst = string:tokens("Hello,How,Are,You,Today",","),
io:fwrite("~s~n", [string:join(Lst,".")]),
ok.</
=={{header|Euphoria}}==
<
sequence out
integer first, delim
Line 1,705 ⟶ 1,775:
for i = 1 to length(s) do
puts(1, s[i] & ',')
end for</
=={{header|F_Sharp|F#}}==
<
=={{header|Factor}}==
<
=={{header|Falcon}}==
'''VBA/Python programmer's approach to this solution, not sure if it's the most falconic way'''
<
/* created by Aykayayciti Earl Lamont Montgomery
April 9th, 2018 */
Line 1,729 ⟶ 1,799:
> b
</syntaxhighlight>
{{out}}
<pre>
Line 1,740 ⟶ 1,810:
A string can be split on a given character, returning a list of the intervening strings.
<
class Main
{
Line 1,753 ⟶ 1,823:
}
}
</syntaxhighlight>
=={{header|Fennel}}==
{{trans|Lua}}
<
(let [pattern (string.format "([^%s]+)" sep)
fields {}]
Line 1,764 ⟶ 1,834:
(let [str "Hello,How,Are,You,Today"]
(print (table.concat (str:split ",") ".")))</
=={{header|Forth}}==
There is no standard string split routine, but it is easily written. The results are saved temporarily to the dictionary.
<
here >r 2swap
begin
Line 1,786 ⟶ 1,856:
1 ?do dup 2@ type ." ." cell+ cell+ loop 2@ type ;
s" Hello,How,Are,You,Today" s" ," split .tokens \ Hello.How.Are.You.Today</
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<
CHARACTER(23) :: str = "Hello,How,Are,You,Today"
Line 1,812 ⟶ 1,882:
END DO
END PROGRAM Example</
=={{header|Frink}}==
<
println[join[".", split[",", "Hello,How,Are,You,Today"]]]
</syntaxhighlight>
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
window 1, @"Tokenize a string"
void local fn DoIt
CFStringRef string = @"Hello,How,Are,You,Today"
CFArrayRef tokens = fn StringComponentsSeparatedByString( string, @"," )
print fn ArrayComponentsJoinedByString( tokens, @"." )
end fn
fn DoIt
HandleEvents
</syntaxhighlight>
{{out}}
<pre>
Hello.How.Are.You.Today
</pre>
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=218e240236cdf1419a405abfed906ed3 Click this link to run this code]'''
<
Dim sString As String[] = Split("Hello,How,Are,You,Today")
Print sString.Join(".")
End</
Output:
<pre>
Line 1,833 ⟶ 1,922:
=={{header|GAP}}==
<
# [ "Hello", "How", "Are", "You", "Today" ]
JoinStringsWithSeparator(last, ".");
# "Hello.How.Are.You.Today"</
=={{header|Genie}}==
<
init
Line 1,846 ⟶ 1,935:
words:array of string[] = str.split(",")
joined:string = string.joinv(".", words)
print joined</
{{out}}
Line 1,854 ⟶ 1,943:
=={{header|Go}}==
<
import (
Line 1,864 ⟶ 1,953:
s := "Hello,How,Are,You,Today"
fmt.Println(strings.Join(strings.Split(s, ","), "."))
}</
=={{header|Groovy}}==
<
=={{header|Haskell}}==
'''Using Data.Text'''
<
import Data.Text (splitOn,intercalate)
import qualified Data.Text.IO as T (putStrLn)
main = T.putStrLn . intercalate "." $ splitOn "," "Hello,How,Are,You,Today"</
Output: Hello.How.Are.You.Today
Line 1,884 ⟶ 1,973:
The necessary operations are unfortunately not in the standard library (yet), but simple to write:
<
splitBy _ [] = []
splitBy f list = first : splitBy f (dropWhile f rest) where
Line 1,899 ⟶ 1,988:
-- using regular expression to split:
import Text.Regex
putStrLn $ joinWith "." $ splitRegex (mkRegex ",") $ "Hello,How,Are,You,Today"</
Tokenizing can also be realized by using unfoldr and break:
<
Hello
How
Are
You
Today</
* You need to import the modules Data.List and Control.Arrow
Line 1,913 ⟶ 2,002:
=={{header|HicEst}}==
<
nWords = INDEX(string, ',', 256) + 1
Line 1,925 ⟶ 2,014:
DO i = 1, nWords
WRITE(APPend) TRIM(CHAR(i, maxWordLength, list)), '.'
ENDDO</
=={{header|Icon}} and {{header|Unicon}}==
<
A := []
"Hello,How,Are,You,Today" ? {
Line 1,936 ⟶ 2,025:
every writes(!A,".")
write()
end</
{{out}}
Line 1,946 ⟶ 2,035:
A Unicon-specific solution is:
<
procedure main()
Line 1,952 ⟶ 2,041:
every writes(!A,".")
write()
end</
One wonders what the expected output should be with the input string ",,,,".
=={{header|Io}}==
<
=={{header|J}}==
<
] t=: <;._1 ',',s
+-----+---+---+---+-----+
Line 1,969 ⟶ 2,058:
'.' (I.','=s)}s NB. two steps combined
Hello.How.Are.You.Today</
Alternatively using the system library/script <tt>strings</tt>
<
',' splitstring s
+-----+---+---+---+-----+
Line 1,979 ⟶ 2,068:
'.' joinstring ',' splitstring s
Hello.How.Are.You.Today</
<tt>splitstring</tt> and <tt>joinstring</tt> also work with longer "delimiters":
<
"Hello","How","Are","You","Today"</
But, of course, this could be solved with simple string replacement:
<
Hello.How.Are.You.Today</
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 ⟶ 2,083:
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:
<
Here '''fn''' is applied to each ',' delimited substring and the results are assembled into an array.
Or, factoring out the names:
<
=={{header|Java}}==
Line 2,010 ⟶ 2,099:
{{works with|Java|1.8+}}
<
System.out.println(String.join(".", toTokenize.split(",")));</
{{works with|Java|1.4+}}
<
String words[] = toTokenize.split(",");//splits on one comma, multiple commas yield multiple splits
Line 2,020 ⟶ 2,109:
for(int i=0; i<words.length; i++) {
System.out.print(words[i] + ".");
}</
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+}}
<
StringTokenizer tokenizer = new StringTokenizer(toTokenize, ",");
while(tokenizer.hasMoreTokens()) {
System.out.print(tokenizer.nextToken() + ".");
}</
=={{header|JavaScript}}==
<syntaxhighlight lang="javascript">console.log(
"Hello,How,Are,You,Today"
.split(",")
.join(".")
);</syntaxhighlight>A more advanced program to tokenise strings:<syntaxhighlight lang="javascript" line="1">
const Tokeniser = (function () {
const numberRegex = /-?(\d+\.d+|\d+\.|\.\d+|\d+)((e|E)(\+|-)?\d+)?/g;
return {
settings: {
operators: ["<", ">", "=", "+", "-", "*", "/", "?", "!"],
separators: [",", ".", ";", ":", " ", "\t", "\n"],
groupers: ["(", ")", "[", "]", "{", "}", '"', '"', "'", "'"],
keepWhiteSpacesAsTokens: false,
trimTokens: true
},
isNumber: function (value) {
if (typeof value === "number") {
return true;
} else if (typeof value === "string") {
return numberRegex.test(value);
}
return false;
},
closeGrouper: function (grouper) {
if (this.settings.groupers.includes(grouper)) {
return this.settings.groupers[this.settings.groupers.indexOf(grouper) + 1];
}
return null;
},
tokenType: function (char) {
if (this.settings.operators.includes(char)) {
return "operator";
} else if (this.settings.separators.includes(char)) {
return "separator";
} else if (this.settings.groupers.includes(char)) {
return "grouper";
}
return "other";
},
parseString: function (str) {
if (typeof str !== "string") {
if (str === null) {
return "null";
} if (typeof str === "object") {
str = JSON.stringify(str);
} else {
str = str.toString();
}
}
let tokens = [], _tempToken = "";
for (let i = 0; i < str.length; i++) {
if (this.tokenType(_tempToken) !== this.tokenType(str[i]) || this.tokenType(str[i]) === "separator") {
if (_tempToken.trim() !== "") {
tokens.push(this.settings.trimTokens ? _tempToken.trim() : _tempToken);
} else if (this.settings.keepWhiteSpacesAsTokens) {
tokens.push(_tempToken);
}
_tempToken = str[i];
if (this.tokenType(_tempToken) === "separator") {
if (_tempToken.trim() !== "") {
tokens.push(this.settings.trimTokens ? _tempToken.trim() : _tempToken);
} else if (this.settings.keepWhiteSpacesAsTokens) {
tokens.push(_tempToken);
}
_tempToken = "";
}
} else {
_tempToken += str[i];
}
}
if (_tempToken.trim() !== "") {
tokens.push(this.settings.trimTokens ? _tempToken.trim() : _tempToken);
} else if (this.settings.keepWhiteSpacesAsTokens) {
tokens.push(_tempToken);
}
return tokens.filter((token) => token !== "");
}
};
})();
</syntaxhighlight>Output:<syntaxhighlight lang="javascript">
Tokeniser.parseString("Hello,How,Are,You,Today");
</syntaxhighlight>
=={{header|jq}}==
<
"Hello,How,Are,You,Today"
Hello.How.Are.You.Today</
=={{header|Jsish}}==
Being in the ECMAScript family, Jsi is blessed with many easy to use character, string and array manipulation routines.
<
{{out}}
<pre>Hello.How.Are.You.Today</pre>
=={{header|Julia}}==
<syntaxhighlight lang="julia">
s = "Hello,How,Are,You,Today"
a = split(s, ",")
Line 2,058 ⟶ 2,228:
println("Splits into ", a)
println("Reconstitutes to \"", t, "\"")
</syntaxhighlight>
{{out}}
Line 2,068 ⟶ 2,238:
=={{header|K}}==
<
"." /: words</
{{out}}
Line 2,075 ⟶ 2,245:
"Hello.How.Are.You.Today"
</pre>
{{works with|ngn/k}}<syntaxhighlight lang=K>","\"Hello,How,Are,You,Today"
("Hello"
"How"
"Are"
"You"
"Today")</syntaxhighlight>
=={{header|Klingphix}}==
<
nl "End " input</
{{out}}
<pre>Hello.How.Are.You.Today.
Line 2,086 ⟶ 2,263:
=={{header|Kotlin}}==
{{works with|Kotlin|1.0b4}}
<
val input = "Hello,How,Are,You,Today"
println(input.split(',').joinToString("."))
}</
{{out}}
<pre>Hello.How.Are.You.Today</pre>
=={{header|Ksh}}==
<
#!/bin/ksh
Line 2,125 ⟶ 2,302:
######
_tokenize "${string}" "${inputdelim}" "${outputdelim}"</
{{out}}<pre>Hello.How.Are.You.Today</pre>
Line 2,133 ⟶ 2,310:
=={{header|Lambdatalk}}==
<
{S.replace , by . in Hello,How,Are,You,Today}.
-> Hello.How.Are.You.Today.
</syntaxhighlight>
=={{header|Lang}}==
<syntaxhighlight lang="lang">
$str = Hello,How,Are,You,Today
fn.println(fn.join(\., fn.split($str, \,)))
</syntaxhighlight>
=={{header|Lang5}}==
<
=={{header|LDPL}}==
<
DATA:
explode/words is text vector
Line 2,194 ⟶ 2,377:
add 1 and i in i
repeat
</syntaxhighlight>
=={{header|LFE}}==
<
> (set split (string:tokens "Hello,How,Are,You,Today" ","))
("Hello" "How" "Are" "You" "Today")
> (string:join split ".")
"Hello.How.Are.You.Today"
</syntaxhighlight>
=={{header|Lingo}}==
<
_player.itemDelimiter = ","
output = ""
Line 2,214 ⟶ 2,397:
delete the last char of output
put output
-- "Hello.How.Are.You.Today"</
=={{header|Logo}}==
{{works with|UCB Logo}}
<
output parse map [ifelse ? = :sep ["| |] [?]] :str
end</
This form is more robust, doing the right thing if there are embedded spaces.
<
if empty? :str [output lput :w :acc]
ifelse equal? first :str :by ~
[output (split butfirst :str :by lput :w :acc)] ~
[output (split butfirst :str :by :acc lput first :str :w)]
end</
<
[Hello How Are You Today]</
=={{header|Logtalk}}==
Using Logtalk built-in support for Definite Clause Grammars (DCGs) and representing the strings as atoms for readbility:
<
:- object(spliting).
Line 2,258 ⟶ 2,441:
:- end_object.
</syntaxhighlight>
{{out}}
<pre>
Line 2,268 ⟶ 2,451:
=={{header|Lua}}==
Split function callously stolen from the lua-users wiki
<
local sep, fields = sep or ":", {}
local pattern = string.format("([^%s]+)", sep)
Line 2,276 ⟶ 2,459:
local str = "Hello,How,Are,You,Today"
print(table.concat(str:split(","), "."))</
{{out}}
<pre>Hello.How.Are.You.Today</pre>
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module CheckIt {
Function Tokenize$(s){
Line 2,294 ⟶ 2,477:
}
Checkit
</syntaxhighlight>
=={{header|M4}}==
<
define(`set',`define(`$1[$2]',`$3')')
define(`get',`defn($1[$2])')
Line 2,307 ⟶ 2,490:
define(`show',
`ifelse(eval(j<n),1,`get(a,j).`'define(`j',incr(j))`'show')')
show</
{{out}}
Line 2,315 ⟶ 2,498:
=={{header|Maple}}==
<
{{Out|Output}}
<pre>"Hello.How.Are.You.Today"</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
=={{header|MATLAB}} / {{header|Octave}}==
<syntaxhighlight lang="matlab">
s=strsplit('Hello,How,Are,You,Today',',')
fprintf(1,'%s.',s{:})
</syntaxhighlight>
{{out}}
Line 2,334 ⟶ 2,517:
=={{header|Maxima}}==
<
printf(true, "~{~a~^.~}~%", l)$</
A slightly different way
<syntaxhighlight lang="maxima">
split("Hello,How,Are,You,Today",",")$
simplode(%,".");
</syntaxhighlight>
{{out}}
<pre>
"Hello.How.Are.You.Today"
</pre>
=={{header|MAXScript}}==
<
for word in (filterString "Hello,How,Are,You,Today" ",") do
(
output += (word + ".")
)
format "%\n" output</
=={{header|Mercury}}==
<syntaxhighlight lang="text">
:- module string_tokenize.
:- interface.
Line 2,359 ⟶ 2,552:
Tokens = string.split_at_char((','), "Hello,How,Are,You,Today"),
io.write_list(Tokens, ".", io.write_string, !IO),
io.nl(!IO).</
=={{header|min}}==
{{works with|min|0.19.3}}
<
=={{header|MiniScript}}==
<
print tokens.join(".")</
=={{header|MMIX}}==
<
EOS IS 0
NL IS 10
Line 2,423 ⟶ 2,616:
LDBU t,tp
PBNZ t,2B % UNTIL EOB(uffer)
TRAP 0,Halt,0</
{{out}}
<pre>
Line 2,435 ⟶ 2,628:
=={{header|Modula-3}}==
<
IMPORT IO, TextConv;
Line 2,452 ⟶ 2,645:
END;
IO.Put("\n");
END Tokenize.</
=={{header|MUMPS}}==
<
NEW I,J,INP
SET INP="Hello,how,are,you,today"
Line 2,461 ⟶ 2,654:
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"
QUIT</
In use:
Line 2,468 ⟶ 2,661:
=={{header|Nanoquery}}==
<
print word + "."
end</
{{out}}
<pre>Hello.How.Are.You.Today.</pre>
=={{header|Nemerle}}==
<
using System.Console;
using Nemerle.Utility.NString;
Line 2,488 ⟶ 2,681:
// a quick in place list comprehension takes care of that
}
}</
=={{header|NetRexx}}==
<
* 20.08.2012 Walter Pachl derived from REXX Version 3
**********************************************************************/
Line 2,502 ⟶ 2,695:
Say ss.word(i)'.'
End
Say 'End-of-list.'</
Output as in REXX version
=={{header|NewLISP}}==
<
=={{header|Nial}}==
Line 2,514 ⟶ 2,707:
Define Array with input string:
<
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|H|e|l|l|o|,|H|o|w|,|A|r|e|,|Y|o|u|,|T|o|d|a|y|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+</
Split string at the commas:
<
+-----------+-------+-------+-------+-----------+
|+-+-+-+-+-+|+-+-+-+|+-+-+-+|+-+-+-+|+-+-+-+-+-+|
||H|e|l|l|o|||H|o|w|||A|r|e|||Y|o|u|||T|o|d|a|y||
|+-+-+-+-+-+|+-+-+-+|+-+-+-+|+-+-+-+|+-+-+-+-+-+|
+-----------+-------+-------+-------+-----------+</
Join string with <code>.</code> and remove last <code>.</code>
<
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|H|e|l|l|o|.|H|o|w|.|A|r|e|.|Y|o|u|.|T|o|d|a|y|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+</
Less cluttered display, using <code>set "sketch;set "nodecor</code> display switches.
<
Hello,How,Are,You,Today
t:= s eachall = `, cut s
Line 2,544 ⟶ 2,737:
+-----+---+---+---+-----+
u:=front content (cart t `.)
Hello.How.Are.You.Today</syntaxhighlight>
Or as a one-liner:
<syntaxhighlight lang="nial">
front content (cart (s eachall = `, cut s) `.)
</syntaxhighlight>
=={{header|Nim}}==
<
let text = "Hello,How,Are,You,Today"
let tokens = text.split(',')
echo tokens.join(".")</
{{out}}
<pre>Hello.How.Are.You.Today</pre>
=={{header|Nu}}==
<syntaxhighlight lang="nu">'Hello,How,Are,You,Today' | split row ',' | str join '.'</syntaxhighlight>
{{out}}
<pre>Hello.How.Are.You.Today</pre>
=={{header|Objeck}}==
<
class Parse {
function : Main(args : String[]) ~ Nil {
Line 2,565 ⟶ 2,769:
};
}
}</
=={{header|Objective-C}}==
Line 2,572 ⟶ 2,776:
{{works with|Cocoa}}
<
NSArray *tokens = [text componentsSeparatedByString:@","];
NSString *result = [tokens componentsJoinedByString:@"."];
NSLog(result);</
=={{header|OCaml}}==
To split on a single-character separator:
<
String.concat "." words
</syntaxhighlight>
The function split_on_char has been introduced in OCaml 4.04. In previous versions, it could be implemented by:
<
let r = ref [] in
let j = ref (String.length s) in
Line 2,594 ⟶ 2,798:
end
done;
String.sub s 0 !j :: !r</
=={{header|Oforth}}==
<
{{out}}
Line 2,606 ⟶ 2,810:
=={{header|ooRexx}}==
<
do while text \= ''
parse var text word1 ',' text
call charout 'STDOUT:',word1'.'
end</
{{out}}
<pre>Hello.How.Are.You.Today.</pre>
=={{header|OpenEdge/Progress}}==
<
i_c AS CHAR
):
Line 2,638 ⟶ 2,842:
MESSAGE
tokenizeString( "Hello,How,Are,You,Today" )
VIEW-AS ALERT-BOX.</
{{out}}
<pre>
Line 2,651 ⟶ 2,855:
=={{header|Oz}}==
<
{System.printInfo T#"."}
end</
=={{header|PARI/GP}}==
Line 2,663 ⟶ 2,867:
{{Works with|PARI/GP|2.7.4 and above}}
<
\\ 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
Line 2,681 ⟶ 2,885:
print("3.",tokenize(",Hello,,How,Are,You,Today",","));
}
</
{{Output}}
Line 2,699 ⟶ 2,903:
{{Works with|PARI/GP|2.7.4 and above}}
<
\\ 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
Line 2,728 ⟶ 2,932:
print("7. 0 pp: ", stok("",","));
}
</
{{Output}}
Line 2,744 ⟶ 2,948:
=={{header|Pascal}}==
{{works with|Free_Pascal}}
<
{$mode objfpc}{$H+}
Line 2,768 ⟶ 2,972:
Tokens.Free;
end;
end.</
The result is:
Tokenize from: "Hello,How,Are,You,Today"
to: "Hello.How.Are.You.Today"
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
begin
var s := 'Hello,How,Are,You,Today';
var strings := s.Split(',');
Print(strings.JoinToString('.'));
end.
</syntaxhighlight>
=={{header|Perl}}==
<
CLI one-liner form:
<
which is a compact way of telling Perl to do
<
LINE: while (defined($_ = <ARGV>)) {
chomp $_;
Line 2,788 ⟶ 3,001:
continue {
die "-p destination: $!\n" unless print $_;
}</
=={{header|Phix}}==
<!--<
<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>
<!--</
{{Out}}
<pre>
Line 2,800 ⟶ 3,013:
=={{header|Phixmonti}}==
<
"Hello,How,Are,You,Today" "," " " subst split len for get print "." print endfor</
=={{header|PHP}}==
{{works with|PHP|5.x}}
<
$str = 'Hello,How,Are,You,Today';
echo implode('.', explode(',', $str));
?></
=={{header|Picat}}==
Using the built-in functions <code>split/2</code> and <code>join/2</code>.
<syntaxhighlight lang="picat">import util.
go =>
S = "Hello,How,Are,You,Today",
T = S.split(","),
println(T),
T.join(".").println(),
% As a one liner:
S.split(",").join(".").println().</syntaxhighlight>
{{out}}
<pre>[Hello,How,Are,You,Today]
Hello.How.Are.You.Today
Hello.How.Are.You.Today</pre>
=={{header|PicoLisp}}==
<
(split (chop "Hello,How,Are,You,Today") ",") )</
=={{header|Pike}}==
<
=={{header|PL/I}}==
<
declare s character (100) initial ('Hello,How,Are,You,Today');
declare n fixed binary (31);
Line 2,841 ⟶ 3,072:
put skip list (string(table));
end;
end;</
{{out}}
<pre>Hello.How.Are.You.Today</pre>
=={{header|PL/M}}==
<
/* CP/M CALLS */
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
Line 2,885 ⟶ 3,116:
CALL EXIT;
EOF;</
{{out}}
<pre>HELLO. HOW. ARE. YOU. TODAY. </pre>
=={{header|Plain English}}==
<
Start up.
Split "Hello,How,Are,You,Today" into some string things given the comma byte.
Line 2,906 ⟶ 3,137:
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.
Repeat.</
{{out}}
<pre>
Line 2,919 ⟶ 3,150:
First show the use of sysparse_string to break up a string and make a list of strings.
<
lvars list;
sysparse_string('the cat sat on the mat') -> list;
;;; print the list of strings
list =>
** [the cat sat on the mat]</
By giving it an extra parameter 'true' we can make it recognize numbers and produce a list of strings and numbers
<
sysparse_string('one 1 two 2 three 3 four 4', true) -> list;
;;; print the list of strings and numbers
Line 2,937 ⟶ 3,168:
** <true>
isinteger(list(2))=>
** <true></
Now show some uses of the built in procedure sys_parse_string, which allows more options:
<
true -> pop_pr_quotes;
;;;
Line 2,955 ⟶ 3,186:
;;; print the list of strings
strings =>
** ['Hello' 'How' 'Are' 'You' 'Today']</
If {% ... %} were used instead of [% ... %] the result would be
a vector (i.e. array) of strings rather than a list of strings.
<
;;; print the vector
strings =>
** {'Hello' 'How' 'Are' 'You' 'Today'}</
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:
<
{% sys_parse_string('100 101 102 103 99.9 99.999', strnumber) %} -> numbers;
;;; the result is a vector containing integers and floats,
;;; which can be printed thus:
numbers =>
** {100 101 102 103 99.9 99.999}</
Using lower level pop-11 facilities to tokenise the string:
<
lvars str='Hello,How,Are,You,Today';
;;; Iterate over string
Line 2,994 ⟶ 3,225:
endif;
;;; Reverse the list
rev(ls) -> ls;</
Since the task requires to use array we convert list to array
<
destlist(ls);
;;; Build a vector from them
Line 3,006 ⟶ 3,237:
printf(ar(i), '%s.');
endfor;
printf('\n');</
We could use list directly for printing:
<
printf(i, '%s.');
endfor;</
so the conversion to vector is purely to satisfy task formulation.
Line 3,018 ⟶ 3,249:
=={{header|PowerShell}}==
{{works with|PowerShell|1}}
<
[string]::Join('.', $words)</
{{works with|PowerShell|2}}
<
$words -join '.'</
{{works with|PowerShell|2}}
The StringSplitOptions enumeration weeds out the return of empty elements.
<syntaxhighlight lang="powershell">
"Hello,How,Are,You,Today", ",,Hello,,Goodbye,," | ForEach-Object {($_.Split(',',[StringSplitOptions]::RemoveEmptyEntries)) -join "."}
</syntaxhighlight>
{{Out}}
<pre>
Line 3,038 ⟶ 3,269:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<
splitup(Sep,[A|AL],B) --> [A], {\+ [A] = Sep }, splitup(Sep,AL,B).
splitup(Sep,[],[B|BL]) --> Sep, splitup(Sep,B,BL).
Line 3,046 ⟶ 3,277:
phrase(splitup(".",Tokens),Backtogether),
string_to_list(ABack,Backtogether),
writeln(ABack).</
{{out}}
<pre>
Line 3,058 ⟶ 3,289:
this can be accomplished in a few lines in the top level:
<
?- split_string("Hello,How,Are,You,Today", ",", "", Split),
| atomics_to_string(Split, ".", PeriodSeparated),
| writeln(PeriodSeparated).
Hello.How.Are.You.Today
</syntaxhighlight>
=={{header|Python}}==
{{works with|Python|2.5}}{{works with|Python|3.0}}
<
tokens = text.split(',')
print ('.'.join(tokens))</
Or if interpretation of the task description means you don't need to keep an intermediate array:
<
=={{header|Q}}==
<
"." sv words</
{{out}}
Line 3,084 ⟶ 3,315:
=={{header|QB64}}==
''CBTJD'': 2020/03/12
<
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.
Line 3,102 ⟶ 3,333:
PRINT LEFT$(tf$, LEN(tf$) - 1) ' | Print all but the last period of tf$.
END ' | Program end.
</syntaxhighlight>
'''Alternative method using word$ function:'''
----
''CBTJD'': 2020/03/12
<
DIM t$(LEN(a$) / 2) ' | Create an overestimated sized array.
FOR nd = 1 TO LEN(a$) ' | Start loop to find each comma.
Line 3,135 ⟶ 3,366:
DONE: ' | Label for bail destination of word count error check.
END FUNCTION ' | End of function.
</syntaxhighlight>
=={{header|Quackery}}==
<
witheach
[ dup char , = iff
Line 3,148 ⟶ 3,379:
[ witheach [ echo$ say "." ] ] is display ( [ --> )
$ "Hello,How,Are,You,Today" tokenise display</
{{Out}}
Line 3,155 ⟶ 3,386:
=={{header|R}}==
<
junk <- strsplit(text, split=",")
print(paste(unlist(junk), collapse="."))</
or the one liner
<
=={{header|Racket}}==
<
#lang racket
(string-join (string-split "Hello,How,Are,You,Today" ",") ".")
;; -> "Hello.How.Are.You.Today"
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|#22 "Thousand Oaks"}}
<syntaxhighlight lang="raku"
Or with function calls:
<syntaxhighlight lang="raku"
=={{header|Raven}}==
<
=={{header|REBOL}}==
<
tokens: parse original ","
dotted: "" repeat i tokens [append dotted rejoin [i "."]]
print ["Dotted: " dotted]</
{{out}}
Line 3,196 ⟶ 3,427:
=={{header|Red}}==
<
>> tokens: split str ","
>> probe tokens
Line 3,203 ⟶ 3,434:
>> 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
Hello.How.Are.You.Today</
=={{header|Retro}}==
<syntaxhighlight lang="retro">{{
: char ( -$ ) " " ;
: tokenize ( $-$$ )
Line 3,218 ⟶ 3,449:
[ tokenize action dup 1 <> ] while drop
^buffer'get drop ;
}}</
This will suffice to split a string into an array of substrings. It is used like this:
<
"Hello,How,Are,You,Today" ', strings split</
Since the buffer' vocabulary creates a zero-terminated buffer, we can display it using the each@ combinator and a simple quote:
<
=={{header|REXX}}==
===version 1===
This REXX version doesn't append a period to the last word in the list.
<
original = 'Hello,How,Are,You,Today' /*some words separated by commas (,). */
say 'The input string:' original /*display original string ──► terminal.*/
Line 3,244 ⟶ 3,475:
say @.j || left(., j\==#) /*maybe append a period (.) to a word. */
end /*j*/ /* [↑] don't append a period if last. */
say center(' End─of─list ', 40, "═") /*display a (EOL) trailer for the list.*/</
{{out|output|text= when using the internal default input:}}
<pre>
Line 3,262 ⟶ 3,493:
Hello,Betty Sue,How,Are,You,Today
<
sss='Hello,How,Are,You,Today'
say 'input string='sss
Line 3,273 ⟶ 3,504:
say word(ss,i)dot
End
say 'End-of-list.'</
'''output''' is similar to REXX version 1.
=={{header|Ring}}==
<
see substr("Hello,How,Are,You,Today", ",", ".")
</syntaxhighlight>
=={{header|RPL}}==
The program below fully complies with the task requirements, e.g. the input string is converted to a list of words, then the list is converted to a string.
{{works with|Halcyon Calc|4.2.8}}
{| class="wikitable"
! RPL code
! Comment
|-
|
≪
"}" + "{" SWAP + STR→
1 OVER SIZE '''FOR''' j
DUP j GET →STR 2 OVER SIZE 1 - SUB j SWAP PUT
'''NEXT'''
"" 1 3 PICK SIZE '''FOR''' j
OVER j GET +
'''IF''' OVER SIZE j ≠ '''THEN''' "." + '''END'''
'''NEXT''' SWAP DROP
≫ '<span style="color:blue">'''TOKNZ'''</span>' STO
|
<span style="color:blue">'''TOKNZ'''</span> ''<span style="color:grey">( "word,word" → "word.word" )</span> ''
convert string into list (words being between quotes)
loop for each list item
convert it to a string, remove quotes at beginning and end
loop for each list item
add item to output string
if not last item, append "."
clean stack
return output string
|}
"Hello,How,Are,You,Today" <span style="color:blue">'''TOKNZ'''</span>
</pre>
'''Output:'''
<span style="color:grey"> 1:</span> "Hello.How.Are.You.Today"
If direct string-to-string conversion is allowed, then this one-liner for HP-48+ will do the job:
≪ 1 OVER SIZE '''FOR''' j '''IF''' DUP j DUP SUB "," == '''THEN''' j "." REPL '''END NEXT''' ≫ '<span style="color:blue">'''TOKNZ'''</span>' STO
=={{header|Ruby}}==
<
=={{header|Rust}}==
<
let s = "Hello,How,Are,You,Today";
let tokens: Vec<&str> = s.split(",").collect();
println!("{}", tokens.join("."));
}</
=={{header|S-lang}}==
<
print(strjoin(a, "."));</
{{out}}
Line 3,299 ⟶ 3,568:
=={{header|Scala}}==
<
=={{header|Scheme}}==
{{works with|Guile}}
<
(define s "Hello,How,Are,You,Today")
(define words (map match:substring (list-matches "[^,]+" s)))
Line 3,310 ⟶ 3,579:
(display (list-ref words n))
(if (< n (- (length words) 1))
(display ".")))</
(with SRFI 13)
<
(define words (string-tokenize s (char-set-complement (char-set #\,))))
(define t (string-join words "."))</
{{works with|Gauche Scheme}}
<
(string-join
(string-split "Hello,How,Are,You,Today" #\,)
".")) </
{{output}}
<pre>
Line 3,328 ⟶ 3,597:
=={{header|Seed7}}==
<
tokens := split("Hello,How,Are,You,Today", ",");</
=={{header|Self}}==
<
((s splitOn: ',') joinUsing: '.') printLine.
</syntaxhighlight>
=={{header|Sidef}}==
<
=={{header|Simula}}==
<
CLASS TEXTARRAY(N); INTEGER N;
Line 3,390 ⟶ 3,659:
END.
</syntaxhighlight>
{{out}}
<pre>HELLO.HOW.ARE.YOU.TODAY.</pre>
=={{header|Slate}}==
<
=={{header|Slope}}==
<syntaxhighlight lang="slope">(display
(list->string
(string->list
"Hello,How,Are,You,Today"
",")
"."))</syntaxhighlight>
{{out}}
<pre>Hello.How.Are.You.Today</pre>
=={{header|Smalltalk}}==
<
array := 'Hello,How,Are,You,Today' subStrings: $,.
array fold: [:concatenation :string | concatenation, '.', string ]</
Some implementations also have a ''join:'' convenience method that allows the following shorter solution:
<
The solution displaying a trailing period would be:
<
array := 'Hello,How,Are,You,Today' subStrings: $,.
array inject: '' into: [:concatenation :string | concatenation, string, '.' ]</
=={{header|SNOBOL4}}==
Line 3,416 ⟶ 3,695:
For this task, it's convenient to define Perl-style split( ) and join( ) functions.
<
split t = table()
sp1 str pos(0) (break(chs) | rem) $ t<i = i + 1>
Line 3,432 ⟶ 3,711:
* # Test and display
output = join('.',split(',','Hello,How,Are,You,Today'))
end</
{{out}}
Line 3,440 ⟶ 3,719:
=={{header|Standard ML}}==
<
val main = (String.concatWith ".") o splitter;</
Test:
<
val it = "Hello.How.Are.You.Today" : string</
=={{header|Swift}}==
{{works with|Swift|3.x}}
<
let tokens = text.components(separatedBy: ",") // for single or multi-character separator
print(tokens)
let result = tokens.joined(separator: ".")
print(result)</
{{works with|Swift|2.x}}
<
let tokens = text.characters.split(",").map{String($0)} // for single-character separator
print(tokens)
let result = tokens.joinWithSeparator(".")
print(result)</
{{works with|Swift|1.x}}
<
let tokens = split(text, { $0 == "," }) // for single-character separator
println(tokens)
let result = ".".join(tokens)
println(result)</
For multi-character separators:<
let text = "Hello,How,Are,You,Today"
let tokens = text.componentsSeparatedByString(",")
print(tokens)</
=={{header|Tcl}}==
Generating a list form a string by splitting on a comma:
<
Joining the elements of a list by a period:
<
Thus the whole thing would look like this:
<
If you'd like to retain the list in a variable with the name "words", it would only be marginally more complex:
<
(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,495 ⟶ 3,774:
<code>tr</code> knows nothing about arrays, so this solution only changes each comma to a period.
<
=={{header|Transd}}==
<syntaxhighlight lang="Scheme">#lang transd
MainModule: {
_start: (lambda locals: s "Hello,How,Are,You,Today"
(textout (join (split s ",") "."))
)
}</syntaxhighlight>
{{out}}
<pre>
Hello.How.Are.You.Today
</pre>
=={{header|TUSCRIPT}}==
<
$$ MODE TUSCRIPT
SET string="Hello,How,Are,You,Today"
SET string=SPLIT (string,":,:")
SET string=JOIN (string,".")
</syntaxhighlight>
=={{header|TXR}}==
Line 3,510 ⟶ 3,802:
sequences of non-commas.
<
@(coll)@{token /[^,]+/}@(end)
@(output)
@(rep)@token.@(last)@token@(end)
@(end)</
Different approach. Collect tokens, each of
Line 3,520 ⟶ 3,812:
before a comma, or else extends to the end of the line.
<
@(coll)@(maybe)@token,@(or)@token@(end)@(end)
@(output)
@(rep)@token.@(last)@token@(end)
@(end)</
Using TXR Lisp:
<
Hello.How.Are.You.Today</
=={{header|UNIX Shell}}==
{{works with|Bourne Shell}}
<
(IFS=,
printf '%s.' $string
echo)</
----
{{works with|Bourne Again SHell}}
{{works with|Public Domain Korn SHell|5.2.14}}
<
stripchar-l ()
#removes the specified character from the left side of the string
Line 3,599 ⟶ 3,891:
join "$( split "$list" "$input_delimiter" )" \
"$contains_a_space" "$output_delimiter";
}</
''Example''
<
Hello.How.Are.You.Today </
----
Line 3,612 ⟶ 3,904:
{{works with|ksh93}}
{{works with|zsh}}
<syntaxhighlight lang="sh">
string1="Hello,How,Are,You,Today"
elements_quantity=$(echo $string1|tr "," "\n"|wc -l)
Line 3,625 ⟶ 3,917:
# or to cheat
echo "Hello,How,Are,You,Today"|tr "," "."</
=={{header|UnixPipes}}==
{{works with|Bourne Shell}}
<
(IFS=, read -r A B; echo "$A".; test -n "$B" && (echo "$B" | token))
}
echo "Hello,How,Are,You" | token</
=={{header|Ursa}}==
<
set text "Hello,How,Are,You,Today"
decl string<> tokens
Line 3,643 ⟶ 3,935:
out tokens<i> "." console
end for
out endl console</
=={{header|Ursala}}==
Line 3,651 ⟶ 3,943:
second order function parameterized by the delimiter. Character
literals are preceded by a backquote.
<
token_list = sep`, 'Hello,How,Are,You,Today'
Line 3,657 ⟶ 3,949:
#cast %s
main = mat`. token_list</
{{out}}
<pre>
Line 3,664 ⟶ 3,956:
=={{header|Vala}}==
<
string s = "Hello,How,Are,You,Today";
print(@"$(string.joinv(".", s.split(",")))");
}</
{{out}}
<pre>Hello.How.Are.You.Today</pre>
=={{header|VBA}}==
<
Dim temp() As String
temp = Tokenize("Hello,How,Are,You,Today", ",")
Line 3,684 ⟶ 3,976:
Private Sub Display(arr() As String, sep As String)
Debug.Print Join(arr, sep)
End Sub</
{{Out}}
<pre>Hello How Are You Today</pre>
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
s = "Hello,How,Are,You,Today"
WScript.StdOut.Write Join(Split(s,","),".")
</syntaxhighlight>
{{Out}}
<pre>Hello.How.Are.You.Today</pre>
Line 3,702 ⟶ 3,994:
The contents of each text register is then displayed to user, separated by a period.
<
Ins_Text("Hello,How,Are,You,Today")
Line 3,721 ⟶ 4,013:
}
Buf_Quit(OK)</
=={{header|V (Vlang)}}==
<
// Tectonics: v run tokenize-a-string.v
module main
Line 3,731 ⟶ 4,023:
pub fn main() {
println("Hello,How,Are,You,Today".split(',').join('.'))
}</
{{out}}
<pre>prompt$ v run rosetta/tokenize-a-string.v
Line 3,738 ⟶ 4,030:
=={{header|WinBatch}}==
<
result = ''
BoxOpen('WinBatch Tokenizing Example', '')
Line 3,746 ⟶ 4,038:
next
display(10, 'End of Program', 'Dialog and program will close momentarily.')
BoxShut()</
{{out}}
Line 3,752 ⟶ 4,044:
=={{header|Wortel}}==
<
Returns
<pre>"Hello.How.Are.You.Today"</pre>
=={{header|Wren}}==
<
var t = s.split(",").join(".") + "."
System.print(t)</
{{out}}
Line 3,767 ⟶ 4,059:
=={{header|XPath 2.0}}==
<
{{out}}
Line 3,773 ⟶ 4,065:
=={{header|XPL0}}==
<
include c:\cxpl\codes;
int I, J, K, Char;
Line 3,792 ⟶ 4,084:
for K:= 4 downto 0 do [Text(0, addr Array(K,0)); ChOut(0, ^.)];
CrLf(0);
]</
The 'addr' operator is used to fetch the 32-bit address of Array rather
Line 3,803 ⟶ 4,095:
=={{header|Yabasic}}==
<
n = token("Hello. How are you today?", s$(), ".? ")
Line 3,811 ⟶ 4,103:
if i < n print ".";
next
print</
=={{header|Zig}}==
<syntaxhighlight lang="zig">const std = @import("std");
pub fn main() void {
const string = "Hello,How,Are,You,Today";
var tokens = std.mem.split(u8, string, ",");
std.debug.print("{s}", .{tokens.next().?});
while (tokens.next()) |token| {
std.debug.print(".{s}", .{token});
}
}</syntaxhighlight>
=={{header|zkl}}==
<
Hello.How.Are.You.Today</
=={{header|Zoea}}==
<syntaxhighlight lang="zoea">
program: tokenize_a_string
input: "Hello,How,Are,You,Today"
output: "Hello.How.Are.You.Today"
</syntaxhighlight>
=={{header|Zoea Visual}}==
Line 3,828 ⟶ 4,130:
=={{header|Zsh}}==
<
tokens=(${(s:,:)str})
print ${(j:.:)tokens}</
Or, using SH_SPLIT_WORD:
<
IFS=, echo ${(j:.:)${=str}}</
{{omit from|PARI/GP|No real capacity for string manipulation}}
|