fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.
Fuzion
•
Idioms
•
Idiom # 219: Replace multiple spaces with single space
Idiom # 219: Replace multiple spaces with single space
See
programming-idioms.org
:
Code
t := s.split.as_string " " # replace all ws and trim beginning and end.
What are effects?
Running Example
ex219 is s := "Hello world" t0 := s.split.as_string " " say t0 # alternatively, using a monoid: filter_multi_space : Monoid String is redef infix ∙ (a, b String) => if b=" " && a.ends_with " " then a else a + b redef e => "" t1 := s.as_codepoints .map x->($x) .fold filter_multi_space say t1 # alternatively, using a loop for t := "", t + n c in s.as_codepoints n := if c = " " && t.ends_with c then "" else c else say t
What are effects?
last changed: 2024-07-01
next: Idiom # 220: Create a tuple value