如何存储字符串? [英] How strings are stored ?

查看:97
本文介绍了如何存储字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




1 - 如果我写了一个类似字符串str =" Hello"的代码,它是如何存储在

的内存中的?它会创建一个数组并将H放入其中。在第0位,所以

on。 ?


2 - 有没有办法在.NET中调整数组大小?我想这不是

可能这样做。


谢谢

解决方案

6月4日下午4:08,Navaneeth.KN

< Navaneet ... @ discussion.microsoft.comwrote:


1 - 如果我写了一个类似字符串str =Hello的代码,它是如何存储在

的内存中的?它会创建一个数组并将H放入其中。在第0位,所以

on。 ?



它没有创建一个单独的数组对象 - 字符串数据存储在字符串本身内的
。 AFAIK,数组和字符串是唯一的静态不知道的.NET

类型。

注意字符串文字是实习的,所以如果你有Hello,那么br />
其他地方将使用相同的字符串引用。


2 - 有没有办法在.NET中调整数组大小?我想这可能不是这样的。



不。您必须创建一个正确大小的新数组并复制

数据。 IIRC,Array中有一些方法可以让你更容易。


Jon


6月4日,11:08 * am, Navaneeth.KN

< Navaneet ... @ discussion.microsoft.comwrote:





1 - 如果我写一个类似字符串str =" Hello"的代码,它是如何存储在

的内存中的?它会创建一个数组并将H放入其中。在第0位,所以

on。 ?



简而言之,它不是一个数组。实际上你不需要知道

确切地说,你可以使用数组来访问字符串的每个元素

如符号:myString [1]。


为了更深入地解释每个字符串存储在内存中的位置,请参阅

a查看Skeet文章 http://www.yoda.arachsys.com/csharp/strings.html


2 - 有没有办法在.NET中调整数组大小?我想这可能不是这样的。



NO


谢谢


" Navaneeth.K.N"写道:





1 - 如果我写一个像字符串str =" Hello"的代码,怎么样是存储在内存中的?b $ b?它会创建一个数组并将H放入其中。在第0位,所以

on。 ?



最有可能的,但是那个底层数组是System.String对象的一部分

,你真的无法访问,也没有保证这是

数据表示是什么。重要的是System.String公开了一个索引器operator []。采用从零开始的整数并返回

对应的字符。


>

2 - 是有没有办法在.NET中调整数组大小?我想这可能不是这样的。



不是直接的,不能超过你用C / C ++中的数组做的那样。

也就是说,你可以分配一个具有所需新大小的新数组,将旧数组中的元素复制到新数组,然后将引用存储到

新数组中的任何变量引用原来的,基于对象的集合和新的基于Generic的
集合都支持基本上可扩展的数组(ArrayList或
$ b)。旧的。


$ b List< T>)。


在字符串的特殊情况下,请参阅StringBuilder类。


问候,

- TB


Hi,

1 - If I write a code like string str = "Hello", how it is getting stored in
the memory ? Will it create an array and put the "H" in 0th location, and so
on. ?

2 - Is there any way to resize an array size in .NET ? I guess it''s not
possible to do that.

Thanks

解决方案

On Jun 4, 4:08 pm, Navaneeth.K.N
<Navaneet...@discussions.microsoft.comwrote:

1 - If I write a code like string str = "Hello", how it is getting stored in
the memory ? Will it create an array and put the "H" in 0th location, and so
on. ?

It doesn''t create a separate array object - the string data is stored
within the string itself. AFAIK, arrays and strings are the only .NET
types whose size is not known statically.
Note that string literals are interned, so if you have "Hello"
elsewhere that will use the same string reference.

2 - Is there any way to resize an array size in .NET ? I guess it''s not
possible to do that.

Nope. You have to create a new array of the right size and copy the
data. IIRC, there are methods in Array which make that easier.

Jon


On Jun 4, 11:08*am, Navaneeth.K.N
<Navaneet...@discussions.microsoft.comwrote:

Hi,

1 - If I write a code like string str = "Hello", how it is getting stored in
the memory ? Will it create an array and put the "H" in 0th location, and so
on. ?

In short no, it''s not an array. In reality you do not need to know
that exactly, you can access each element of a string using a array
like notation: myString[1].

For a deeper explanation of where in memory each string is stored take
a look at Skeet article at http://www.yoda.arachsys.com/csharp/strings.html

2 - Is there any way to resize an array size in .NET ? I guess it''s not
possible to do that.

NO

Thanks


"Navaneeth.K.N" wrote:

Hi,

1 - If I write a code like string str = "Hello", how it is getting stored in
the memory ? Will it create an array and put the "H" in 0th location, and so
on. ?

Most likely, but that underlying array is part of the System.String object
and not really accessible to you, nor is there any guarantee that this is
what the data representation is. What matters is that System.String exposes
an indexer "operator []" that takes a zero-based integer and returns the
corresponding character.

>
2 - Is there any way to resize an array size in .NET ? I guess it''s not
possible to do that.

Not directly, no more so than you could do that with an array in C/C++.
That is, you can allocate a new array with the desired new size, copy the
elements from the old array to the new, and then store the reference to the
new array in whatever variable was referencing the old.

Both the original, object based collections and the new Generic based
collections support what are essentially growable arrays (ArrayList or
List<T>).

In the particular case of strings, see the StringBuilder class.

Regards,
-- TB


这篇关于如何存储字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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