Script;
Code:
public static class Extension {
public static string Replace(this string subject,string search, string replace, int count) {
string new_String = "";
for(int i = 0; i < subject.Length; i++){
if(subject[i] == search[0]) {
string che = "";
for(int e = 0; e < search.Length; e++) {
if (subject[i+e] == search[e]) {
che += search[e];
}
}
if (che == search) {
if (count > 0) {
new_String += replace;
i += search.Length-1;
count--;
}
else {
new_String += subject[i];
}
}
else {
new_String += subject[i];
}
}
else {
new_String += subject[i];
}
}
return new_String;
}
public static string Replace(this string subject,char search, char replace, int count) {
string new_String = "";
for(int i = 0; i < subject.Length; i++){
if(subject[i] == search) {
if (count > 0) {
new_String += replace;
count--;
}
else {
new_String += subject[i];
}
}
else {
new_String += subject[i];
}
}
return new_String;
}
}
Example1:
Usage:
Code:
"Hey Hey".Replace("He", "he", 1);
Result:
Example2:
Usage:
Code:
"Heystack HeyStack".Replace('e', 'a', 1);
Result: