2

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?

3
  • What are you trying to achieve? Commented Jan 4, 2020 at 4:48
  • I just want to create a new string , which value equals rawStr's value. Commented Jan 4, 2020 at 4:49
  • 1
    That seems like alot of overhead just to copy a string. Commented Jan 4, 2020 at 7:34

2 Answers 2

1

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<TType>( 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, what puzzling me is in the first piece of code, the newStr's value is an empty char array, I dont's know where's wrong.
The chars array has a reference that is replaced by your new charArray. So by this assignment you are no longer referencing the characters that will be used to create the string. The original array remains unchanged.
0

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

var rawStr = "raw str";
var newStr = rawStr;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.