Tokenize a string: Difference between revisions

Content deleted Content added
m →‎[[Java]]: Use Java header instead
m Switched to header template
Line 2: Line 2:
Separate the string "Hello,How,Are,You,Today" by commas into an array so that each index of the array 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.
Separate the string "Hello,How,Are,You,Today" by commas into an array so that each index of the array 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.


==[[Ada]]==
=={{header|Ada}}==
[[Category:Ada]]

with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Text_Io; use Ada.Text_Io;
Line 24: Line 22:
end Parse_Commas;
end Parse_Commas;


==[[C]]==
=={{header|C}}==


'''Standard:''' ANSI C
'''Standard:''' ANSI C
Line 59: Line 57:
</pre>
</pre>


==[[C#]]==
=={{header|C sharp|C#}}==
[[Category:C#]]
string str = "Hello,How,Are,You,Today";
string str = "Hello,How,Are,You,Today";


Line 133: Line 130:
}
}


==[[E]]==
=={{header|E}}==
[[Category:E]]

".".rjoin("Hello,How,Are,You,Today".split(","))
".".rjoin("Hello,How,Are,You,Today".split(","))


==[[Forth]]==
=={{header|Forth}}==
[[Category: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.


Line 162: Line 155:
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


==[[Groovy]]==
=={{header|Groovy}}==
[[Category:Groovy]]

println 'Hello,How,Are,You,Today'.split(',').join('.')
println 'Hello,How,Are,You,Today'.split(',').join('.')


Line 187: Line 178:
}
}


==[[JavaScript]]==
=={{header|JavaScript}}==
[[Category:JavaScript]]
'''Interpreter:''' Firefox 2.0
'''Interpreter:''' Firefox 2.0


alert( "Hello,How,Are,You,Today".split(",").join(".") );
alert( "Hello,How,Are,You,Today".split(",").join(".") );


==[[MAXScript]]==
=={{header|MAXScript}}==
[[Category:MAXScript]]
output = ""
output = ""
for word in (filterString "Hello,How,Are,You,Today" ",") do
for word in (filterString "Hello,How,Are,You,Today" ",") do
Line 202: Line 191:
format "%\n" output
format "%\n" output


==[[Perl]]==
=={{header|Perl}}==
[[Category:Perl]]
'''Interpreter:''' [[Perl]] any 5.X
'''Interpreter:''' [[Perl]] any 5.X


Line 220: Line 208:
print $_.'.' for (@words);
print $_.'.' for (@words);


==[[PHP]]==
=={{header|PHP}}==
[[Category:PHP]]
'''Interpreter:''' [[PHP]] any 5.x
'''Interpreter:''' [[PHP]] any 5.x


Line 230: Line 217:
?>
?>


==[[Pop11]]==
=={{header|Pop11}}==
[[Category:Pop11]]

Natural solution in Pop11 uses lists:
Natural solution in Pop11 uses lists:


Line 274: Line 259:
so the convertion to vector is purely to satisfy task formulation.
so the convertion to vector is purely to satisfy task formulation.


==[[Python]]==
=={{header|Python}}==
[[Category:Python]]
'''Interpreter:''' Python 2.5
'''Interpreter:''' Python 2.5


Line 291: Line 275:
print "\n".join(tokens)
print "\n".join(tokens)


==[[Raven]]==
=={{header|Raven}}==
[[Category:Raven]]

'Hello,How,Are,You,Today' ',' split '.' join print
'Hello,How,Are,You,Today' ',' split '.' join print


==[[Ruby]]==
=={{header|Ruby}}==
[[Category:Ruby]]

string = "Hello,How,Are,You,Today".split(',')
string = "Hello,How,Are,You,Today".split(',')
string.each do |w|
string.each do |w|
Line 306: Line 286:
puts "Hello,How,Are,You,Today".split(',').join('.')
puts "Hello,How,Are,You,Today".split(',').join('.')


==[[Seed7]]==
=={{header|Seed7}}==
[[Category:Seed7]]

var array string: tokens is 0 times "";
var array string: tokens is 0 times "";


tokens := split("Hello,How,Are,You,Today", ",");
tokens := split("Hello,How,Are,You,Today", ",");


==[[Smalltalk]]==
=={{header|Smalltalk}}==
[[Category:Smalltalk]]

|array |
|array |
array := ('Hello,How,Are,You,Today' findTokens: $,) asArray.
array := ('Hello,How,Are,You,Today' findTokens: $,) asArray.
Line 323: Line 299:
[:concatenation :string | concatenation , string , '.'])
[:concatenation :string | concatenation , string , '.'])


==[[Standard ML]]==
=={{header|Standard ML}}==
[[Category:Standard ML]]

val splitter = String.tokens (fn c => c = #",");
val splitter = String.tokens (fn c => c = #",");
val main = (String.concatWith ".") o splitter;
val main = (String.concatWith ".") o splitter;
Line 334: Line 308:
<i>val it = "Hello.How.Are.You.Today" : string</i>
<i>val it = "Hello.How.Are.You.Today" : string</i>


==[[Tcl]]==
=={{header|Tcl}}==
[[Category:Tcl]]

Generating a list form a string by splitting on a comma:
Generating a list form a string by splitting on a comma: