RegEx. Replace Method

Source: Internet
Author: User
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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.