Split a character string based on change of character: Difference between revisions
Split a character string based on change of character (view source)
Revision as of 07:33, 2 July 2024
, 6 days agoAdd Refal
SqrtNegInf (talk | contribs) (→{{header|Perl}}: Unicode) |
Not a robot (talk | contribs) (Add Refal) |
||
(93 intermediate revisions by 50 users not shown) | |||
Line 1:
[[Category: String manipulation]]
[[Category:Strings]]
[[Category:Simple]]
{{task}}
Line 22 ⟶ 23:
should be split and show:
<big><big> g, HHH, 5, YY, ++, ///, \ </big></big>
{{Template:Strings}}
<br><br>
=={{header|11l}}==
{{trans|C++}}
<syntaxhighlight lang="11l">F split(input, delim)
V res = ‘’
L(ch) input
I !res.empty & ch != res.last
res ‘’= delim
res ‘’= ch
R res
print(split(‘gHHH5YY++///\’, ‘, ’))</syntaxhighlight>
{{out}}
<pre>
g, HHH, 5, YY, ++, ///, \
</pre>
=={{header|8080 Assembly}}==
<syntaxhighlight lang="8080asm"> org 100h
jmp demo
;;; Split the string under DE on changing characters,
;;; and store the result at HL.
split: ldax d ; Load character from string
spcopy: mov m,a ; Store in output
cpi '$' ; CP/M string terminator
rz ; Stop when the end is reached
mov b,a ; Store previous character in B
inx d ; Increment input pointer
inx h ; Increment output pointer
ldax d ; Get next character
cmp b ; Same as previous character?
jz spcopy ; Then just copy it
cpi '$' ; Otherwise, if it is the en
jz spcopy ; Then just copy it as well
mvi m,',' ; Otherwise, add a comma and a space
inx h
mvi m,' '
inx h
jmp spcopy
;;; Demo code
demo: lxi d,string
lxi h,out
call split ; Split the string
lxi d,out
mvi c,9 ; And print it using CP/M
jmp 5
string: db 'gHHH5YY++///',5Ch,'$'
out: equ $</syntaxhighlight>
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|8086 Assembly}}==
<syntaxhighlight lang="asm"> cpu 8086
org 100h
section .text
jmp demo
;;; Split the string at DS:SI on changing characters,
;;; and store the result at ES:DI.
split: lodsb ; Load character
.copy: stosb ; Store in output
cmp al,'$' ; Done yet?
je .out ; If so, stop.
mov ah,al ; Store previous character
lodsb ; Get next character
cmp al,ah ; Same character?
je .copy ; Then just copy it
cmp al,'$' ; End of string?
je .copy ; Then just copy it too
mov dl,al
mov ax,', ' ; Otherwise, add a comma and a space
stosw
mov al,dl
jmp .copy
.out: ret
;;; Demo code
demo: mov si,string
mov di,buf
call split ; Split the string
mov dx,buf
mov ah,9
int 21h ; And print the result using DOS
ret
section .data
string: db 'gHHH5YY++///\$'
section .bss
buf: resb 32</syntaxhighlight>
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program splitcar64.s */
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
/*********************************/
/* Initialized data */
/*********************************/
.data
szCarriageReturn: .asciz "\n"
szString1: .asciz "gHHH5YY++///\\"
/* IMPORTANT REMARK for compiler as
The way to get special characters into a string is to escape these characters: precede them
with a backslash ‘\’ character. For example ‘\\’ represents one backslash: the first \ is
an escape which tells as to interpret the second character literally as a backslash (which
prevents as from recognizing the second \ as an escape character).
*/
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sBuffer: .skip 100
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
ldr x0,qAdrszString1 // input string address
ldr x1,qAdrsBuffer // output buffer address
bl split
ldr x0,qAdrsBuffer
bl affichageMess // display message
ldr x0,qAdrszCarriageReturn
bl affichageMess
100: // standard end of the program
mov x0,0 // return code
mov x8,EXIT // request to exit program
svc 0 // perform the system call
qAdrszString1: .quad szString1
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrsBuffer: .quad sBuffer
/******************************************************************/
/* generate value */
/******************************************************************/
/* x0 contains the address of input string */
/* x1 contains the address of output buffer */
split:
stp x1,lr,[sp,-16]! // save registers
mov x4,0 // indice loop input string
mov x5,0 // indice buffer
ldrb w2,[x0,x4] // read first char in reg x2
cbz x2,4f // if null -> end
strb w2,[x1,x5] // store char in buffer
add x5,x5,1 // increment location buffer
1:
ldrb w3,[x0,x4] //read char[x4] in reg x3
cbz x3,4f // if null end
cmp x2,x3 // compare two characters
bne 2f
strb w3,[x1,x5] // = -> store char in buffer
b 3f // loop
2:
mov x2,',' // else store comma in buffer
strb w2,[x1,x5] // store char in buffer
add x5,x5,1
mov x2,' ' // and store space in buffer
strb w2,[x1,x5]
add x5,x5,1
strb w3,[x1,x5] // and store input char in buffer
mov x2,x3 // and maj x2 with new char
3:
add x5,x5,1 // increment indices
add x4,x4,1
b 1b // and loop
4:
strb w3,[x1,x5] // store zero final in buffer
100:
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
{{Output}}<pre> gg, HHH, 5, YY, ++, ///, \ </pre>
=={{header|Action!}}==
<syntaxhighlight lang="action!">PROC Split(CHAR ARRAY s)
BYTE i
CHAR curr,last
i=1 last=s(1)
Put('")
WHILE i<=s(0)
DO
curr=s(i)
IF curr#last THEN
Print(", ")
FI
Put(curr)
last=curr
i==+1
OD
Put('")
RETURN
PROC Test(CHAR ARRAY s)
PrintF("Input: ""%S""%E",s)
Print("Split: ") Split(s)
PutE() PutE()
RETURN
PROC Main()
Test("gHHH5YY++///\")
Test("gHHH 5++,,,///\")
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Split_a_character_string_based_on_change_of_character.png Screenshot from Atari 8-bit computer]
<pre>
Input: "gHHH5YY++///\"
Split: "g, HHH, 5, YY, ++, ///, \"
Input: "gHHH 5++,,,///\"
Split: "g, HHH, , 5, ++, ,,,, ///, \"
</pre>
=={{header|Ada}}==
<
with Ada.Text_IO;
procedure Split is
Line 42 ⟶ 280:
Print_Tokens ("gHHH5YY+++");
end split;
</syntaxhighlight>
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">BEGIN
# returns s with ", " added between each change of character #
PROC split on characters = ( STRING s )STRING:
Line 57 ⟶ 294:
[ 3 * ( ( UPB s - LWB s ) + 1 ) ]CHAR result;
INT r pos := LWB result;
CHAR s char := s[ LWB s ];
FOR s pos FROM LWB s TO UPB s DO
Line 75 ⟶ 311:
print( ( split on characters( "gHHH5YY++///\" ), newline ) )
END</
{{out}}
<pre>
g, HHH, 5, YY, ++, ///, \
</pre>
=={{header|Amazing Hopper}}==
VERSION 1: string
<syntaxhighlight lang="c">
#include <basico.h>
#define INICIO 1
#define CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\"+-/ \\:,;:_*"
algoritmo
objetivo = "gHHH5YY\"\"++ ///,,,\\", indice=0
largo=0, sublargo=0, v=0
#( largo = len(indice:=(onechar(CHARS,objetivo))) )
t=0, nuevo=""
para cada caracter ( v, indice, largo )
#(t = replicate(v, sublargo := ((poschar(INICIO, v, objetivo) - 1 ) ) ))
#(nuevo = cat( cat(nuevo, t), ", "))
objetivo+=sublargo
siguiente
nuevo -= 2
imprimir( "NEW STRING=\n", nuevo,NL)
terminar
</syntaxhighlight>
{{out}}
<pre>
$ hopper3 basica/splitrep.bas
NEW STRING=
g, HHH, 5, YY, "", ++, , ///, ,,,, \
</pre>
VERSION 2: arrays
<syntaxhighlight lang="c">
#include <basico.h>
#define INICIO 1
#define CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\"+-/ \\:,;:_*"
algoritmo
objetivo = "gHHH5YY\"\"++ ///,,,,\\", indice=0
largo=0, sublargo=0, lista={}, v=0
#( largo = len(indice:=(onechar(CHARS,objetivo))) )
para cada caracter ( v, indice, largo )
#( replicate(v, sublargo := ((poschar(INICIO, v, objetivo) - 1 ))))
meter en( lista )
objetivo+=sublargo
siguiente
imprimir( "LISTA=\n", lista, NL )
terminar
</syntaxhighlight>
{{out}}
<pre>
$ hopper3 basica/splitrep2.bas
LISTA=
g,HHH,5,YY,"",++, ,///,,,,,,\
</pre>
=={{header|ANSI BASIC}}==
{{works with|Decimal BASIC}}
<syntaxhighlight lang="basic">REM >split
DECLARE EXTERNAL FUNCTION FN_split$
Line 100 ⟶ 404:
NEXT i
LET FN_split$ = split$
END FUNCTION</
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">split ← 2↓∘∊(⊂', '),¨(⊢≠¯1⌽⊢)⊂⊢</syntaxhighlight>
{{out}}
<pre> split 'gHHH5YY++///\'
g, HHH, 5, YY, ++, ///, \</pre>
=={{header|AppleScript}}==
===Functional===
{{Trans|JavaScript}}
<
map(curry(intercalate)'s |λ|(""), ¬
group("gHHH5YY++///\\")))
Line 222 ⟶ 534:
{}
end if
end tail</
{{Out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
===Straightforward===
(Also case-sensitve.)
<syntaxhighlight lang="applescript">on splitAtCharacterChanges(input)
set len to (count input)
if (len < 2) then return input
set chrs to input's characters
set currentChr to beginning of chrs
considering case
repeat with i from 2 to len
set thisChr to item i of chrs
if (thisChr is not currentChr) then
set item i of chrs to ", " & thisChr
set currentChr to thisChr
end if
end repeat
end considering
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
set output to chrs as text
set AppleScript's text item delimiters to astid
return output
end splitAtCharacterChanges
-- Test code:
splitAtCharacterChanges("gHHH5YY++///\\")</syntaxhighlight>
{{output}}
<syntaxhighlight lang="applescript">"g, HHH, 5, YY, ++, ///, \\"</syntaxhighlight>
===ASObjC===
<syntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
on splitAtCharacterChanges(input)
tell (current application's class "NSMutableString"'s stringWithString:(input)) to ¬
return (its stringByReplacingOccurrencesOfString:("(.)\\1*+(?!$)") withString:("$0, ") ¬
options:(current application's NSRegularExpressionSearch) range:({0, its |length|()})) as text
end splitAtCharacterChanges
-- Test code:
splitAtCharacterChanges("gHHH5YY++///\\")</syntaxhighlight>
{{output}}
<syntaxhighlight lang="applescript">"g, HHH, 5, YY, ++, ///, \\"</syntaxhighlight>
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
/* program splitcar.s */
Line 347 ⟶ 707:
output : gg, HHH, 5, YY, ++, ///, \
</syntaxhighlight>
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">parts: [] current: ""
loop split {gHHH5YY++///\} 'ch [
if? or? empty? current
contains? current ch -> 'current ++ ch
else [
'parts ++ current
current: new ch
]
]
'parts ++ current
print parts</syntaxhighlight>
{{out}}
<pre>g HHH 5 YY ++ /// \</pre>
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">Split_Change(str){
for i, v in StrSplit(str)
res .= (v=prev) ? v : (res?", " :"") v , prev := v
return res
}</syntaxhighlight>
Examples:<syntaxhighlight lang="autohotkey">str := "gHHH5YY++///\"
MsgBox % Split_Change(str)</syntaxhighlight>
Outputs:<pre>g, HHH, 5, YY, ++, ///, \</pre>
===RegEx Version===
<syntaxhighlight lang="autohotkey">Split_Change(str){
return RegExReplace(str, "(.)\1*(?!$)", "$0, ")
}</syntaxhighlight>
Examples:<syntaxhighlight lang="autohotkey">str := "gHHH5YY++///\"
MsgBox % Split_Change(str)</syntaxhighlight>
Outputs:<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f SPLIT_A_CHARACTER_STRING_BASED_ON_CHANGE_OF_CHARACTER.AWK
BEGIN {
Line 371 ⟶ 763:
return(new_str)
}
</syntaxhighlight>
{{out}}
<pre>
Line 380 ⟶ 772:
=={{header|BaCon}}==
Literal strings in BaCon are passed to the C compiler as they are; a backslash therefore needs to be escaped.
<
c$ = LEFT$(txt$, 1)
Line 391 ⟶ 783:
END IF
PRINT d$;
NEXT</
{{out}}
<pre>
g, HHH, 5, YY, ++, ///, \
</pre>
=={{header|BASIC256}}==
<syntaxhighlight lang="freebasic">function split$(instring$)
if length(instring$) < 2 then return instring$
ret$ = left(instring$,1)
for i = 2 to length(instring$)
if mid(instring$,i,1) <> mid(instring$, i-1, 1) then ret$ += ", "
ret$ += mid(instring$, i, 1)
next i
return ret$
end function
print split$("gHHH5YY++///\")</syntaxhighlight>
=={{header|BBC BASIC}}==
<
PRINT FN_split( "gHHH5YY++///\" )
END
Line 414 ⟶ 819:
split$ += d$
NEXT
= split$</
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|BQN}}==
<syntaxhighlight lang="bqn">Split ← (+`⊏⊸»⊸≠)⊸⊔
Join ← {∾⟜𝕨⊸∾´𝕩}
", " Join⟜Split "gHHH5YY++///\"</syntaxhighlight>
{{out}}
<pre>"g, HHH, 5, YY, ++, ///, \"</pre>
=={{header|C}}==
<
#include <stdlib.h>
#include <string.h>
Line 442 ⟶ 855:
*(counter--)='\0';
return realloc(result,strlen(result));
}</
{{out}}
<pre>
Line 449 ⟶ 862:
=={{header|C sharp}}==
<
using System.Linq;
using System.Collections.Generic;
Line 480 ⟶ 893:
public static string Delimit<T>(this IEnumerable<T> source, string separator = "") => string.Join(separator ?? "", source);
}</
{{out}}
<pre>
Line 487 ⟶ 900:
=={{header|C++}}==
<
// Solution for http://rosettacode.org/wiki/Split_a_character_string_based_on_change_of_character
#include<string>
Line 504 ⟶ 917:
int main(){
std::cout << split("gHHH5 ))YY++,,,///\\", ", ") << std::endl;
}</
{{out}}
<pre>g, HHH, 5, , )), YY, ++, ,,,, ///, \</pre>
=={{header|Clojure}}==
<
(println (clojure.string/join ", " (map first (re-seq #"(.)\1*" s)))))
(print-cchanges "gHHH5YY++///\\")
</syntaxhighlight>
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|CLU}}==
<syntaxhighlight lang="clu">% Split a string based on a change of character
split_on_change = iter (s: string) yields (string)
part: string := ""
for c: char in string$chars(s) do
if ~string$empty(part)
cand part[string$size(part)] ~= c then
yield(part)
part := ""
end
part := part || string$c2s(c)
end
yield(part)
end split_on_change
start_up = proc ()
po: stream := stream$primary_output()
str: string := "gHHH5YYY++///\\" % \\ escapes, as in C
rslt: string := ""
first: bool := true
for part: string in split_on_change(str) do
if first then first := false
else rslt := rslt || ", "
end
rslt := rslt || part
end
stream$putl(po, rslt)
end start_up</syntaxhighlight>
{{out}}
<pre>g, HHH, 5, YYY, ++, ///, \</pre>
=={{header|COBOL}}==
<syntaxhighlight lang="cobol">
identification division.
program-id. split-ch.
Line 580 ⟶ 1,025:
end program split-ch.
</syntaxhighlight>
{{out}}
<pre>
Line 594 ⟶ 1,039:
=={{header|Common Lisp}}==
<
(loop :for prev := nil :then c
:for c :across string
Line 600 ⟶ 1,045:
(split "gHHH5YY++///\\")
</syntaxhighlight>
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
Line 606 ⟶ 1,051:
Doing more work that what's being ask, the following solution builds a list of strings then output it:
<
(flet ((make-buffer ()
(make-array 0 :element-type 'character :adjustable t :fill-pointer t)))
Line 620 ⟶ 1,065:
(format t "~{~A~^, ~}"(nreverse result)))))
(split "gHHH5YY++///\\")</
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
sub split(in: [uint8], buf: [uint8]): (out: [uint8]) is
out := buf;
loop
[buf] := [in];
if [in] == 0 then break; end if;
if [in] != [@next in] and [@next in] != 0 then
[buf+1] := ',';
[buf+2] := ' ';
buf := buf+2;
end if;
buf := buf+1;
in := in+1;
end loop;
end sub;
var buf: uint8[32];
print(split("gHHH5YY++//\\", &buf[0]));
print_nl();</syntaxhighlight>
{{out}}
<pre>g, HHH, 5, YY, ++, //, \</pre>
=={{header|D}}==
<
void main() {
Line 640 ⟶ 1,110:
}
writeln();
}</
{{output}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
<syntaxhighlight lang="Delphi">
function SplitStringCharChange(S: string): string;
{Split string whenever the previous char is different from the current one}
var I: integer;
var C: char;
begin
Result:='';
{Copy string to output}
for I:=1 to Length(S) do
begin
Result:=Result+S[I];
{Appended ", " if the next char is different}
if (I<Length(S)) and (S[I]<>S[I+1]) then Result:=Result+', ';
end;
end;
procedure ShowSplitString(Memo: TMemo);
const S1 = 'gHHH5YY++///\';
var S2: string;
begin
Memo.Lines.Add(S1);
S2:=SplitStringCharChange(S1);
Memo.Lines.Add(S2);
end;
</syntaxhighlight>
{{out}}
<pre>
gHHH5YY++///\
g, HHH, 5, YY, ++, ///, \
Elapsed Time: 1.767 ms.
</pre>
=={{header|Dyalect}}==
<
var c
var str = ""
var last = this.
for n in 0..last {
if c && this[n] != c {
Line 659 ⟶ 1,170:
str += c
}
str
}
print("gHHH5YY++///\\".
{{out}}
Line 671 ⟶ 1,182:
=={{header|EasyLang}}==
<syntaxhighlight lang="text">
a$
for c$
if
cp$ = c$
s$ &= c$
.
print
</syntaxhighlight>
{{out}}
<pre>
g, HHH, 5, YY, ++, ///, \
</pre>
=={{header|ed}}==
<syntaxhighlight lang="sed">
# by Artyom Bologov
H
# Repeating two times because matching is greedy
g/.*/s/((.)\2*)((.)\4*)/\1\
\3/g
g/.*/s/((.)\2*)((.)\4*)/\1\
\3/g
,p
Q
</syntaxhighlight>
{{out}}
<pre>$ cat split-on-change.ed | ed -lEGs split-on-change.input
Newline appended
g
HHH
5
YY
++
///
\
</pre>
=={{header|Elixir}}==
<
IO.puts " input string: #{str}"
String.graphemes(str)
Line 696 ⟶ 1,236:
end
split.("gHHH5YY++///\\")</
{{out}}
Line 705 ⟶ 1,245:
=={{header|F_Sharp|F#}}==
<
let splitRuns s = Regex("""(.)\1*""").Matches(s) |> Seq.cast<Match> |> Seq.map (fun m -> m.Value) |> Seq.toList
printfn "%A" (splitRuns """gHHH5YY++///\""")</
{{out}}
<pre>["g"; "HHH"; "5"; "YY"; "++"; "///"; "\"]</pre>
=={{header|Factor}}==
<
"gHHH5YY++///\\"
"aaabbccccdeeff" [ [ = ] monotonic-split ", " join print ] bi@</
{{out}}
<pre>
Line 723 ⟶ 1,263:
=={{header|Forth}}==
{{works with|Gforth|0.7.3}}
<
: C@A+ A @ C@ [ 1 CHARS ]L A +! ;
: SPLIT. ( c-addr u --) SWAP A ! A @ C@
Line 735 ⟶ 1,275:
s" gHHH5YY++///\" TEST
s" gHHH5 ))YY++,,,///\" TEST
BYE</
{{out}}
<pre>input: gHHH5YY++///\
Line 749 ⟶ 1,289:
If the problem were to be solved by writing a "main line" only, there would have to be a declaration of the text variable there but since a subroutine can receive a CHARACTER variable of any size (the actual size is passed as a secret parameter), this can be dodged.
For this example a DO-loop stepping along the text is convenient, but in a larger context it would probably be most useful to work along the text with fingers L1 and L2 marking the start and finish positions of each sequence. <
Can't display the inserted commas in a different colour so as not to look like any commas in TEXT.
CHARACTER*(*) TEXT !The text.
Line 769 ⟶ 1,309:
PROGRAM POKE
CALL SPLATTER("gHHH5YY++///\") !The example given.
END</
Unfortunately, the syntax highlighter has failed to notice the terminating quote character, presumably because the preceding backslash might be an "escape sequence" trigger, a facility ''not'' used in Fortran text ''literals'' except possibly as a later modernist option.
{{Out}}
<pre>
g, HHH, 5, YY, ++, ///, \
</pre>
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">function split( instring as string ) as string
if len(instring) < 2 then return instring
dim as string ret = left(instring,1)
for i as uinteger = 2 to len(instring)
if mid(instring,i,1)<>mid(instring, i - 1, 1) then ret + = ", "
ret += mid(instring, i, 1)
next i
return ret
end function</syntaxhighlight>
=={{header|Frink}}==
<syntaxhighlight lang="frink">s = "gHHH5YY++///\\"
println[join[", ", map[getFunction["first", 1], s =~ %r/((.)\2*)/g]]]</syntaxhighlight>
{{out}}
<pre>
g, HHH, 5, YY, ++, ///, \
</pre>
=={{header|FutureBasic}}==
FB can process either Pascal strings (slowly being deprecated), or Apple's Core Foundation CFStrings (and Objective-C NSStrings). Here's the old-school Pascal string function:
<syntaxhighlight lang="text">
local fn SplitString( inputStr as Str255 ) as Str255
Str255 resultStr
NSUInteger i
if len$( inputStr ) < 2 then resultStr = inputStr : exit fn
resultStr = left$( inputStr, 1 )
for i = 2 to len$( inputStr )
if mid$( inputStr, i, 1 ) <> mid$( inputStr, i - 1, 1 ) then resultStr = resultStr + ", "
resultStr = resultStr + mid$(inputStr, i, 1)
next
end fn = resultStr
window 1
print fn SplitString( "gHHH5YY++///\" )
HandleEvents
</syntaxhighlight>
And here's the recommended CFString counterpart:
<syntaxhighlight lang="text">
local fn SplitString( inputStr as CFStringRef ) as CFStringRef
NSUInteger i
unichar chr, lastChr = fn StringCharacterAtIndex( inputStr, 0 )
CFMutableStringRef resultStr = fn MutableStringWithCapacity(0)
for i = 0 to len( inputStr ) - 1
chr = fn StringCharacterAtIndex( inputStr, i )
if ( chr != lastChr ) then MutableStringAppendString( resultStr, @", " )
MutableStringAppendString( resultStr, mid( inputStr, i, 1 ) )
lastChr = chr
next
end fn = resultStr
window 1
print fn SplitString( @"gHHH5YY++///\\" )
HandleEvents
</syntaxhighlight>
'''Output for either function:'''
<pre>
g, HHH, 5, YY, ++, ///, \
Line 779 ⟶ 1,385:
=={{header|Go}}==
Treating "character" as a byte:
<
import (
Line 805 ⟶ 1,411:
}
return b.String()
}</
{{out}}
<pre>
g, HHH, 5, YY, ++, ///, \
</pre>
=={{header|Haskell}}==
<
main :: IO ()
main = putStrLn $ intercalate ", " (group "gHHH5YY++///\\")</
{{Out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
or as a hand-written fold:
<syntaxhighlight lang="haskell">import Data.List (intercalate)
import Data.Bool (bool)
charGroups :: String -> [String]
charGroups =
let go (a, b) (s, groups)
| a == b = (b : s, groups)
| otherwise =
( [a],
bool s [b] (null s) : groups
)
in uncurry (:) . foldr go ([], []) . (zip <*> tail)
main :: IO ()
main =
putStrLn $ intercalate ", " $ charGroups "gHHH5YY++///\\"</syntaxhighlight>
<pre>g, HHH, 5, YY, ++, ///, \</pre>
or in terms of '''span''':
<syntaxhighlight lang="haskell">import Data.List (intercalate)
charGroups :: String -> [String]
charGroups [] = []
charGroups (c : cs) =
let (xs, ys) = span (c ==) cs
in (c : xs) : charGroups ys
main :: IO ()
main =
putStrLn $ intercalate ", " $ charGroups "gHHH5YY++///\\"</syntaxhighlight>
{{Out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|IS-BASIC}}==
<
110 PRINT S$(1);
120 FOR I=2 TO LEN(S$)
Line 828 ⟶ 1,469:
140 PRINT S$(I);
150 NEXT
160 PRINT</
=={{header|J}}==
'''Solution:'''
<
delimitChars=: ', ' joinstring splitChars</
'''Example Usage:'''
<
g, HHH, 5, YY, ++, ///, \</
=={{header|Java}}==
You can use a regular expression to capture every character preceded by 0 or more of itself.
<syntaxhighlight lang="java">
import java.util.regex.Matcher;
import java.util.regex.Pattern;
</syntaxhighlight>
<syntaxhighlight lang="java">
String split(String string) {
Pattern pattern = Pattern.compile("(.)\\1*");
Matcher matcher = pattern.matcher(string);
StringBuilder strings = new StringBuilder();
int index = 0;
while (matcher.find()) {
if (index++ != 0)
strings.append(", ");
strings.append(matcher.group());
}
return strings.toString();
}
</syntaxhighlight>
<pre>
g, HHH, 5, YY, ++, ///, \
</pre>
<br />
An alternate demonstration
<syntaxhighlight lang="java">package org.rosettacode;
import java.util.ArrayList;
Line 904 ⟶ 1,568:
return output.toString();
}
}</
{{Out}}
Line 912 ⟶ 1,576:
===ES6===
{{Trans|Haskell}}
<
"use strict";
// ----------- SPLIT ON CHARACTER CHANGES ------------
const
.map(x => x.join(""))
// --------------------- GENERIC ---------------------
// group :: [a] -> [[a]]
const group = xs =>
// A list of lists, each containing only
// elements equal under (===), such that the
// concatenation of these lists is xs.
groupBy(a => b => a === b)(xs);
// groupBy :: (a -> a -> Bool) [a] -> [[a]]
const groupBy = eqOp =>
// A list of lists, each containing only elements
// equal under the given equality operator,
// such that the concatenation of these lists is xs.
xs => 0 < xs.length ? (() => {
const [h, ...t] = xs;
const [groups, g] = t.reduce(
([gs, a], x) => eqOp(x)(a[0]) ? (
Tuple(gs)([...a, x])
) : Tuple([...gs, a])([x]),
Tuple([])([h])
);
return [...groups, g];
})() : [];
//
const
"0":
length:
for (const k in
return main();
})();</syntaxhighlight>
{{Out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
Or, in terms of a general `span` function:
<syntaxhighlight lang="javascript">(() => {
"use strict";
// -------- STRING SPLIT ON CHARACTER CHANGES --------
// charGroups :: String -> [String]
const charGroups = s =>
// The characters of s split at each point where
// consecutive characters differ.
0 < s.length ? (() => {
const
c = s[0],
[xs, ys] = span(x => c === x)([
...s.slice(1)
]);
return [
[c, ...xs], ...charGroups(ys)
]
.map(zs => [...zs].join(""));
})() : "";
// ---------------------- TEST -----------------------
const main = () =>
charGroups("gHHH5YY++///\\")
.join(", ");
// --------------------- GENERIC ---------------------
// span :: (a -> Bool) -> [a] -> ([a], [a])
const span = p =>
// Longest prefix of xs consisting of elements which
// all satisfy p, tupled with the remainder of xs.
xs => {
const i = xs.findIndex(x => !p(x));
return -1 !== i ? [
xs.slice(0, i),
// MAIN ---
return main();
})();</syntaxhighlight>
{{Out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|jq}}==
<
# output: a stream of runs
def runs:
Line 984 ⟶ 1,702:
end;
"gHHH5YY++///\\" | [runs] | join(", ")</
{{out}}
Using the -r ("raw output") command-line option of jq:
Line 993 ⟶ 1,711:
Starting with
<
;'Split a string based on change of character, in Jsish';
Line 1,014 ⟶ 1,732:
;splitOnChange('aaa');
;splitOnChange('aaaba');
;splitOnChange('gH HH5YY++//,/\\');</
Then
Line 1,023 ⟶ 1,741:
Giving
<
;'Split a string based on change of character, in Jsish';
Line 1,056 ⟶ 1,774:
splitOnChange('gH HH5YY++//,/\') ==> g, H, , HH, 5, YY, ++, //, ,, /, \
=!EXPECTEND!=
*/</
Which tests as:
Line 1,074 ⟶ 1,792:
=={{header|Julia}}==
<
using IterTools
str = "gHHH5YY++///\\"
sep = map(join, groupby(identity, str))
println("string: $str\nseparated: ", join(sep, ", "))</
{{out}}
<pre>string: gHHH5YY++///\
separated: g, HHH, 5, YY, ++, ///, \</pre>
=={{header|K}}==
<syntaxhighlight lang="k">split: {(&~=':x)_x}
","/ split "gHHH5YY++///\\"</syntaxhighlight>
{{out}}
<pre>"g,HHH,5,YY,++,///,\\"</pre>
=={{header|Kotlin}}==
<
fun splitOnChange(s: String): String {
Line 1,100 ⟶ 1,825:
val s = """gHHH5YY++///\"""
println(splitOnChange(s))
}</
{{out}}
<pre>
g, HHH, 5, YY, ++, ///, \
</pre>
=== Using fold() ===
<syntaxhighlight lang="kotlin">
fun splitOnChange(src: String): String =
src.fold("") { acc, c ->
if (acc.isEmpty() || acc.last() == c) "$acc$c" else "$acc, $c"
}
fun main() {
splitOnChange("""gHHH5YY++///\""").also { println(it)}
}
</syntaxhighlight>
{{out}}
<pre>
g, HHH, 5, YY, ++, ///, \
</pre>
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{def mysplit
{def mysplit.r
{lambda {:w :i}
{if {> :i {W.length :w}}
then
else {if {not {W.equal? {W.get :i :w} {W.get {+ :i 1} :w}}}
then ____ else} {W.get {+ :i 1} :w}{mysplit.r :w {+ :i 1}}}}}
{lambda {:w}
{S.replace ____ by in {mysplit.r #:w 0}}}}
-> mysplit
{mysplit gHHH5YY++///\}
-> g HHH 5 YY ++ /// \
</syntaxhighlight>
=={{header|Lua}}==
Note that the backslash must be quoted as a double backslash as Lua uses C-like escape sequences.
<
local outStr, nextChar = inStr:sub(1, 1)
for pos = 2, #inStr do
Line 1,121 ⟶ 1,879:
end
print(charSplit("gHHH5YY++///\\"))</
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
Line 1,127 ⟶ 1,885:
'''Alternative:'''
Simply scan difference in reverse order and insert delimiter in place, the loop counter i will not update with length of s.
<
for i=#s,2,-1 do
if s:sub(i,i)~=s:sub(i-1,i-1) then
Line 1,134 ⟶ 1,892:
end
return s
end</
=={{header|Ksh}}==
<syntaxhighlight lang="ksh">
#!/bin/ksh
# Split a character string based on change of character
# # Variables:
#
str='gHHH5YY++///\'
delim=', '
# # Functions:
#
# # Function _splitonchg(str, delim) - return str split by delim at char change
#
function _splitonchg {
typeset _str ; _str="$1"
typeset _delim ; _delim="$2"
typeset _i _splitstr ; integer _i
for ((_i=1; _i<${#_str}+1; _i++)); do
if [[ "${_str:$((_i-1)):1}" != "${_str:${_i}:1}" ]]; then
_splitstr+="${_str:$((_i-1)):1}${_delim}"
else
_splitstr+="${_str:$((_i-1)):1}"
fi
done
echo "${_splitstr%"${_delim}"*}"
}
######
# main #
######
print "Original: ${str}"
print " Split: $(_splitonchg "${str}" "${delim}")"
</syntaxhighlight>
{{out}}<pre>
Original: gHHH5YY++///\
Split: g, HHH, 5, YY, ++, ///, \</pre>
=={{header|M2000 Interpreter}}==
Stack New open a new stack object as current stack, and keep the old one. After the end of block execution old stack get back as current stack. Data statement push to bottom (we read from top, so using data we get a FIFO type). Letter$ pops a string or raise an error if no string found at the top of stack.
<syntaxhighlight lang="m2000 interpreter">
Module PrintParts(splitthis$) {
Def string m$, p$
Line 1,161 ⟶ 1,960:
}
PrintParts "gHHH5YY++///\"
</syntaxhighlight>
=={{header|Maple}}==
Added an additional backlash to escape the \ character at the end.
<
local start,i,len;
start := 1;
Line 1,178 ⟶ 1,976:
printf("%s", str[start..len]);
end proc;
splitChange("gHHH5YY++///\\");</
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
The backslash (\) must be escaped with another backslash when defining the string.
<
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|MiniScript}}==
<
output = []
lastLetter = s[0]
Line 1,200 ⟶ 1,995:
lastLetter = letter
end for
print output.join("")</
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|Modula-2}}==
<
FROM Terminal IMPORT Write,WriteString,WriteLn,ReadChar;
Line 1,229 ⟶ 2,024:
ReadChar
END CharacterChange.</
{{out}}
<pre>g
Line 1,240 ⟶ 2,035:
=={{header|Nim}}==
<syntaxhighlight lang="nim">proc splitOnDiff(str: string): string =
result = ""
if str.len < 1: return result
var prevChar
for idx in 0 ..
if str[idx] != prevChar:
result &= ", "
Line 1,256 ⟶ 2,048:
result &= str[idx]
assert splitOnDiff("""X""") == """X"""
Line 1,263 ⟶ 2,054:
assert splitOnDiff("""gHHH5YY++///\""") == """g, HHH, 5, YY, ++, ///, \"""
echo splitOnDiff("""gHHH5YY++///\""")</
{{output}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|ooRexx}}==
<
If str=='' Then str= 'gHHH5YY++///\' /*Not specified? Then use the default.*/
i=1
Line 1,279 ⟶ 2,072:
i=j
End
Say ol</
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|Pascal}}==
<syntaxhighlight lang="pascal">program SplitChars;
{$IFDEF FPC}
{$MODE DELPHI}{$COPERATORS ON}
{$ENDIF}
const
TestString = 'gHHH5YY++///\';
function SplitAtChars(const S: String):String;
var
i : integer;
lastChar:Char;
begin
result := '';
IF length(s) > 0 then
begin
LastChar := s[1];
result := LastChar;
For i := 2 to length(s) do
begin
if s[i] <> lastChar then
begin
lastChar := s[i];
result += ', ';
end;
result += LastChar;
end;
end;
end;
BEGIN
writeln(SplitAtChars(TestString));
end.</syntaxhighlight>
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|Perl}}==
<
use warnings;
use feature 'say';
Line 1,297 ⟶ 2,125:
$last = $1;
}
say "Orginal: $string\n Split: 「" . join('」, 「', @S) . "」\n";
}</
{{out}}
<pre>Orginal: gHHH5YY++///\
Line 1,306 ⟶ 2,134:
Split: 「fff」, 「」, 「n⃗n⃗n⃗」, 「»»»」, 「 」, 「ℵℵ」, 「☄☄」, 「☃」, 「☃̂」, 「☃」, 「🤔」, 「🇺🇸」, 「🤦♂️」, 「👨👩👧👦」</pre>
=={{header|
<!--<syntaxhighlight lang="phix">-->
<span style="color: #008080;">function</span> <span style="color: #000000;">split_on_change</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">ch</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">prev</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">", "</span>
<span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ch</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">ch</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">split_on_change</span><span style="color: #0000FF;">(</span><span style="color: #008000;">`gHHH5YY++///\`</span><span style="color: #0000FF;">))</span>
<!--</syntaxhighlight>-->
{{Out}}
<pre>
g, HHH, 5, YY, ++, ///, \
</pre>
=={{header|Phixmonti}}==
<syntaxhighlight lang="Phixmonti">/# Rosetta Code problem: https://rosettacode.org/wiki/Split_a_character_string_based_on_change_of_character
by Galileo, 11/2022 #/
include ..\Utilitys.pmt
""
"gHHH5YY++///\" 1 get >ps
len for get
dup tps == if
rot swap chain swap
else
ps> drop >ps
swap ", " tps chain chain swap
endif
endfor
pstack</syntaxhighlight>
{{out}}
<pre>
=== Press any key to exit ===</pre>
=={{header|
<syntaxhighlight lang="picolisp">(de splitme (Str)
(let (Str (chop Str) Fin)
(if
(prinl (splitme "gHHH5YY++///\\"))</syntaxhighlight>
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|Pike}}==
<syntaxhighlight lang="pike">
string input = "gHHH5YY++///\\"; // \ needs escaping
string last_char;
foreach(input/1, string char) {
if(last_char && char != last_char)
write(", ");
write(char);
last_char = char;
}
</syntaxhighlight>
{{Out}}
<pre>
g, HHH, 5, YY, ++, ///, \
</pre>
=={{header|Plain English}}==
To make sense of this example, you must understand riders. A rider is a simple abstraction for efficiently parsing strings. A rider is a record with an original substring, a source substring, and a token substring.
After executing the following code, for example:
<syntaxhighlight lang="plainenglish">Put "abcdef" into a string.
Slap a rider on the string.</syntaxhighlight>
The rider looks like this:
<syntaxhighlight lang="plainenglish">Original: "abcdef"
Source: "abcdef"
Token: ""</syntaxhighlight>
Now when we <code>Bump the rider.</code>, it looks like this:
<syntaxhighlight lang="plainenglish">Original: "abcdef"
Source: "bcdef"
Token: "a"</syntaxhighlight>
Another bump, and:
<syntaxhighlight lang="plainenglish">Original: "abcdef"
Source: "cdef"
Token: "ab"</syntaxhighlight>
Now let's say we have a complete token and want to start a new one. We can
<code>Position the rider's token on the rider's source.</code>
and now the rider looks like this:
<syntaxhighlight lang="plainenglish">Original: "abcdef"
Source: "cdef"
Token: ""</syntaxhighlight>
And that's all there is to it.
<syntaxhighlight lang="plainenglish">To run:
Start up.
Split "gHHH5YY++///\" into some string things by change of character.
Write the string things on the console.
Destroy the string things.
Wait for the escape key.
Shut down.
To split a string into some string things by change of character:
If the string's length is less than 2, add the string to the string things; exit.
Slap a rider on the string.
Loop.
Move the rider (change of character rules).
Add the rider's token to the string things.
If the rider's source is blank, exit.
Repeat.
To move a rider (change of character rules):
Position the rider's token on the rider's source.
Loop.
If the rider's source is blank, exit.
If the rider's token is blank, bump the rider; repeat.
Put the rider's token's last plus 1 into a byte pointer.
If the rider's token's last's target is not the byte pointer's target, exit.
Bump the rider.
Repeat.
To write some string things to a console;
To write some string things on a console:
Get a string thing from the string things.
Loop.
If the string thing is nil, write "" on the console; exit.
Write the string thing's string on the console without advancing.
If the string thing's next is not nil, write ", " on the console without advancing.
Put the string thing's next into the string thing.
Repeat.</syntaxhighlight>
{{out}}
<pre>
g, HHH, 5, YY, ++, ///, \
Line 1,367 ⟶ 2,290:
=={{header|PowerShell}}==
{{trans|BBC BASIC}}
<syntaxhighlight lang="powershell">
function Split-String ([string]$String)
{
Line 1,388 ⟶ 2,311:
$splitString
}
</syntaxhighlight>
<syntaxhighlight lang="powershell">
Split-String "gHHH5YY++///\"
</syntaxhighlight>
{{Out}}
<pre>
Line 1,398 ⟶ 2,321:
=={{header|PureBasic}}==
<
Define *p.Character = @s$,
c_buf.c = *p\c
Line 1,416 ⟶ 2,339:
splitstring("gHHH5YY++///\")
Input()
EndIf</
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
Line 1,424 ⟶ 2,347:
===Python3.6+===
Using [[https://docs.python.org/3.6/library/itertools.html#itertools.groupby itertools.groupby]].
<
def splitter(text):
Line 1,431 ⟶ 2,354:
if __name__ == '__main__':
txt = 'gHHH5YY++///\\' # Note backslash is the Python escape char.
print(f'Input: {txt}\nSplit: {splitter(txt)}')</
{{out}}
Line 1,438 ⟶ 2,361:
===Python: Using zip===
<
return (''.join(x + ('' if x == nxt else ', ')
for x, nxt in zip(txt, txt[1:] + txt[-1])))
Line 1,444 ⟶ 2,367:
if __name__ == '__main__':
txt = 'gHHH5YY++///\\'
print(splitterz(txt))</
{{out}}
Line 1,450 ⟶ 2,373:
===Python2===
<
try: input = raw_input
Line 1,460 ⟶ 2,383:
groups.append(''.join(g))
print(' input string: %s' % s)
print(' output string: %s' % ', '.join(groups))</
{{out}} when using the default input:
<pre>
input string: gHHH5YY++///\
output string: g, HHH, 5, YY, ++, ///, \
</pre>
=={{header|Quackery}}==
<syntaxhighlight lang="quackery">[ dup size 2 <
iff size done
behead swap
[] nested join
witheach
[ over != if
[ drop i^ 1+
conclude ] ] ] is $run ( $ --> n )
[ dup size 2 < if done
dup $run split
dup [] =
iff drop done
dip [ $ ", " join ]
recurse join ] is runs$ ( $ --> $ )
</syntaxhighlight>
'''Testing in Quackery shell.'''
<pre>/O> $ "gHHH5YY++///\" runs$ echo$
...
g, HHH, 5, YY, ++, ///, \
Stack empty.
</pre>
=={{header|Racket}}==
{{trans|Python}}
<
(define (split-strings-on-change s)
(map list->string (group-by values (string->list s) char=?)))
Line 1,477 ⟶ 2,424:
<
)
", "))</
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2017.05}}
<syntaxhighlight lang="raku" line>sub group-chars ($str) { $str.comb: / (.) $0* / }
# Testing:
for Q[gHHH5YY++///\], Q[fffn⃗n⃗n⃗»»» ℵℵ☄☄☃☃̂☃🤔🇺🇸🤦♂️👨👩👧👦] -> $string {
put 'Original: ', $string;
put ' Split: ', group-chars($string).join(', ');
}</syntaxhighlight>
{{out}}
<pre>
Original: gHHH5YY++///\
Split: g, HHH, 5, YY, ++, ///, \
Original: fffn⃗n⃗n⃗»»» ℵℵ☄☄☃☃̂☃🤔🇺🇸🤦♂️👨👩👧👦
Split: fff, , n⃗n⃗n⃗, »»», , ℵℵ, ☄☄, ☃, ☃̂, ☃, 🤔, 🇺🇸, 🤦♂️, 👨👩👧👦
</pre>
The second test-case is to show that Raku works with strings on the Unicode grapheme level, handles whitespace, combiners, and zero width characters up to Unicode Version 13.0 correctly. (Raku generally tracks updates to the Unicode spec and typically lags no more than a month behind.) For those of you with browsers unable to display the second string, it consists of:
* {LATIN SMALL LETTER F} x 3
* {ZERO WIDTH NO-BREAK SPACE} x 3
* {LATIN SMALL LETTER N, COMBINING RIGHT ARROW ABOVE} x 3
* {RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK} x 3
* {SPACE} x 2,
* {ALEF SYMBOL} x 2,
* {COMET} x 2,
* {SNOWMAN} x 1,
* {SNOWMAN, COMBINING CIRCUMFLEX ACCENT} x 1
* {SNOWMAN} x 1,
* {THINKING FACE} x 1
* {REGIONAL INDICATOR SYMBOL LETTER U, REGIONAL INDICATOR SYMBOL LETTER S} x 1
* {FACE PALM, ZERO WIDTH JOINER, MALE SIGN, VARIATION SELECTOR-16} x 1
* {MAN, ZERO WIDTH JOINER, WOMAN, ZERO WIDTH JOINER, GIRL, ZERO WIDTH JOINER, BOY} x 1
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Prout <Join (', ') <Split 'gHHH5YY++///\\'>>>;
};
Split {
(e.Cur) = (e.Cur);
(e.Cur s.1) s.1 e.X = <Split (e.Cur s.1 s.1) e.X>;
(e.Cur) s.1 e.X = (e.Cur) <Split (s.1) e.X>;
s.1 e.X = <Split (s.1) e.X>;
};
Join {
(e.Joiner) = ;
(e.Joiner) (e.Str) = e.Str;
(e.Joiner) (e.Str) e.Strs = e.Str e.Joiner <Join (e.Joiner) e.Strs>;
};</syntaxhighlight>
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|REXX}}==
===version 1===
<
parse arg str /*obtain optional arguments from the CL*/
if str=='' then str= 'gHHH5YY++///\' /*Not specified? Then use the default.*/
Line 1,494 ⟶ 2,497:
end /*j*/ /* [↓] keep peeling chars until done. */
say ' input string: ' str /*display the original string & output.*/
say ' output string: ' $ /*stick a fork in it, we're all done. */</
{{out|output|text= when using the default input:}}
<pre>
Line 1,502 ⟶ 2,505:
===version 2===
<
Parse arg str /*obtain optional arguments from the CL*/
if str=='' then str= 'gHHH5YY++///\' /*Not specified? Then use the default.*/
Line 1,521 ⟶ 2,524:
result=result||x
say ' input string: ' input
say ' output string: ' result </
{{out]]
<pre> input string: gHHH5YY++///\
Line 1,527 ⟶ 2,530:
=={{header|Ring}}==
<
see split("gHHH5YY++///\")
Line 1,542 ⟶ 2,545:
next
return split
</syntaxhighlight>
Output:
<pre>
g, HHH, 5, YY, ++, ///, \
</pre>
=={{header|RPL}}==
≪ → text
≪ "" text 1 1 SUB
1 text SIZE '''FOR''' j
text j DUP SUB
'''IF''' DUP2 ≠ '''THEN''' SWAP DROP ", " OVER + '''END'''
ROT SWAP + SWAP
'''NEXT '''DROP
≫ ≫ ‘<span style="color:blue">COMASPLT</span>’ STO
=={{header|Ruby}}==
<
puts " input string: #{str}"
s = str.chars.chunk(&:itself).map{|_,a| a.join}.join(", ")
Line 1,556 ⟶ 2,569:
end
split("gHHH5YY++///\\")</
{{out}}
Line 1,565 ⟶ 2,578:
=={{header|Rust}}==
<
let chars: Vec<_> = string.chars().collect();
let mut result = Vec::new();
Line 1,598 ⟶ 2,611:
println!("input string: {}", test_string);
println!("output string: {}", splitter(test_string));
}</
{{out}}
Line 1,609 ⟶ 2,622:
output string: g, HHH, 5, YY, ++, ///, \
</pre>
===Alternate using IterTools===
<syntaxhighlight lang="rust">use itertools::Itertools;
pub fn split_text(s: &str) -> Vec<String> {
let mut r = Vec::new();
for (_, group) in &s.chars().into_iter().group_by(|e| *e) {
r.push(group.map(|e| e.to_string()).join(""));
}
r
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_splitting_text() {
assert_eq!(split_text("gHHH5YY++///\\"), vec!["g", "HHH", "5", "YY", "++", "///", "\\"]);
assert!(split_text("").is_empty());
}
}
</syntaxhighlight>
=={{header|Scala}}==
<
// based on a change of character (left to right).
// See https://rosettacode.org/wiki/Split_a_character_string_based_on_change_of_character#Scala
Line 1,618 ⟶ 2,654:
(s + 'X').sliding(2).map(pair => pair.head + (if (pair.head != pair.last) ", " else "")).mkString("")
println(runLengthSplit("""gHHH5YY++///\"""))</
{{Out}}See it in running in your browser by [https://scalafiddle.io/sf/c4dp8GT/2 ScalaFiddle (JavaScript)]
or by [https://scastie.scala-lang.org/mDoBS77YSG2Z7w5xdAPzcw Scastie (JVM)].
<syntaxhighlight lang="scala">
def runLengthSplit(s:String):List[String] = {
def recursiveSplit(acc:List[String], rest:String): List[String] = rest match {
Line 1,637 ⟶ 2,673:
val result = runLengthSplit("""gHHH5YY++///\""")
println(result.mkString(","))
</syntaxhighlight>
{{Out}}
<pre>
Line 1,644 ⟶ 2,680:
=={{header|Sed}}==
<syntaxhighlight lang="sed">echo 'gHHH5YY++///\' | sed 's/\(.\)\1*/&, /g;s/, $//'</syntaxhighlight>
Output:
g, HHH, 5, YY, ++, ///, \
=={{
<syntaxhighlight lang="setl">program split_a_character_string_based_on_change_of_character;
s := "gHHH5YY++///\\";
print(join_strings(", ", split_on_change(s)));
proc split_on_change(s);
parts := [];
loop while s /= "" do
parts with:= span(s, s(1));
end loop;
return parts;
end proc;
proc join_strings(s, parts);
if parts=[] then return ""; end if;
return parts(1) +/ [s + part : part in parts(2..)];
end proc;
end program;</syntaxhighlight>
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|Sidef}}==
<
gather {
while (var match = (str =~ /((.)\g{-1}*)/g)) {
Line 1,660 ⟶ 2,713:
}
say group(ARGV[0] \\ 'gHHH5YY++///\\').join(', ')</
{{out}}
<pre>
g, HHH, 5, YY, ++, ///, \
</pre>
=={{header|SNOBOL4}}==
{{works with|SNOBOL4, SPITBOL for Linux}}
<syntaxhighlight lang="snobol4">
* Program: split_on_change_of_character.sbl
* To run: sbl split_on_change_of_character.sbl
* Description: Split a (character) string into comma (plus a blank)
* delimited strings based on a change of character (left to right).
*
* Blanks should be treated as any other character
* (except they are problematic to display clearly).
* The same applies to commas.
*
* For instance, the string:
*
* gHHH5YY++///\
* should be split and show:
*
* g, HHH, 5, YY, ++, ///, \
* Comment: Tested using the Spitbol for Linux version of SNOBOL4
lf = substr(&alphabet,11,1) ;* New line or line feed
* Function split_cc will split a string on a change of character.
define('split_cc(s)tchar,target,post')
:(split_cc_end)
split_cc
tchar = substr(s,1,1) :f(freturn)
split_cc_pat = span(*tchar) . target (rpos(0) | len(1) . tchar rem) . post
split_cc2
s ? split_cc_pat = post :f(split_cc3)
split_cc = (ident(split_cc) target, split_cc ', ' target) :s(split_cc2)
split_cc3
:(return)
split_cc_end
test_string = "gHHH5YY++///\"
output = test_string lf
split_string = split_cc(test_string)
output = split_string
END</syntaxhighlight>
{{out}}
<pre>
gHHH5YY++///\
g, HHH, 5, YY, ++, ///, \
</pre>
=={{header|Standard ML}}==
<
* Head-Tail implementation of grouping
*)
Line 1,676 ⟶ 2,777:
fun group xs = group' nil xs
fun groupString str = String.concatWith ", " (map implode (group (explode str)))</
{{out}}
<pre>- groupString "gHHH5YY++///\\";
val it = "g, HHH, 5, YY, ++, ///, \\" : string</pre>
=={{header|Swift}}==
<
func splitOnChanges() -> [String] {
guard !isEmpty else {
Line 1,711 ⟶ 2,811:
}
print("gHHH5YY++///\\".splitOnChanges().joined(separator: ", "))</
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|Tailspin}}==
<syntaxhighlight lang="tailspin">
composer splitEquals
<reps> <nextReps>*
rule reps: <'(.)\1*'>
rule nextReps: <reps> -> \(', ' ! $ ! \)
end splitEquals
'gHHH5YY++///\' -> splitEquals -> !OUT::write
</syntaxhighlight>
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|tbas}}==
{{Trans|BBC BASIC}}
<
DIM c$, d$, split$, i%
c$ = LEFT$(s$, 1)
Line 1,735 ⟶ 2,848:
PRINT SPLITUNIQUE$("gHHH5YY++///\")
END</
=={{header|Tcl}}==
This is most concise with regular expressions. Note well the two steps: it could be achieved in one very clever regexp, but being that clever is usually a bad idea (for both readability and performance, in this case).
<
regsub -all {(.)\1*} $string {\0, } string
regsub {, $} $string {} string
puts $string</
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|Transd}}==
The task doesn't state explicitly about the order in which substrings should be
displayed. So, here are two variants: one is order-preserving, the other is not
order-preserving.
<syntaxhighlight lang="Scheme">#lang transd
MainModule: {
s: "gHHH5YY++///\\",
_start: (λ
(with res ""
(for c in (split s "") do
(if (neq Char(c) (back res)) (+= res ", "))
(+= res c))
(textout res))
(lout "Second variant: ")
(for v in (values (group-by (split s ""))) do
(textout (if @idx ", ") (join v "")))
)
}</syntaxhighlight>
{{out}}
<pre>
g, HHH, 5, YY, ++, ///, \
Second variant:
++, ///, 5, HHH, YY, \, g
</pre>
=={{header|Uiua}}==
{{works with|Uiua|0.11.1}}
<syntaxhighlight lang="uiua">
&p/$"_, _"⊜□+1⊸⊛ "gHHH5YY++///\\"
</syntaxhighlight>
{{out}}
<pre>
g, HHH, 5, YY, ++, ///, \
</pre>
=={{header|VBA}}==
<syntaxhighlight lang="vb">
Option Explicit
Line 1,785 ⟶ 2,937:
Split_Special = R
End Function
</syntaxhighlight>
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">fn main() {
println(splitter('gHHH5YY++///\\')) \\ The "\" character needs to be escaped.
}
fn splitter(text string) string {
mut check := text.substr(0, 1)
mut new_text, mut temp := '', ''
for index, _ in text {
temp = text.substr(index, index + 1)
if temp != check {
new_text = new_text + ', '
check = temp
}
new_text = new_text + temp
}
return new_text
}
</syntaxhighlight>
{{out}}
<pre>
g, HHH, 5, YY, ++, ///, \
</pre>
=={{header|Wren}}==
<syntaxhighlight lang="wren">var split = Fn.new { |s|
if (s.count == 0) return ""
var res = []
var last = s[0]
var curr = last
for (c in s.skip(1)) {
if (c == last) {
curr = curr + c
} else {
res.add(curr)
curr = c
}
last = c
}
res.add(curr)
return res.join(", ")
}
var s = "gHHH5YY++///\\"
System.print(split.call(s))</syntaxhighlight>
{{out}}
<pre>
g, HHH, 5, YY, ++, ///, \
</pre>
=={{header|XLISP}}==
<
(defun delim (old-list new-list current-char)
(if (null old-list)
Line 1,801 ⟶ 3,005:
(list->string (delim (string->list s) '() (car (string->list s)))) )
(display (delimit "gHHH5YY++///\\")) ;; NB. The "\" character needs to be escaped</
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">string 0; \change to zero-terminated convention
char S;
[S:= "gHHH5YY++///\";
while S(0) do
[ChOut(0, S(0));
if S(1)#S(0) & S(1)#0 then Text(0, ", ");
S:= S+1;
];
]</syntaxhighlight>
{{out}}
<pre>
g, HHH, 5, YY, ++, ///, \
</pre>
=={{header|Yabasic}}==
<syntaxhighlight lang="freebasic">sub esplit$(instring$)
if len(instring$) < 2 return instring$
ret$ = left$(instring$,1)
for i = 2 to len(instring$)
if mid$(instring$,i,1) <> mid$(instring$, i - 1, 1) ret$ = ret$ + ", "
ret$ = ret$ + mid$(instring$, i, 1)
next i
return ret$
end sub
print esplit$("gHHH5YY++///\\")</syntaxhighlight>
=={{header|Z80 Assembly}}==
<syntaxhighlight lang="z80">PrintChar equ &BB5A ;Amstrad CPC BIOS call
Terminator equ 0 ;marks the end of a string
org &8000
LD HL,StringA
loop:
ld a,(HL) ;load a char from (HL)
cp Terminator ;is it the terminator?
ret z ;if so, exit
ld e,a ;store this char in E temporarily
inc hl ;next char
ld a,(HL) ;get next char
cp Terminator ;is the next char the terminator?
jp z,StringDone ;if so, print E and exit.
;needed to prevent the last char from getting a comma and space.
dec hl ;go back one so we don't skip any chars
cp e ;does (HL) == (HL+1)?
push af
ld a,e
call PrintChar ;either way, print E to screen.
pop af ;retrieve the results of the last compare.
jr z,SkipComma ;if A=E, no comma or space. Just loop again.
ld a,','
call PrintChar
ld a,' '
call PrintChar
SkipComma:
inc hl ;next char
jp loop ;back to start
StringDone:
ld a,e ;last character in string is printed here.
jp PrintChar
ReturnToBasic:
RET
StringA:
byte "gHHH5YY++///\",0</syntaxhighlight>
{{out}}
<pre>
g, HHH, 5, YY, ++, ///, \
</pre>
=={{header|zkl}}==
<
C,out := str[0],Sink(C);
foreach c in (str[1,*]){ out.write(if(c==C) c else String(", ",C=c)) }
out.close();
}
group("gHHH5YY++///\\").println();</
{{out}}
<pre>
Line 1,818 ⟶ 3,101:
=={{header|ZX Spectrum Basic}}==
<
20 LET c$=s$(1)
30 LET n$=c$
Line 1,826 ⟶ 3,109:
70 LET c$=s$(i)
80 NEXT i
90 PRINT n$</
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
|