This is a discussion on Type conversion from arraylist to string in C# within the C# Programming forums, part of the Software Development category; Hi everyone, I've been trying to convert from arraylist type to string type, but all my efforts have been ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Hi everyone, I've been trying to convert from arraylist type to string type, but all my efforts have been in vain. Does anyone know how to this? I have already tried the .ToString(), the type converter, etc. but I always get the same result. Here is my code: string CheckingString(string s) { ArrayList vector = new ArrayList(); string result = s; for (int i = 0; i < s.Length; i++) { char x = System.Char.ToLower(sIdea [i]); int j = Convert.ToInt32(x); if ((j >= 47 && j <= 58) || (j >= 65 && j <= 90) || (j >= 97 && j <= 122)) { vector.Add(x); } } result = vector.ToString().Trim(); return result; } results get the value "System.Collections.ArrayList", which is not what I want. I want result to get the value of s modified.
__________________ The OXYGEN Delivers edgy, intelligent Technology to all... |
| Sponsored Links |
| |||
| You can't convert the type ArrayList to a string because an ArrayList is not an Object, it is a Collection( Collection of Objects )- You would have to retrive each string in the ArrayList, like this: <code> ArrayList a = new ArrayList(); string str = ""; foreach( string s in a ) { str = s; } return str; </code> But you have to realize that this will only work if there is only a single string in the arraylist. If there is more than one string, this will return the last string value in the ArrayList. |
| |||
| Thank you for your replies. I finally used this: <code> result = System.String.Concat(vector.ToArray()); </code> First, I convert the arraylist into an array, and then call this system.string which converts it into a string.
__________________ The OXYGEN Delivers edgy, intelligent Technology to all... Last edited by oxygen : 07-30-2007 at 09:21 AM. |
| |||
| hi....Oxy..... I was looking for a way to convert an array my self exept non of the ways explained her could help... So i made my own code: ArrayList lines = new ArrayList(); foreach (string line in File.ReadAllLines(theDir1)) { lines.Add(line); } foreach (string line in File.ReadAllLines(theDir2)) { lines.Add(line); } foreach (string line in File.ReadAllLines(theDir3)) { lines.Add(line); } File.WriteAllText(theDir4, ""); foreach (string line in lines) { temp = line; temp += "\n"; File.AppendAllText(theDir4, temp); } This code fills "array lines" with text from files and then writes the array to file but you could do just about anything with the "string line". I had to transfer the value in "string line" to "string temp" since you would get this error trying to edit line: Cannot assign to 'line' because it is a 'foreach iteration variable'. Im no codewiz but i hope it helps ya. <code> public string ArrayListToString(System.Collections.ArrayList ar) { return ArrayListToString(ar, ','); } public string ArrayListToString(System.Collections.ArrayList ar, char delim) { return ArrayListToString(ar, delim.ToString()); } public string ArrayListToString(System.Collections.ArrayList ar, string delim) { return string.Join(delim, (string []) ar.ToArray(typeof(string))); } </code> |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Which is best Vector or ArrayList? | leoraja8 | Java Programming | 7 | 09-25-2007 12:05 AM |
| How to do a type conversion in VC++ .NET? | bluesky | C and C++ Programming | 2 | 08-10-2007 05:38 AM |
| Difference between Array and ArrayList | leoraja8 | Java Programming | 1 | 05-11-2007 04:06 AM |
| Arraylist | leoraja8 | Java Programming | 1 | 05-11-2007 03:47 AM |
| Convert Object to Arraylist and Arraylist to Object | raja | C# Programming | 0 | 05-08-2007 03:26 AM |