Friday, October 23, 2020

Difference between Trim() and Replace() in c#

 Trim removes all whitespace characters from the beginning and end of the string. that means spaces, tabs, new lines, returns and other assorted whitespace characters.


Replace() only replaces the designed characters with given replacement. so replace(" ", string.empty) will only replace space with empty string. replace also

Replace all instances of the designated string with the given replacement not just those at the beginning end of the string.

String.Replace will remove all (and only) space characters, and string. Trim will remove all whitespace characters from the beginning and the end of the string not once int the middle.

Example:

var tmp=" hello world \t";

var res1=tmp.trim(); //output is hello world

var res2=res1.Replace(" ",String.empty); // helloworld





No comments:

Post a Comment

Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...