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