This example describes the C # custom string substitution replace method. Share to everyone for your reference. The implementation method is as follows:
First, the question:
The first encounter is a title algorithm, which replaces some fragments of the original string with the specified new string fragment, for example, replacing the source string: CDE in ABCDEABCDFBCDEFG with 12345 to get the result string: AB12345ABCDFB12345FG, That is: ABCDEABCDFBCDEFG-AB12345ABCDFB12345FG.
Second, the realization method:
Obviously you can't use string. Replace method, you need to customize a method string Replace (String originalstring, String strtobereplaced, String strtoreplace), the following is my implementation code, Completed in half an hour, pass the debugging and the routine data test verification, still is a pass bar.
The code is as follows:
public static string Replace (String originalstring, String strtobereplaced, String strtoreplace)
{
string resultstring = null;
char[] Originalchararray = Originalstring.tochararray ();
char[] Strtobechararray = Strtobereplaced.tochararray ();
char[] Strtochararray = Strtoreplace.tochararray ();
list<char> newcharlist = new list<char> ();
for (int i = 0; i < Originalchararray.count (); i++)
{
if (originalchararray[i] = = Strtobechararra Y[0])
{
bool isreplace = false;
for (int j = 0; J < Strtobechararray.count (); j + +)
{
if (((i + j) < Originalchararray.count () )
&& (originalchararray[i + j] = = Strtobechararray[j]))
{
Isreplace = true;
}
Else
{
Isreplace = false;
break;
}
}
if (isreplace)
{
i + = Strtobechararray.count () –1;
for (int k = 0; k < Strtochararray.count (); k++)
{
Newcharlist.add (Strtochararray[k]);
}
}
Else
{
Newcharlist.add (Originalchararray[i]);
}
}
Else
{
Newcharlist.add (Originalchararray[i]);
}
}
resultstring = string. Join ("", newcharlist);
return resultstring;
}
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
C # Custom string substitution Replace Replace method instance
This address: http://www.paobuke.com/develop/c-develop/pbk23319.html
Related content C # using regular expressions to hide phone numbers median four-bit for *c# computing standard deviation equivalent to Stdev function instance in Excel C # Implementation of Web page authorization Action Logic encapsulation example simple way to determine whether a collection is a subset of another collection in C #
C # Programming implements rounding and Redundancy methods C # verifies that a given string form date is a legitimate method C # implements three ways to send mail C # implements a JSON string processing instance
C # Custom string substitution Replace Replace method instance