Skip to content

Commit db18b26

Browse files
authored
Merge pull request #3292 from tinmarino/merge_faq_string
Faq: string common operations
2 parents 061042b + 37d4e36 commit db18b26

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 example of L<contextualization|/language/contexts> can numify any string you could enter as a L<literal 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
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+
To get an hexadecimal list of each byte of a string (i.e. hex encoder),
330+
first convert the string to a L<Blob|/type/Blob> with L<.encode|/routine/encode>.
331+
332+
=begin code
333+
say "I ❤ 🦋".encode>>.base(16); OUTPUT: «(49 20 E2 9D A4 20 F0 9F A6 8B)␤»
334+
=end code
335+
336+
Note that L<.gist|/routine/gist> or L<.raku|/routine/perl> methods can be useful for variable introspection:
337+
338+
=begin code
339+
say "I ❤ 🦋".encode.raku; # OUTPUT: «utf8.new(73,32,226,157,164,32,240,159,166,139)␤»
340+
say "I ❤ 🦋".encode.gist; # OUTPUT: «utf8:0x<49 20 E2 9D A4 20 F0 9F A6 8B>␤»
341+
=end code
342+
343+
=head2 String: How can I remove from a string some characters by index
344+
345+
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:
346+
347+
=begin code
348+
say '0123456789'.comb[(^* (-) (1..3, 8).flat).keys.sort].join; # OUTPUT: «045679␤»
349+
=end code
350+
351+
If the string is large, L<.comb|/routine/comb> can take time. In which case, L<.substr-rw|/routine/substr-rw> is faster:
352+
353+
=begin code
354+
multi postcircumfix:<[- ]> (Str:D $str is copy, +@indices) {
355+
for @indices.reverse {
356+
when Int { $str.substr-rw($_,1) = '' }
357+
when Range { $str.substr-rw($_ ) = '' }
358+
}
359+
return $str;
360+
}
361+
362+
say '0123456789'[- 1..3, 8 ]; # OUTPUT: «045679␤»
363+
=end code
364+
365+
=head2 String: How can I split a string in equal parts
366+
367+
L<.comb|/routine/comb> is accepting an optional L<Int|/type/Int>:
368+
369+
=begin code
370+
.say for 'abcdefghijklmnopqrstuvwxyz'.comb: 8; # OUTPUT: «abcdefgh␤ijklmnop␤qrstuvwx␤yz»
371+
=end code
372+
302373
=head1 Language features
303374
304375
X<|Data::Dumper (FAQ)>

0 commit comments

Comments
 (0)