Split a character string based on change of character: Difference between revisions
Content added Content deleted
(Added Algol 68) |
(Added Lua version) |
||
Line 257: | Line 257: | ||
{{Out}} |
{{Out}} |
||
<pre>g, HHH, 5, YY, ++, ///, \</pre> |
|||
=={{header|Lua}}== |
|||
Note that the backslash must be quoted as a double backslash as Lua uses C-like escape sequences. |
|||
<lang Lua>function charSplit (inStr) |
|||
local outStr, nextChar = inStr:sub(1, 1) |
|||
for pos = 2, #inStr do |
|||
nextChar = inStr:sub(pos, pos) |
|||
if nextChar ~= outStr:sub(#outStr, #outStr) then |
|||
outStr = outStr .. ", " |
|||
end |
|||
outStr = outStr .. nextChar |
|||
end |
|||
return outStr |
|||
end |
|||
print(charSplit("gHHH5YY++///\\"))</lang> |
|||
{{out}} |
|||
<pre>g, HHH, 5, YY, ++, ///, \</pre> |
<pre>g, HHH, 5, YY, ++, ///, \</pre> |
||