将字符串数组元素复制到C#中的字符串 [英] copy string array elements to strings in C#

查看:97
本文介绍了将字符串数组元素复制到C#中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有以下代码;

string test1 = words [2]; //" Exclud"

string test2 =" Exclud";

string test3 = String.Copy(words [2]); //" Exclud"


bool booTest1 = test1.Equals(test2); // false

bool booTest2 = test2.Equals(" Exclud"); // true

bool booTest3 = test1.Equals( " Exclud"); // false

bool booTest4 = words [0] .Equals(" Exclud"); // false

bool booTest5 = test3.Equals (Exclud); // false

bool booTest6 = test1.Equals(test1); // true


尽管test1 =Exclud这是从字符串arrray单词复制[],

比较时显示test1!=" Exclud"


有人可以帮我这个?


***通过开发人员指南 http:// www发送.developersdex.com ***



I have the following code;
string test1 = words[2];//"Exclud"
string test2 ="Exclud";
string test3 = String.Copy(words[2]);//"Exclud"

bool booTest1 = test1.Equals(test2);//false
bool booTest2 = test2.Equals("Exclud");//true
bool booTest3 = test1.Equals("Exclud");//false
bool booTest4 = words[0].Equals("Exclud");//false
bool booTest5 = test3.Equals("Exclud");//false
bool booTest6 = test1.Equals(test1); //true

Though test1 = "Exclud" which is copied from string arrray words[],
when compared it shows that test1 != "Exclud"

Can some one please help me with this?

*** Sent via Developersdex http://www.developersdex.com ***

推荐答案

我尝试了下面给出的代码&为所有

比较打印True

string [] words = {" Exclud"," Exclud"," Exclud","排除};


string test1 = words [2]; //" Exclud"

string test2 =" Exclud";

string test3 = String.Copy(words [2]); //" Exclud"

bool booTest1 = test1.Equals(test2); // false

Console.Write(booTest1);

bool booTest2 = test2.Equals(" Exclud"); // true

Console.Write(booTest2 );

bool booTest3 = test1.Equals(" Exclud"); // false

Console.Write(booTest3);

bool booTest4 = words [0] .Equals(" Exclud"); // false

Console.Write(booTest4);

bool booTest5 = test3.Equals(" Exclud); // false

Console.Write(booTest5);

bool booTest6 = test1.Equals(test1); // true

Console.Write(booTest6);

I tried this code given below & is printing True for all the
comparisions

string[] words = {"Exclud","Exclud","Exclud","Exclud"};

string test1 = words[2];//"Exclud"
string test2 ="Exclud";
string test3 = String.Copy(words[2]);//"Exclud"

bool booTest1 = test1.Equals(test2);//false
Console.Write(booTest1);
bool booTest2 = test2.Equals("Exclud");//true
Console.Write(booTest2);
bool booTest3 = test1.Equals("Exclud");//false
Console.Write(booTest3);
bool booTest4 = words[0].Equals("Exclud");//false
Console.Write(booTest4);
bool booTest5 = test3.Equals("Exclud");//false
Console.Write(booTest5);
bool booTest6 = test1.Equals(test1); //true
Console.Write(booTest6);


我尝试了下面给出的代码&为所有

比较打印True

string [] words = {" Exclud"," Exclud"," Exclud","排除};


string test1 = words [2]; //" Exclud"

string test2 =" Exclud";

string test3 = String.Copy(words [2]); //" Exclud"

bool booTest1 = test1.Equals(test2); // false

Console.Write(booTest1);

bool booTest2 = test2.Equals(" Exclud"); // true

Console.Write(booTest2 );

bool booTest3 = test1.Equals(" Exclud"); // false

Console.Write(booTest3);

bool booTest4 = words [0] .Equals(" Exclud"); // false

Console.Write(booTest4);

bool booTest5 = test3.Equals(" Exclud); // false

Console.Write(booTest5);

bool booTest6 = test1.Equals(test1); // true

Console.Write(booTest6);

I tried this code given below & is printing True for all the
comparisions

string[] words = {"Exclud","Exclud","Exclud","Exclud"};

string test1 = words[2];//"Exclud"
string test2 ="Exclud";
string test3 = String.Copy(words[2]);//"Exclud"

bool booTest1 = test1.Equals(test2);//false
Console.Write(booTest1);
bool booTest2 = test2.Equals("Exclud");//true
Console.Write(booTest2);
bool booTest3 = test1.Equals("Exclud");//false
Console.Write(booTest3);
bool booTest4 = words[0].Equals("Exclud");//false
Console.Write(booTest4);
bool booTest5 = test3.Equals("Exclud");//false
Console.Write(booTest5);
bool booTest6 = test1.Equals(test1); //true
Console.Write(booTest6);


考虑到这一点:


string s1 =" abcdef";

string s2 =" abc";

s2 + =" def";

Console.WriteLine(" s1 = s2?{0}",s1.Equals(s2));

Console.WriteLine(" ref(s1)= ref(s2)?{ 0}",object.ReferenceEquals(s1,s2));


并尝试用以下内容更改s2的初始化:


string s2 =" abcdef";


如果您好奇,请尝试使用ildasm打开您的程序集。


------ -------

解释(好吧,我会尝试):


这都是引用类型和不可变类型的问题。 String是一个

不可变引用类型。在分配abcdef时到s1,编译器将

创建一个本地字符串,该引用将被分配给s1。然后,如果

另一个字符串需要设置为abcdef,相同的引用将是

重用以节省空间。


但是,当设置第一个abc时然后def到s2,两个字符串是

在本地创建,因此有两个引用。


你的数组问题是一样的。我可以合理地假设那个eranga'的

单词数组来自其他地方,但是sumit用本地创建了一个本地数组

引用


我希望它很清楚这有帮助


Fabien

" sumit" <苏********** @ gmail.com> écritdansle message de news:
11**********************@f14g2000cwb.googlegroups。 com ...
Consider this to :

string s1 = "abcdef";
string s2 = "abc";
s2 += "def";
Console.WriteLine("s1 = s2 ? {0}", s1.Equals(s2));
Console.WriteLine("ref(s1) = ref(s2) ? {0}", object.ReferenceEquals(s1,s2));

and try to change the initialisation of s2 with :

string s2 = "abcdef";

If you are curious, try to open your assembly with ildasm.

-------------
Explanation (well I''ll try) :

It''s all a matter of reference type and immutable type. String are an
immutable reference type. When assigning "abcdef" to s1, the compiler will
create a local string which reference will be assigned to s1. Then, if
another string needs to be set to "abcdef", the same reference will be
reused to save space.

However, when setting first "abc" and then "def" to s2, two strings are
created locally and thus two references.

Your problems with array are the same.I can reasonably suppose that eranga''s
words array comes from elsewhere but sumit created a local array with local
references

I hope it''s clear and that helps

Fabien
"sumit" <su**********@gmail.com> a écrit dans le message de news:
11**********************@f14g2000cwb.googlegroups. com...
我尝试了下面给出的代码&为所有
比较打印True

string [] words = {" Exclud",Exclud,Exclud,Exclud};

string test1 = words [2]; //" Exclud"
string test2 =" Exclud";
string test3 = String.Copy(words [2]); //" ;排除

bool booTest1 = test1.Equals(test2); // false
Console.Write(booTest1);
bool booTest2 = test2.Equals(" Exclud" ); // true
Console.Write(booTest2);
bool booTest3 = test1.Equals(" Exclud"); // false
Console.Write(booTest3);
bool booTest4 = words [0] .Equals(" Exclud"); // false
Console.Write(booTest4);
bool booTest5 = test3.Equals(" Exclud"); // false
Console.Write(booTest5);
bool booTest6 = test1.Equals(test1); // true
Console.Write(booTest6);
I tried this code given below & is printing True for all the
comparisions

string[] words = {"Exclud","Exclud","Exclud","Exclud"};

string test1 = words[2];//"Exclud"
string test2 ="Exclud";
string test3 = String.Copy(words[2]);//"Exclud"

bool booTest1 = test1.Equals(test2);//false
Console.Write(booTest1);
bool booTest2 = test2.Equals("Exclud");//true
Console.Write(booTest2);
bool booTest3 = test1.Equals("Exclud");//false
Console.Write(booTest3);
bool booTest4 = words[0].Equals("Exclud");//false
Console.Write(booTest4);
bool booTest5 = test3.Equals("Exclud");//false
Console.Write(booTest5);
bool booTest6 = test1.Equals(test1); //true
Console.Write(booTest6);



这篇关于将字符串数组元素复制到C#中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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