@@ -493,6 +493,50 @@ Also using a different value or an incorrect starting index won't match:
493
493
say $integer.substr-eq(42, 3); #-> False
494
494
say $integer.substr-eq(7342, 0); #-> False
495
495
496
+ = head2 method substr-rw
497
+
498
+ method substr-rw($from, $length?)
499
+
500
+ A version of C < substr > that returns a writable reference to a part of a
501
+ string variable. Its first argument, C < $from > specifies the index in the
502
+ string from which a substitution should occur, and its last argument,
503
+ C < $length > specifies how many characters are to be replaced.
504
+
505
+ For example, in its method form, if one wants to take the string C < "abc" >
506
+ and replace the second character (at index 1) with the letter C < "z" > , then
507
+ one do this:
508
+
509
+ my $string = "abc";
510
+ $string.substr-rw(1, 1) = "z";
511
+ $string.say; #-> azc
512
+
513
+ C < substr-rw > also has a function form, so the above example can also be
514
+ written like so:
515
+
516
+ my $string = "abc";
517
+ substr-rw($string, 1, 1) = "z";
518
+ $string.say; #-> azc
519
+
520
+ It is also possible to alias the writable reference returned by C < substr-rw >
521
+ for repeated operations:
522
+
523
+ my $string = "A character in the 'Flintstones' is: barney";
524
+ $string ~~ /(barney)/;
525
+ my $ref := substr-rw($string, $0.from, $0.to);
526
+ $string.say;
527
+ #-> A character in the 'Flintstones' is: barney
528
+ $ref = "fred";
529
+ $string.say;
530
+ #-> A character in the 'Flintstones' is: fred
531
+ $ref = "wilma";
532
+ $string.say;
533
+ #-> A character in the 'Flintstones' is: wilma
534
+
535
+ Notice that the start position and length of string to replace has been
536
+ specified via the C < .from > and C < .to > methods on the C < Match > object, C < $0 > .
537
+ It is thus not necessary to count characters in order to replace a
538
+ substring, hence making the code more flexible.
539
+
496
540
= head2 method succ
497
541
498
542
method succ(Str:D) returns Str:D
0 commit comments