string.Trim()行为 [英] string.Trim() Behavior

查看:149
本文介绍了string.Trim()行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据intellisense帮助,string.Trim()"从此实例的开头和结尾删除所有出现或

空白字符。


但是,以下代码似乎没有修改s。


s.Trim(''\ r'');


虽然以下代码修改了s。


s = s.Trim(\ r'');


我明白上面引用的帮助文本是针对不带参数的

方法的版本。但是我会假设Trim

的变体以相同的基本方式工作。


如果这是修改这个实例,为什么我只有这样才能得到效果分配

结果?


谢谢。


-

Jonathan Wood

SoftCircuits编程
http://www.softcircuits.com

解决方案

嗨Jonathan,


字符串值是不可变的。


String被称为immutable,因为一旦创建了

,就无法修改它的值。看似修改String的方法实际上返回一个包含修改的新的
。如果有必要修改类似字符串对象的实际内容,请使用System.Text.StringBuilder

类。


(参见:

ms-help:// MS.NETFrameworkSDKv1.1/cpref/html/frlrfsystemstringclasstopic.htm)


所以Trim方法总是如此给你一个新的字符串。


HTH,

Ryan

" Jonathan Wood" < jw *** @softcircuits.comD'è?óê?t

新闻:Oz ************* @ TK2MSFTNGP04.phx.gbl ...


根据intellisense帮助,string.Trim()"删除所有出现





此实例开头和结尾的空格字符。


但是,以下代码似乎不会修改s。


s.Trim(''\''');


虽然以下代码可以修改s。


s = s.Trim(\ r'');


我理解上面引用的帮助文本是针对此版本的

不带参数的方法。但是我会假设Trim

的变体以相同的基本方式工作。


如果这是修改这个实例,为什么我只有这样才能得到效果



分配


结果?


谢谢。


-

Jonathan Wood

SoftCircuits编程
http://www.softcircuits.com


我的GUESS是故意这样写的。另外,字符串是不可变的
。重新分配给自己实际上会导致一个新的字符串

对象被创建,因此它可以工作。


但是再次,我猜测。


Jonathan Wood写道:


根据intellisense帮助,string.Trim()"删除所有出现或

此实例开头和结尾的空白字符。


但是,以下代码似乎不会修改s。


s.Trim(''\''');


虽然以下代码修改了s。


s = s.Trim(\\\'');


我理解上面引用的帮助文本是针对不带参数的

方法的版本。但是我会假设Trim

的变体以相同的基本方式工作。


如果这是修改这个实例,为什么我只有这样才能得到效果分配

结果?


谢谢。


-

Jonathan Wood

SoftCircuits编程
http://www.softcircuits.com



-

jeremiah();


< blockquote> Jonathan Wood写道:


根据intellisense帮助,string.Trim()"删除所有出现

或空格字符从这个实例的开头和结尾开始。


但是,以下代码似乎没有修改s。


s.Trim (''\''');


虽然以下代码可以修改s。


s = s.Trim(\ r) '');


我理解上面引用的帮助文本是针对不带参数的

方法的版本。但是我会假设Trim

的变体以相同的基本方式工作。


如果这是修改这个实例,为什么我只有这样才能得到效果

分配结果?


谢谢。


-

Jonathan Wood

SoftCircuits编程
http://www.softcircuits.com



嗨Jonathan,


虽然字符串对象是引用对象,但它被归类为不可变的,

意味着一旦创建它就无法修改它的值。 ''修剪''

方法,实际上所有其他字符串方法都不能改变字符串对象的内容,因此它们必须*返回一个新的字符串对象。


行:

s = s.Trim(''\ r'');


不修改对象,它会将''s''重新分配给新创建的字符串

对象,这是因为从's'的当前值中修剪''\ r''。 br />

-

希望这会有所帮助,

Tom Spink


Google首先,稍后再问。


According to the intellisense help, string.Trim() "Removes all occurances or
white space characters from the beginning and end of this instance."

However, the follow code does not appear to modify s.

s.Trim(''\r'');

While the follow code DOES modify s.

s = s.Trim(\r'');

I understand that the help text quoted above is for the version of this
method that takes no arguments. But I would assume that variations of Trim
work the same fundamental way.

If this is modifying this instance, why do I only get the effect if I assign
the result?

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

解决方案

Hi Jonathan,

string value is immutable.

A String is called immutable because its value cannot be modified once it
has been created. Methods that appear to modify a String actually return a
new String containing the modification. If it is necessary to modify the
actual contents of a string-like object, use the System.Text.StringBuilder
class.

(See:
ms-help://MS.NETFrameworkSDKv1.1/cpref/html/frlrfsystemstringclasstopic.htm)

so Trim method always return you a NEW string.

HTH,
Ryan
"Jonathan Wood" <jw***@softcircuits.comD′è?óê?t
news:Oz*************@TK2MSFTNGP04.phx.gbl...

According to the intellisense help, string.Trim() "Removes all occurances

or

white space characters from the beginning and end of this instance."

However, the follow code does not appear to modify s.

s.Trim(''\r'');

While the follow code DOES modify s.

s = s.Trim(\r'');

I understand that the help text quoted above is for the version of this
method that takes no arguments. But I would assume that variations of Trim
work the same fundamental way.

If this is modifying this instance, why do I only get the effect if I

assign

the result?

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com



my GUESS is that it was written that way on purpose. Also, strings are
immutable. reassigning back to itself actually causes a new string
object to be created, thus it works.

but again, i''m guessing.

Jonathan Wood wrote:

According to the intellisense help, string.Trim() "Removes all occurances or
white space characters from the beginning and end of this instance."

However, the follow code does not appear to modify s.

s.Trim(''\r'');

While the follow code DOES modify s.

s = s.Trim(\r'');

I understand that the help text quoted above is for the version of this
method that takes no arguments. But I would assume that variations of Trim
work the same fundamental way.

If this is modifying this instance, why do I only get the effect if I assign
the result?

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

--
jeremiah();


Jonathan Wood wrote:

According to the intellisense help, string.Trim() "Removes all occurances
or white space characters from the beginning and end of this instance."

However, the follow code does not appear to modify s.

s.Trim(''\r'');

While the follow code DOES modify s.

s = s.Trim(\r'');

I understand that the help text quoted above is for the version of this
method that takes no arguments. But I would assume that variations of Trim
work the same fundamental way.

If this is modifying this instance, why do I only get the effect if I
assign the result?

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

Hi Jonathan,

While a string object is a reference object, it is classed as immutable,
meaning that you cannot modify it''s value once it''s created. The ''Trim''
method, and indeed all other string methods cannot alter the contents of
the string object, so they *must* return a new string object.

The line:
s = s.Trim(''\r'');

Does not modify the object, it reassigns ''s'' to the newly created string
object, that results from trimming ''\r'' from the current value of ''s''.

--
Hope this helps,
Tom Spink

Google first, ask later.


这篇关于string.Trim()行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆