C#Trim()与replace() [英] C# Trim() vs replace()
问题描述
在C# string
中,如果我们想将字符串中的"
替换为 string.empty
,可以使用 stringValue.Trim()
或 stringValue.replace(",string.empty)
.两者具有相同的目的.但是哪个更好?
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?
推荐答案
Trim()
和 Replace()
的作用不同.
Trim()
删除字符串开头和结尾的所有空格字符.这意味着空格
,标签
,换行
,返回
和其他各种空白字符.
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()
仅将指定的字符替换为给定的替换.因此, Replace(",string.empty)
仅将空格替换为空字符串.Replace()还会用给定的替换项替换指定字符串的所有实例,而不仅仅是在字符串开头和结尾处的那些实例.
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.
这篇关于C#Trim()与replace()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!