?
Solved

How do you convert an array element that is a string to another array in C#

Posted on 2007-10-15
11
Medium Priority
?
181 Views
Last Modified: 2010-04-15
Hi,
I'm having a little trouble with arrays in C#

I have an array of strings but some of the elements may need to be converted to arrays.

Pseudo code:

arr[0] = "some string";
arr[1] = "some pipe | delimited string";
arr[1] = arr[1].Split( ("|").ToCharArray() );

so this would turn arr[1] into another string array within that array.

Is this possible as i will not always know ahead of time which elements need to be turned into an array?

Thanks for the help!
0
Comment
Question by:amagondes
  • 5
  • 4
  • 2
11 Comments
 
LVL 7

Expert Comment

by:lucky_james
ID: 20078553
put a check on whether the string in that array contains that delimiter or not.....

can be modified as :

for(int i =0 ; i<arr.Count ; i++)
{
    if(arr.Contains("|"))
     {
              arr[i] = arr[i].Split( ("|").ToCharArray() );
     }
}

Hope this helps.
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20078589
plz let me know if you have any doubt in above comment.......
0
 

Author Comment

by:amagondes
ID: 20078656
hi lucky_james,
when i try to do something like this:

// arrPID[5] contains a string
arrPID[5] = arrPID[5].Split(("^").ToCharArray());
or
arrPID[5] = arrPID[5].ToString().Split(("^").ToCharArray());

i get the following error:
Cannot implicitly convert type 'string[]' to 'string'

Thanks for your help
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 22

Expert Comment

by:JimBrandley
ID: 20078676
What you are trying to do cannot be done. An array of strings contains strings, not string arrays. It would help if you explain what you need to do with the strings when all is done.

Jim
0
 

Author Comment

by:amagondes
ID: 20078683
so basically what i want to end up with is something like this:

string[] myArray;
myArray[0] = "Some string";
myArray[1] = another array;
myArray[2] = "maybe a string or maybe an array";

thanks again
0
 

Author Comment

by:amagondes
ID: 20078697
Jim,
Thanks for clearing that up. I was not sure if it was possible
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20078730
sorry, i accept it may not be possible that way.
How about this way :
split it into one string array and then inserting into the original array. Of course, this would amount the additional overhead but i feel this can be done.
0
 
LVL 22

Accepted Solution

by:
JimBrandley earned 2000 total points
ID: 20078732
One possible way to attack this is to use an ArrayList. As
ArrayList stringsOrArrays = new ArrayList(10);
stringsOrArrays.Add("Some string");
stringsOrArrays.Add(another array);
stringsOrArrays.Add("maybe a string or maybe an array");

Then when you want to read them,
string stringResult = string.Empty;
string[] resultArray = null;

stringResult = stringsOrArrays[i] as string;
if (stringResult == null)
   resultArray = stringsOrArrays[i] as string[];

One of those two will be non-null.

Jim
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20078879
ArrayList arr= new ArrayList(10);

.....

string[] strResult= null;
for(int i =0 ; i<arr.Count ; i++)
{
    if(arr.Contains("|"))
     {
              strResult = arr[i].Split( ("|").ToCharArray() );
              for(int j = 0; j < strResult.GetLength(0); j++)        
              {
                        string strTemp = strResult[j].ToString();
                       arr.Add(strTemp);
              }
     }
}

0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20078888
<<<<<<<<<    if(arr.Contains("|"))
Please replace with
    if(arr[i].Contains("|"))

Hope this helps
0
 

Author Comment

by:amagondes
ID: 20079069
Hi Jim and lucky_james,
Thanks for all of your help. Jim your answer is just what i was looking for so i will award the points now.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month15 days, 20 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question