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