int head = 0;
int next;
int searchLen = search.length();
StringValue result = null;
while (head <= (next = indexOf(subject, search, head, isInsensitive))) {
if (result == null)
result = subject.createStringBuilder();
result = result.append(subject, head, next);
result = result.append(replace);
if (head < next + searchLen)
head = next + searchLen;
else
head += 1;
count++;
}
if (count != 0 && result != null) {
countV.set(LongValue.create(count));
int subjectLength = subject.length();
if (head > 0 && head < subjectLength)
result = result.append(subject, head, subjectLength);
return result;
}
else
return subject;