C # string replace _ case insensitive

Source: Internet
Author: User

The source code translation tool is basically complete, with only the last question left: How to ignore the case sensitivity of the original text when replacing the string. Because string. Replace in C # cannot ignore case sensitivity. The first thing that comes to mind is regular expressions. I checked them online and used regular expressions in combination with some logic operations to replace strings without case sensitivity. However, the regular expression method is difficult to use, and experiments prove that the speed is not the fastest. What I want to talk about is the most convenient to use, and the execution speed is also the fastest. Is to use strings in the Microsoft. VisualBasic namespace.

 

First, add the reference Microsoft. VisualBasic. dll

Introduce the namespace using Microsoft. VisualBasic;

 

Use the replace method. The following parameters are used:

 

Strings. replace (content of the original string, content of the field to be replaced, content of the replaced field, starting from the nth digit (note that the default value is 1 ), number of replicas (-1 indicates all), Case Insensitive );

 

Example:

 

 

String STR = "aabbcc_aabbcc ";

// Replace the first BB in the original string with dd

STR = strings. Replace (STR, "BB", "DD", 1, 1, comparemethod. Binary );

Printed result: aaddcc_aabbcc

(Comparemethod. Binary indicates that binary is used for searching. Because the binary code of uppercase and lowercase letters is obviously different, it is case-insensitive)

 

// Replace all the BB strings with dd

STR = strings. Replace (STR, "BB", "DD", 1,-1, comparemethod. Binary );

Printed result: aaddcc_aaddcc

 

// Replace all BB values in the original string with dd to ignore case sensitivity.

STR = strings. Replace (STR, "BB", "DD", 1,-1, comparemethod. Text );

Printed result: aaddcc_aaddcc

(Comparemethod. text means to use text to search, so it is case-insensitive)

This method only needs to pass in several important parameters, which is very convenient to use. Because the principle is to split () first and then join (), the speed is very fast. It is much faster than the regular expression method.

Note that replace cannot replace null strings. For example

String STR = "";

If you replace strings. Replace, null is returned.

STR = strings. Replace (STR, "", "test", 1,-1, comparemethod. Binary );

STR = NULL true

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.