Thread replace nth occurence: Code verstehen (16 answers)
Opened by Student87 at 2013-03-10 11:04

Gast wer
 2013-03-10 12:27
#166299 #166299
Das lässt sich auch anders schreiben:

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
my $str = "blublublublublu";
my $such='lu';
my $repl='LA';

my $count = 3;
my $pos=0;

while($count and $pos >= 0)
{
  $pos=index($str,$such,$pos);
  $count--;
}

if($pos>0){
  substr($str,$pos,length($such),$repl);
}

Last edited: 2013-03-10 12:50:27 +0100 (CET)

View full thread replace nth occurence: Code verstehen