Tokenize a string: Difference between revisions
m
Using lang tags now.
m (→{{header|Objective-C}}: works with) |
m (Using lang tags now.) |
||
Line 34:
</lang>
=={{header|ALGOL 68}}==
)</lang>
Output:<pre>
John Lennon.Paul McCartney.George Harrison.Ringo Starr.
Line 122:
=={{header|C sharp|C#}}==
}</lang>
=={{header|C++}}==
Line 141:
Note doxygen tags in comments before function, describing details of interface.
}</lang>
here is sample usage code:
}</lang>
=={{header|D}}==
=={{header|E}}==
=={{header|Erlang}}==
=={{header|Forth}}==
There is no standard string split routine, but it is easily written. The results are saved temporarily to the dictionary.
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
DO
pos2 = INDEX(str(pos1:), ",")
IF (pos2 == 0) THEN
n = n + 1
word(n) = str(pos1:)
EXIT
END IF
n = n + 1
word(n) = str(pos1:pos1+pos2-2)
pos1 = pos2+pos1
END DO
DO i = 1, n
WRITE(*,"(2A)", ADVANCE="NO") TRIM(word(i)), "."
END DO
END PROGRAM Example</lang>
=={{header|Haskell}}==
The necessary operations are unfortunately not in the standard library (yet), but simple to write:
splitRegex :: Regex -> String -> [String]
joinWith :: [a] -> <nowiki>[[a]]</nowiki> -> [a]
joinWith d xs = concat $ List.intersperse d xs
-- "concat $ intersperse" can be replaced with "intercalate" from the Data.List in GHC 6.8 and later
putStrLn $ joinWith "." $ splitBy (== ',') $ "Hello,How,Are,You,Today"
-- using regular expression to split:
import Text.Regex
putStrLn $ joinWith "." $ splitRegex (mkRegex ',') $ "Hello,How,Are,You,Today"</lang>
=={{header|Groovy}}==
Line 281:
=={{header|Io}}==
=={{header|J}}==
<lang j> s=: 'Hello,How,Are,You,Today'
=={{header|Java}}==
Line 300:
There are multiple ways to tokenize a String in Java. The first is by splitting the String into an array of Strings, and the other way is to use StringTokenizer with a delimiter. The second way given here 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.
}</lang>
=={{header|JavaScript}}==
{{works with|Firefox|2.0}}
=={{header|Logo}}==
{{works with|UCB Logo}}
[Hello How Are You Today]</lang>
=={{header|MAXScript}}==
=={{header|Objective-C}}==
Line 403:
As a one liner without a trailing period, and most efficient way of doing it as you don't have to define an array.
If you needed to keep an array for later use, again no trailing period
If you really want a trailing period, here is an example
=={{header|PHP}}==
{{works with|PHP|5.x}}
?></lang>
=={{header|Pop11}}==
Line 430:
First show the use of sysparse_string to break up a string and make a list of strings.
By giving it an extra parameter 'true' we can make it recognize numbers and produce a list of strings and numbers
Now show some uses of the built in procedure sys_parse_string, which allows more options:
If {% ... %} were used instead of [% ... %] the result would be
a vector (i.e. array) of strings rather than a list of strings.
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:
Using lower level pop-11 facilities to tokenise the string:
Since the task requires to use array we convert list to array
We could use list directly for printing:
so the conversion to vector is purely to satisfy task formulation.
Line 530:
{{works with|Python|2.5}}
If you want to print each word on its own line:
or
or the one liner
=={{header|Raven}}==
=={{header|Ruby}}==
<lang ruby> string = "Hello,How,Are,You,Today".split(',')
string.each do |w|
print "#{w}."
end
puts "Hello,How,Are,You,Today".split(',').join('.')</lang>
=={{header|Seed7}}==
=={{header|Smalltalk}}==
Some implementations also have a ''join:'' convenience method that allows the following shorter solution:
The solution displaying a trailing period would be:
=={{header|Standard ML}}==
Test:
=={{header|Tcl}}==
Generating a list form a string by splitting on a comma:
<lang
Joining the elements of a list by a period:
<lang
Thus the whole thing would look like this:
<lang
If you'd like to retain the list in a variable with the name "words", it would only be marginally more complex:
<lang
=={{header|UnixPipes}}==
|