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", "...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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... |
| Sponsored Links |
| |||
| 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 |
| |||
| 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... |
| |||
| 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. |
| |||
| 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 |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |