RegEx. Replace method (string, string)
Starting from the first character in the input string, replace the string with all matching items in the specified regular expression pattern.
// This code example demonstrates the system. Text. Regular-
// Expressions. RegEx. Replace (string, string) method.
Using system;
Using system. Text. regularexpressions;
Class sample
{
Public static void main ()
{
// Create a regular expression that matches a series of one
// Or more white spaces.
String Pattern = @ "/S + ";
RegEx rgx = new RegEx (pattern );
// Declare a string consisting of text and white spaces.
String inputstr = "a B C D ";
// Replace runs of white space in the input string with
// Comma and a blank.
String outputstr = rgx. Replace (inputstr ,",");
// Display the resulting string.
Console. writeline ("pattern:/" {0}/"", pattern );
Console. writeline ("input string:/" {0}/"", inputstr );
Console. writeline ("output string:/" {0}/"", outputstr );
}
}
/*
This code example produces the following results:
Pattern: "/S +"
Input string: "a B C D"
Output string: "a, B, c, d"
*/
An irregular string (Chinese character) that requires each 30 Chinese characters to be truncated.
String STR = "the company issued a third opinion of the thousands of days on the plane every year yuan angle play that can be obtained from the aspect and the issuing Line Method High Speed Steel color light three authority thermal opportunity and man ah hancake let me go to agenca, And you can wipe it there ";
String [] strarr = RegEx. Replace (STR, @ "[/u4e00-/u9fa5] {30}", @ "$ &~ "). Trimend ('~ '). Split ('~ ');
Foreach (string s in strarr)
Console. writeline (s );