C# Trim() vs replace()

2020-03-01 07:10发布

问题:

In a C# string if we want to replace " " in a string to string.empty, is it fine to use stringValue.Trim() or stringValue.replace(" ", string.empty). Both serve the same purpose. But which one is better?

回答1:

Trim() and Replace() do not serve the same purpose.

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 designated characters with the given replacement. So Replace(" ", string.empty) will only replace spaces with empty strings. Replace() also replaces all instances of the designated string with the given replacement, not just those at the beginning and end of the string.



回答2:

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 ones in the middle.

var tmp = "  hello world  \t";
var res1 = tmp.Trim(); // "hello world"
var res2 = tmp.Replace(" ", String.Empty); // "helloworld\t"


回答3:

Trim can eliminate whitespace and non-whitespace characters only from start and/or end of strings. Replace can remove substring from any places in your string.

Example:

Console.WriteLine("{{Hello World!:)".Trim('{',':',')'));  //output: Hello World
Console.WriteLine("{{Hello%World!:)".Trim('{', '%', ':',')'));  //output: Hello%World

Console.WriteLine("{{Hello World!:)".Replace("{{", string.Empty)
                                    .Replace(":)",string.Empty));  //output: Hello World

Console.WriteLine("{{Hello%World!:)".Replace("{{", string.Empty)
                                    .Replace("%", string.Empty)
                                    .Replace(":)",string.Empty));  //output: Hello World

TL;DR: if you want remove just single characters from start or/and end of string use Trim() otherwise call Replace().



回答4:

char [] chartrim={'*'};
string name=Console.ReadLine(); //input be *** abcd **
string result= name.Trim(chartrim);
Console.WriteLine(result);

In this case output will be abcd. Trim only remove the whitespace or the symbol which u want to trim from beginning and end of the string.

But in case of string.Replace() is that it will replace the string which you want to get replaced for ex.

string name=Console.ReadLine(); //input be mahtab alam
string str2="khan";
string result= name.Replace("alam",str2);
Console.WriteLine(result);

In this case o/p will be mahtab khan.

If you want to remove space in between the strings (in this case output will be mahtabalam)

string name=Console.ReadLine(); //input be mahtab alam
string result= name.Replace(" ",string.Empty);
Console.WriteLine(result)


回答5:

String.Trim() will remove only leading and trailing spaces. So you'll have to use String.Replace() for your purpose.



回答6:

Trim eliminates leading and trailing whitespace whereas Replace changes string data. It changes all occurrences of one substring into another substring. It also handles character replacements.



回答7:

Replace will replace it anywhere in the string. Trim will only trim white space from the beginning and end of the string.....So they actually do different things.



回答8:

as Nick Zimmerman said Trim() removes all whitespace characters from the beginning and end of the string. But you can use it in a different way :

Trim(char[] trimChars) which removes all leading and trailing occurrences of a set of characters specified in the array passed in as a parameter.

Check MSDN