How to copy a char[] array to a string?  
Author Message
shieldy





PostPosted: Visual C# General, How to copy a char[] array to a string? Top

Hi!

anyone knows Is there a method or function in c# to copy to a char array to a string

I want to implement it like this:

// define somewhere;

string str_A="";

char[] char_B = new char[16];

//value it in otherplace;

char_B="fdfdfd";

str_A=char_B;//error

thankyou!




Visual C#12  
 
 
littleguru





PostPosted: Visual C# General, How to copy a char[] array to a string? Top

You can use the string constructor that takes a char array:

char[] chars = new char[] { 'a', 'b', 'c' };

// Create a new string instance that takes the char array.
string
s = new string(chars);



 
 
shieldy





PostPosted: Visual C# General, How to copy a char[] array to a string? Top

You can use the string constructor that takes a char array:

char[] chars = new char[] { 'a', 'b', 'c' };

// Create a new string instance that takes the char array.
string
s = new string(chars);

thankyou!

but my string s is created in the front, and revalue later.



 
 
Michael Letterle





PostPosted: Visual C# General, How to copy a char[] array to a string? Top

You could use a StringBuilder:

StringBuilder s2 = new StringBuilder();
char[] chars = new char[]{'a', 'b', 'c'};
s2.Insert(0, chars);
s = s2.ToString();



You'll just have to add a reference to System.Text at the top of your code.


 
 
shieldy





PostPosted: Visual C# General, How to copy a char[] array to a string? Top

Hi,Michael Letterle

it works!

thankyou!



 
 
James Curran





PostPosted: Visual C# General, How to copy a char[] array to a string? Top

but my string s is created in the front, and revalue later.

Note that the definition and the initialization are two distinct steps, and while it's usually a good idea to do them in one line, it's not required.

string str_a = "";

is exactly the same as:

string str_a;

str_a = "";

Similarly,

string s = new string(chars);

is the same as:

string s ;

s = new string(chars);

And those two lines can be many lines apart.

In other words, the "new" does nothing magical to the left side of the equals. It just creates a string, on the right side of the equal, which is assigned to the value of the left, just like any other assignment.



 
 
shieldy





PostPosted: Visual C# General, How to copy a char[] array to a string? Top

James Curran

THANKYOU!

actually, what I means is that when the string variable was defined and created.

then in other place, it has to be assigned with chars array, it cann't be created again.

anyway, thankyou!



 
 
James Curran





PostPosted: Visual C# General, How to copy a char[] array to a string? Top

then in other place, it has to be assigned with chars array, it cann't be created again.

You weren't listening....

In other words, the "new" does nothing magical to the left side of the equals. It just creates a string, on the right side of the equal, which is assigned to the value of the left, just like any other assignment.

new string(chars) creates a string object;

string s; declares a string references. It is as "created" as it's ever going to be. It just has not been assigned a value yet.

s = ..... assignes the string represented by.... to s. Where the .... came from is irrevelevant.

string s = ..... declares the reference and assignes the value in one line.

Hence,



string a = new string(chars);
a = new string(chars2);
string b ;

string c;
b= "abcd";
c = b;
b = a;

b = new string(char2);
a = b;
b = c;

is perfectly fine code.



 
 
shieldy





PostPosted: Visual C# General, How to copy a char[] array to a string? Top

Hi, James Curran

thanks for your patient explaination. I understood.

what a good guy. thankyou!