.NET Core 2.1 string.Create

huangapple go评论66阅读模式
英文:

.NET Core 2.1 string.Create

问题

I use string.Create method to create a new string like this:

var rawStr = "raw str";
var newStr = string.Create(rawStr.Length, rawStr,
                            (chars, str) =>
                            {
                                chars = str.ToCharArray();
                            });

but, the result newStr just an empty char array.

I saw an answer here, and modify my code:

var rawStr = "raw str";
var newStr = string.Create(rawStr.Length, rawStr.ToCharArray(),
                            (chars, str) =>
                            {
                                //chars = str.ToCharArray();
                                for (int i = 0; i < chars.Length; i++)
                                {
                                    chars[i] = str[i];
                                }
                            });

Then, newStr's value is raw str, this is why?

英文:

I use string.Create method to create a new string like this:

var rawStr = &quot;raw str&quot;;
var newStr = string.Create(rawStr.Length, rawStr,
                            (chars, str) =&gt;
                            {
                                chars = str.ToCharArray();
                            });

but, the result newStr just an empty char array.

I saw an answer here, and modify my code:

var rawStr = &quot;raw str&quot;;
var newStr = string.Create(rawStr.Length, rawStr.ToCharArray(),
                            (chars, str) =&gt;
                            {
                                //chars = str.ToCharArray();
                                for (int i = 0; i &lt; chars.Length; i++)
                                {
                                    chars[i] = str[i];
                                }
                            });

Then, newStr's value is raw str, this is why?

答案1

得分: 1

"String.Create documentation"(字符串创建文档)

创建一个具有特定长度的新字符串,并在创建后使用指定的回调函数进行初始化。

使用create函数,你可以实现一种转换。在其最简单的形式中,它只需获取一个chars数组并创建一个字符串。

让我们来解析这个函数:

string.Create<TType>(newStrLength, typedObject, creationFunction);

TType - 输入类型(在你的情况下为字符串),将被转换或用于创建新字符串。

newStrLength - 你需要提供新字符串的长度。

typedObject - 类型为TType的对象,将传递给创建函数。

creationFunction - 一个 lambda 函数,根据字符和TType缓冲区执行某些操作。Create会调用这个函数。字符由Create提供,它们是新字符串的字符,你可以根据需要进行修改。

在你的情况下,你的创建函数逐个从字符串中获取字符,并将它们映射到一个新字符串,从而有效地创建了一个副本。

在你的第一次尝试中,发生了以下情况:

chars数组具有一个引用,该引用被ToCharArray返回的新数组所替代。因此,通过这个赋值,你不再引用将用于创建字符串的字符。原始数组保持不变。

在第二次尝试中,你改变了原始数组的值,因此新字符串使用它。

英文:

I'll try to explain what you are doing:

String.Create documentation

> Creates a new string with a specific length and initializes it after creation by using the specified callback.

What you can achieve with the create, is a kind of conversion. In it's simplest form, it just takes an array of chars and creates as string.

Let's break down the function:

string.Create&lt;TType&gt;( newStrLength, typedOnject, creationFunction);

TType - It's the input type (in your case as string) that will be converted or used to create the new string

newStrLength - You need to provide the new string length

typedOnject - Object of type TType that will be given to the creation function

creationFunction - A lamda function that will do something based on the characters and the TType buffer. Create is calling this function. The chars are being provided by the Create and they are the new string's chars to modify as you please.

In your case, you creation function gets one by one the characters from the string and maps them to a new string, creating effectively a copy of one.

In your first attempt the following happens:

The chars array has a reference that is replaced by your new array that the ToCharArray returns. So by this assignment you are no longer referencing the characters that will be used to create the string. The original array remains unchanged.

In the second attempt you are changing the values of the original array and thus the new string uses it.

答案2

得分: 0

这种复杂性是不必要的。使用这种语法:

var rawStr = "raw str";
var newStr = rawStr;
英文:

There is no need in this complexity. Use this syntax:

var rawStr = &quot;raw str&quot;;
var newStr = rawStr;

huangapple
  • 本文由 发表于 2020年1月4日 12:39:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/59587941.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定