473,379 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

copy string array elements to strings in C#



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 ***
Nov 17 '05 #1
10 5239
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);

Nov 17 '05 #2
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);

Nov 17 '05 #3
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...
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);

Nov 17 '05 #4
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...
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);

Nov 17 '05 #5
Eranga <km***@yahoo.com> wrote:
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?


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

It looks to me like neither words[0] nor words[2] is actually equal to
"Exclud", which would explain all your observations.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
Eranga <km***@yahoo.com> wrote:
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?


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

It looks to me like neither words[0] nor words[2] is actually equal to
"Exclud", which would explain all your observations.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7
<"Fabien Bezagu" <fbezagu_at_novacor_dot_fr>> wrote:

<snip>
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 can't see how that would change anything. Nowhere in the OP's code or
in sumit's code is reference equality used. All tests are explicitly
using the Equals method.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
<"Fabien Bezagu" <fbezagu_at_novacor_dot_fr>> wrote:

<snip>
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 can't see how that would change anything. Nowhere in the OP's code or
in sumit's code is reference equality used. All tests are explicitly
using the Equals method.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #9
Yes, you're right. I answered too fast. I'm sorry.

Fabien

"Jon Skeet [C# MVP]" <sk***@pobox.com> a écrit dans le message de news:
MP************************@msnews.microsoft.com...
<"Fabien Bezagu" <fbezagu_at_novacor_dot_fr>> wrote:

<snip>
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 can't see how that would change anything. Nowhere in the OP's code or
in sumit's code is reference equality used. All tests are explicitly
using the Equals method.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #10
Yes, you're right. I answered too fast. I'm sorry.

Fabien

"Jon Skeet [C# MVP]" <sk***@pobox.com> a écrit dans le message de news:
MP************************@msnews.microsoft.com...
<"Fabien Bezagu" <fbezagu_at_novacor_dot_fr>> wrote:

<snip>
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 can't see how that would change anything. Nowhere in the OP's code or
in sumit's code is reference equality used. All tests are explicitly
using the Equals method.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matt Garman | last post by:
What is the "best" way to copy a vector of strings to an array of character strings? By "best", I mean most elegantly/tersely written, but without any sacrifice in performance. I'm writing an...
4
by: Venkat | last post by:
Hi All, I need to copy strings from a single dimensional array to a double dimensional array. Here is my program. #include <stdio.h> #include <stdlib.h>
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
3
by: Leo Nunez | last post by:
Hello! I need copy from structure "A" to "B" that contains "strings" in a one line code. Me problem like this : typedef struct tHeader{ char field1; char field2; char field3;
5
by: Paulers | last post by:
Hello all, I have a string array with duplicate elements. I need to create a new string array containing only the unique elements. Is there an easy way to do this? I have tried looping through...
5
by: tshad | last post by:
How do you easily make a copy of an arraylist? If you do: arrayList2 = arrayList1 You get a pointer so that if you clear arrayList2 (arrayList2.Clear) - arrayList1 is also cleared. I...
14
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
5
by: Fred | last post by:
Hi: I've got the following request: (suppose the required head file is included) vector<stringvec; // .......... // push some items into vec string str; // here I want to copy some range...
4
by: kungfuelmosan | last post by:
Hey guys, Im just getting into c++ at the moment so please bare with me Basically i need to declare a vector<stringstringArray(50) inside a class, however by doing so i am getting the following...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.