C # Wonderful functions-8. String Remove () and Replace ()

Source: Internet
Author: User

When operating on strings, we often need to delete or replace some substrings. Remove () and Replace () functions are useful in this case.

Remove ()-delete a part of the substring

We all know that substring can return part of a string. When we want to retain a part of the string substring to discard other parts, we can use substring. When we want to delete a part of the string and retain other parts, we will use Remove.

Remove has two forms:

  • Remove (int startIndex)

    • Delete all characters in the string from the specified position to the last position.
  • Remove (int startIndex, int length)
    • Delete a specified number of characters from the specified position in the instance.

Remove checks the region,

For the first form when

1. startIndex is smaller than zero or

2. the position specified by startIndex is not in this string;

For the second form when

1. startIndex or count is smaller than zero or

2. The sum of startIndex and count specifies the location of the real exception.

An exception is thrown.

 

In Remove () and substring (), it is easy to select which one. See the following example:


String test = "Now is the time for all good men to come to the aid of their country .";



// Take the first 10 Characters

Var sliceUsingSubstring = test. Substring (0, 10 );



// Delete All characters after 10th characters

Var sliceUsingRemove = test. Remove (10 );

If we want to get the first 10 characters, both methods can be used, but Remove seems a little concise, because you do not need to point the starting point.

So what if you want to get the last 10 characters?

String test = "Now is the time for all good men to come to the aid of their country .";


// Obtain the part from length-10 to the end.

Var sliceUsingSubstring = test. Substring (test. Length-10 );



// Delete from the beginning to length-10, and leave the rest

Var sliceUsingRemove = test. Remove (0, test. Length-10 );

In this case, the substring looks more concise.

Now it seems that they are actually, but. net provides two methods in this case, which can be used based on your preferences.

 

However, when we want to retain or delete the middle part of the string, we can see that they are different:


// Obtain a substring of 10 starting from 10th

Var sliceUsingSubstring = test. Substring (10, 10 );

// Delete the part after 20th characters, and then delete the first 10

Var sliceUsingRemove = test. Remove (20). Remove (0, 10 );

 

In this case, it is obvious that substring is easy to read and only needs one operation.

 

However, if we want to delete a string in the middle:

 

// Obtain the substring starting from 0 and ending from 20th.

Var sliceUsingSubstring = test. Substring (0, 10) + test. Substring (20 );

// Delete 10 characters from 10th

Var sliceUsingRemove = test. Remove (10, 10 );

 

In this case, remove is much simpler and easier to read.

 

Replace ()-Replace char or String

Replace all matches of the specified Unicode character or String in this instance with other specified Unicode characters orString.

 

Two reloads

 

  • Replace (char oldChar, char newChar)

    Replace all matching items of the specified Unicode character in this instance with other specified Unicode characters.

Replace (string oldValue, string newValue)

SetStringReplace all matching items with other specifiedString.

 

 

 

String test = "Now is the time for all good men to come to the aid of their country .";

Var politicallyCorrect = test. Replace ("men", "people ");

Var spacesToPipes = test. Replace ('', '| ');

Var withoutThe = test. Replace ("the", string. Empty );

In the next example, you want to change a piece of data containing the "<BR/>" HTML code to Environment. NewLine:

 

string test = "Some data &amp; markup was loaded from a data source.<BR/>&nbsp;Oh look, we started a new line!";



var cleansedData = test.Replace("<BR/>", Environment.NewLine);



var moreCleansedData = test.Replace("&amp;", "&")

.Replace("&nbsp;", " ")

.Replace("<BR/>", Environment.NewLine);

At the same time, I conducted the same experiment on stringbuilder and found that in this case, stringbuilder runs very slowly:

 

var morePerformantCleansedData = new StringBuilder(test)

.Replace("&amp;", "&")

.Replace("&nbsp;", " ")

.Replace("<BR/>", Environment.NewLine)

.ToString();
For more exciting articles, read ASP. NET (Alex Song)

Translated from C #/. NET Little Wonders: The String Remove () and Replace () Methods

Related Article

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.