IT Community - Software Programming, Web Development and Technical Support

Compare a string with String Array.

This is a discussion on Compare a string with String Array. within the C# Programming forums, part of the Software Development category; HI, I have a string array like this. string[] val = new string[4] {"a", "b", "...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 12-28-2007, 09:57 PM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Default Compare a string with String Array.

HI,

I have a string array like this.

string[] val = new string[4] {"a", "b", "b", "c"};
This is a sample but this array contains more values. I want to remove duplicates from this array. Can any one help me to do this?
__________________
The OXYGEN
Delivers edgy, intelligent Technology to all...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-28-2007, 09:59 PM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Compare a string with String Array.

Hi,

Hope this helps.

private string[] MatchStrings(string[] strings)
{
List<string> list = new List<string>();
for (int i = 0; i < strings.Length; i++)
{
if(!list.Contains(strings[i]))
{
list.Add(strings[i]);
}
}
string[] retStrings = new string[list.Count];
for (int i = 0; i < list.Count; i++)
{
retStrings[i] = list[i];
}
return retStrings;
}
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-28-2007, 10:58 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Compare a string with String Array.

I have seen that the term “Immutable” is not very clear.
In brief, Immutable means, once a string is created, its content cannot be changed. This definition brings to confusion tome, that string value can be changed as well. Just see the example below,


string s = ”Hello World”;
s = “Hello World Updated”;



If you check the current value of s, you will see string s holds “Hello World Updated”.
Then what is the reason behind calling System.String immutable.
Slightly changing the definition – once a string is created it allocates to a certain address of the memory. Now, each time, string content is changed, the address it holds lost and a new address is allocated.
This is actually why string is said to be Immutable.
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-28-2007, 10:59 PM
bluesky bluesky is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 201
bluesky is on a distinguished road
Default Re: Compare a string with String Array.

hi shaalini,
I am giving you a simple example,


string s1="Hello World";
string s2=s1;
bool blTest;

blTest=s1.Equals(s2);
Console.WriteLine(blTest.ToString()); //---> True

s2="My World";
blTest=s1.Equals(s2);
Console.WriteLine(blTest.ToString()); //---> False

//Now check the value of s1
Console.WriteLine(s1.ToString()); //--->Hello World


string s1 holds the value “Hello World” and after assigning s1 to s2, s2 also holds the same value as well points to the same location in the memory. That is why the 1st
equality test outputs True. Now changing the value of s1, as it points to a new location i.e. the original address location lost, the 2nd equality test outputs False.
In the last line, s1 value is tested and it holds the same data it has been initialized.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 12-28-2007, 11:16 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Compare a string with String Array.

Hi,
Can u explain the following IComparable, IConvertible, IClonable, IEnumerable interface
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 12-28-2007, 11:18 PM
bluesky bluesky is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 201
bluesky is on a distinguished road
Default Re: Compare a string with String Array.

IComparable Interface
Compares the current instance with another object of the same type and returns the sort order of the current instance compared to the specified object.



Any negative number denotes the current instance is lexicographically less than the value.
Zero denotes the current instance is lexicographically == value.
Any positive number denotes the current instance is lexicographically greater than the value, or value is a null reference.


string s1="Hello";
string s2="HELLO";
int i=s1.CompareTo(s2);
if(i==0)
{
Console.WriteLine("Same Instance");
}
else
{
Console.WriteLine("Different Instance");
}


Output:
Different Instance

IConvertible Interface
Converts the value of an instance to other data type under Common Type System that has an equivalent value. The types that falls under CTS is Byte, Sbyte, Boolean, Int16,
Int32, Int64, Single, Double, Decimal, Char, DateTime, String etc.
The common language runtime typically exposes the IConvertible interface through the Convert class.


string s1="12345";
int i;
i=Convert.ToInt16(s1);
Console.WriteLine(i.ToString());


Output:
12345

IClonable Interface
Creates a new object that is a copy of the current instance.


string s1="HELLO";
string s2;
s2=(string)s1.Clone();
Console.WriteLine(s2);


Output:
HELLO

IEnumerable Interface
Returns an enumerator that can iterate through a collection.


string s1="HELLO";
CharEnumerator cm;
cm=s1.GetEnumerator();
while(cm.MoveNext())
{
Console.WriteLine(cm.Current.ToString());
}


Output:
H
E
L
L
O
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to compare the string with case sensitive in sql server? krishnakumar Database Support 9 10-10-2008 04:54 AM
What is diffrenece between string.compare and string.compareordinal shaalini ASP and ASP.NET Programming 3 12-28-2007 10:46 PM
How can I compare a date in a string with todays date? itbarota HTML, CSS and Javascript Coding Techniques 2 12-18-2007 11:02 PM
How to pass array of string from vc++ dll to C# ? kingmaker C and C++ Programming 2 08-10-2007 05:35 AM
How to split a string by using a string usually in the c# dotnet... Archer C# Programming 1 07-25-2007 03:26 AM


All times are GMT -7. The time now is 08:18 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0