如果字符串是不可变的,那我怎么修剪它们呢? [英] If strings are immutable, then how come I can trim them?

查看:120
本文介绍了如果字符串是不可变的,那我怎么修剪它们呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我只是浪费了大约30分钟,试图弄清原因:

myString.Replace("5之6","3之5")

我不会更改我的字符串...直到我读到有关字符串不可变并且需要创建新字符串的文章.

myNewString = myString.Replace("5之6","5之3")- -一切工作都很酷..

好吧..我知道,字符串是堆上的引用-无法更改...等等等等...

但后来我开始考虑LTring和RTrim ...我知道我以前使用过这些更改字符串而不创建新的string或stringBuilders.

请问有人能启发我吗?

So I just wasted about 30 minutes trying to figure out why:

myString.Replace("5 of 6", "3 of 5")

would not CHANGE my string... until I read a post about strings being immutable and that I needed to create a new string.

myNewString = myString.Replace("5 of 6", "3 of 5")   --  everything worked cool..

OK FINE.. I understand, Strings are references on the heap - cant be changed...blah blah...

But then I began to think about LTring and RTrim... I KNOW I have used those before to change strings without creating new strings or stringBuilders.

Can someone enlighten me please?

推荐答案

好吧,LString和RString也会在VB.NET中返回新字符串.

Well, LString and RString returns the new string in VB.NET as well...

http://msdn.microsoft.com/en-us/library/h9wz3dez(VS.85).aspx

最好的问候,
约翰·施滕伯格

Best regards,
Johan Stenberg


这篇关于如果字符串是不可变的,那我怎么修剪它们呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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