@@ -330,6 +330,40 @@ Examples:
330
330
331
331
sprintf "%ld a big number, %lld a bigger number\n", 4294967295, 4294967296;
332
332
333
+ = head2 subst
334
+
335
+ multi method subst(Str:D: $matcher, $replacement, *%opts)
336
+
337
+ Returns the invocant string where C < $matcher > is replaced by C < $replacement >
338
+ (or the original string, if no match was found).
339
+
340
+ There is an in-place syntactic variant of C < subst > spelled
341
+ C < s/matcher/replacement > .
342
+
343
+ C < $matcher > an be a L < Regex > , or a literal C < Str > . Non-Str matcher arguments
344
+ of type L < Cool > are coereced to to C < Str > for literal matching.
345
+
346
+ my $some-string = "Some foo";
347
+ my $another-string = $some-string.subst(/foo/, "string"); # gives 'Some string'
348
+ $some-string.=subst(/foo/, "string); # in-place substitution. $some-string is now 'Some string'
349
+
350
+
351
+ The replacement can be a closure:
352
+
353
+ my $i = 41;
354
+ my $str = "The answer is secret.";
355
+ my $real-answer = $str.subst(/secret/, {++$i}); # The answer to everything
356
+
357
+ Here are other examples of usage:
358
+
359
+ my $str = "Hey foo foo foo";
360
+ $str.subst(/foo/, "bar", :g); # global substitution - returns Hey bar bar bar
361
+
362
+ $str.subst(/foo/, "no subst", :x(0)); # targeted substitution. Number of times to substitute. Returns back unmodified.
363
+ $str.subst(/foo/, "bar", :x(1)); #replace just the first occurrence.
364
+
365
+ $str.subst(/foo/, "bar", :nth(3)); # replace nth match alone. Replaces the third foo. Returns Hey foo foo bar
366
+
333
367
= head2 substr
334
368
335
369
multi sub substr(Str:D $s, Int:D $from, Int:D $chars = $s.chars - $from) returns Str:D
@@ -424,33 +458,5 @@ Remove the white-space charecters from the end of a string. See also L<trim>.
424
458
425
459
Remove the white-space charecters from the beginning of a string. See also L < trim > .
426
460
427
- = head2 Substitution and replacement operations
428
-
429
- = head2 subst
430
-
431
- Does string substitution, these behave like s/// does in perl5.
432
-
433
- my $some-string = "Some foo";
434
- my $another-string = $some-string.subst(/foo/, "string"); # gives 'Some string'
435
- $some-string.=subst(/foo/, "string); # in-place substitution. $some-string is now 'Some string'
436
-
437
- The syntax usually goes like this:
438
- STRING_TO_REPLACE.subst(/PATTERN/, REPLACEMENT);
439
-
440
- In the case of subst, REPLACEMENT can also be a closure:
441
- my $i = 41;
442
- my $str = "The answer is secret.";
443
- my $real-answer = $str.subst(/secret/, {++$i}); # The answer to everything
444
-
445
- Here are other examples of usage:
446
- my $str = "Hey foo foo foo";
447
- $str.subst(/foo/, "bar", :g); # global substitution - returns Hey bar bar bar
448
-
449
- $str.subst(/foo/, "no subst", :x(0)); # targeted substitution. Number of times to substitute. Returns back unmodified.
450
- $str.subst(/foo/, "bar", :x(1)); #replace just the first occurrence.
451
-
452
- $str.subst(/foo/, "bar", :nth(3)); # replace nth match alone. Replaces the third foo. Returns Hey foo foo bar
453
-
454
-
455
461
= end pod
456
462
0 commit comments