C# 修剪()与替换()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18902602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Trim() vs replace()
提问by SARAVAN
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?
在 C# 中,string
如果我们想将" "
字符串替换为string.empty
,是否可以使用stringValue.Trim()
或stringValue.replace(" ", string.empty)
。两者都有相同的目的。但哪个更好?
采纳答案by Nick Zimmerman
Trim()
and Replace()
do not serve the same purpose.
Trim()
并且Replace()
不用于相同的目的。
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.
Trim()
从字符串的开头和结尾删除所有空白字符。这意味着spaces
, tabs
, new lines
,returns
和其他各种空白字符。
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.
Replace()
只用给定的替换替换指定的字符。所以Replace(" ", string.empty)
只会用空字符串替换空格。Replace() 还用给定的替换替换指定字符串的所有实例,而不仅仅是字符串开头和结尾的那些。
回答by Nemanja Boric
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.
String.Replace
将删除所有(且仅)空格字符,String.Trim
并将删除字符串开头和结尾的所有空白字符,而不是中间的空白字符。
var tmp = " hello world \t";
var res1 = tmp.Trim(); // "hello world"
var res2 = tmp.Replace(" ", String.Empty); // "helloworld\t"
回答by Forte L.
String.Trim()
will remove only leading and trailing spaces. So you'll have to use String.Replace()
for your purpose.
String.Trim()
将仅删除前导和尾随空格。所以你必须String.Replace()
为你的目的使用。
回答by DotNetUser
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.
Trim 消除前导和尾随空格,而 Replace 更改字符串数据。它将一个子字符串的所有出现更改为另一个子字符串。它还处理字符替换。
回答by Joe
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.
替换将替换字符串中的任何位置。Trim 只会从字符串的开头和结尾修剪空白.....所以他们实际上做不同的事情。
回答by MSTr
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 :
正如 Nick Zimmerman 所说 Trim() 从字符串的开头和结尾删除所有空白字符。但是您可以以不同的方式使用它:
Trim(char[] trimChars) which removes all leading and trailing occurrences of a set of characters specified in the array passed in as a parameter.
Trim(char[] trimChars) 删除作为参数传入的数组中指定的一组字符的所有前导和尾随出现。
回答by Mahtab Alam
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.
在这种情况下,输出将为abcd
. 修剪只删除你想从字符串的开头和结尾修剪的空格或符号。
But in case of string.Replace() is that it will replace the string which you want to get replaced for ex.
但是在 string.Replace() 的情况下,它将替换您想要替换的字符串。
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
.
在这种情况下,o/p 将是mahtab khan
。
If you want to remove space in between the strings (in this case output will be mahtabalam)
如果要删除字符串之间的空格(在这种情况下,输出将是 mahtabalam)
string name=Console.ReadLine(); //input be mahtab alam
string result= name.Replace(" ",string.Empty);
Console.WriteLine(result)
回答by 1_bug
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.
Trim 只能从字符串的开头和/或结尾消除空白和非空白字符。替换可以从字符串中的任何位置删除子字符串。
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()
.
TL;DR:如果您只想从字符串的开头或/和结尾删除单个字符,请Trim()
否则调用Replace()
.