Skip to content

Commit 2764952

Browse files
committed
Faq: string common operations
1 parent 061042b commit 2764952

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

doc/Language/faq.pod6

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,77 @@ dependencies within a single compilation unit (e.g., file) are possible
299299
through stubbing. Therefore another possible solution is to move
300300
classes into the same compilation unit.
301301
302+
303+
=head1 Common operations
304+
305+
=head2 String: How can I parse and get a L<number|/language/numerics> from a L<string|/language/type/Str>
306+
307+
Use the L<+ prefix|/language/operators#prefix_+>:
308+
309+
=begin code :ok-test<dd>
310+
say "42.123456789123456789"; # OUTPUT: «42.123456789123456789␤»
311+
say +"42.4e-2"; # OUTPUT: «0.424␤»
312+
=end code
313+
314+
This method can numify any string you could enter as a L<litteral number|/language/syntax#Number_literals>.
315+
L<val|/routine/val> routine converts it to L<allomorph|/language/glossary#Allomorph>.
316+
L<unival|/routine/unival> routine converts one unicode codepoint.
317+
318+
=head2 String: How can I check if a string contains a substring and if so, how can I get indices of matches
319+
320+
Use L<.contains|/type/Str#method_contains> or L<.indices|/type/Str#method_indices>:
321+
322+
=begin code :ok-test<dd>
323+
"az and az and az again".contains("az"); # OUTPUT: «True␤»
324+
"az and az and az again".indices("az"); # OUTPUT: «(0 7 14)␤»
325+
=end code
326+
327+
=head2 String: How can I get the hexadecimal representation of a string
328+
329+
First convert it to a L<Blob|/type/Blob> with L<.encode|/routine/encode>.
330+
331+
=begin code :ok-test<dd>
332+
say "I ❤ 🦋".encode>>.base(16); OUTPUT: «(49 20 E2 9D A4 20 F0 9F A6 8B)␤»
333+
=end code
334+
335+
Note that L<.gist|/routine/gist> or L<.raku|/routine/perl> methods are your friends when L<debugging|/programs/01-debugging>:
336+
337+
=begin code
338+
say "I ❤ 🦋".encode.raku; # OUTPUT: «utf8.new(73,32,226,157,164,32,240,159,166,139)␤»
339+
say "I ❤ 🦋".encode.gist; # OUTPUT: «utf8:0x<49 20 E2 9D A4 20 F0 9F A6 8B>␤»
340+
=end code
341+
342+
=head2 String: How can I remove from a string some characters by index
343+
344+
Use L<.comb|/routine/comb> to transform it to a L<Seq|/type/Seq>, then the L<(-) infix|/language/operators#infix_(-),_infix_\\> to remove the unwanted indices:
345+
346+
=begin code :ok-test<dd>
347+
say '0123456789'.comb[(^* (-) (1..3, 8).flat).keys.sort].join; # OUTPUT: «045679␤»
348+
=end code
349+
350+
If the string is large, L<.comb|/routine/comb> can take time. In which case, L<.substr-rw|/routine/substr-rw> is faster:
351+
352+
=begin code :ok-test<dd>
353+
multi postcircumfix:<[- ]> (Str:D $str is copy, +@indices) {
354+
for @indices.reverse {
355+
when Int { $str.substr-rw($_,1) = '' }
356+
when Range { $str.substr-rw($_ ) = '' }
357+
}
358+
return $str;
359+
}
360+
361+
say '0123456789'[- 1..3, 8 ]; # OUTPUT: «045679␤»
362+
=end code
363+
364+
=head2 String: How can I split a string in equal parts
365+
366+
L<.comb|/routine/comb> is accepting a L<Int|/type/Int> or a L<Regex|/type/Regex>:
367+
368+
=begin code :ok-test<dd>
369+
.say for 'abcdefghijklmnopqrstuvwxyz'.comb: 8; # OUTPUT: «abcdefgh␤ijklmnop␤qrstuvwx␤yz»
370+
.say for 'abcdefg4444hijklmnop4444qrstuvwxyz'.comb: /..\d+../; # OUTPUT: «fg4444hi␤op4444qr␤»
371+
=end code
372+
302373
=head1 Language features
303374
304375
X<|Data::Dumper (FAQ)>

0 commit comments

Comments
 (0)