Click here to Skip to main content
15,905,785 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi
I have declared 3 string arrays
string[] Arr1 = new string[] { "a", "b", "c", "", ""};
string[] Arr2 = new string[] { "", "", "", "1", "2" };
string[] Arr3 = new string[5];


i want the desired output into Arr3 as
"a","1","b","2","c".

Any Help or suggestion would be accepted and appreciated.

Regards,
Pawan.
Posted
Updated 23-Sep-10 18:35pm
v4
Comments
Hiren solanki 23-Sep-10 10:20am    
is the size of arr1 and arr2 are fixed ?
Yusuf 23-Sep-10 18:47pm    
what do you mean by it won't compile? what error do you get?
Pawan Kiran 24-Sep-10 0:12am    
Hi Hiren,

The Size of Arr1,Arr2,Arr3 are Same.
Hiren solanki 24-Sep-10 2:56am    
see my below answered effort, this may be your solution
Hiren solanki 24-Sep-10 4:55am    
you can vote to my answer and mark it as accepted if it helped you.

Hi Pawan,

Here is my effort of 1 hour.
I tried my best to accomplish to your tricky problem.

Here is my code,
C#
int arr1Counter = -1;
            int arr2Counter = -1;
            for (int i = 0; i < Arr3.Length; i = i + 2)
            {
                arr1Counter++;
                arr2Counter++;
                if (arr1Counter < 5)
                {
                    while (Arr1[arr1Counter].ToString() == String.Empty)
                    {
                        arr1Counter++;
                    }
                }
                Arr3[i] = Arr1[arr1Counter];
                if (arr2Counter < 5)
                {
                    while (Arr2[arr2Counter].ToString() == String.Empty)
                    {
                        arr2Counter++;
                    }
                }
                if (i < 4)
                    Arr3[i + 1] = Arr2[arr2Counter];
            }

Please vote and Mark as Answer if Helped.
 
Share this answer
 
Comments
AspDotNetDev 24-Sep-10 3:09am    
You spent an hour converting my pseudo code to C#? By the way, there are a couple errors and inefficiencies in this code (e.g., what happens if the second array contains only empty strings?).
Hiren solanki 24-Sep-10 3:18am    
nope, I dont know what VB is so didn't looked to your psudo. but for the errors
I have assumed there will be three non empty chars in Arr1 and two non empty chars in Arr2.
Pawan Kiran 24-Sep-10 4:52am    
Thank you so much Mr.Hiren.
A little bit of code change has been done, and it solves My Problem.

Once again Thanks for your efforts.
AspDotNetDev 24-Sep-10 13:31pm    
Lol, it's pseudo-code, not VB.
Hiren solanki 24-Sep-10 22:55pm    
But looks like VB. Because it using WHILE .... END WHILE
See here[^] for all the details of the System::String class and its member functions.
 
Share this answer
 
Your question is not clear (your example has become to vague).

The answer to your problem could be as simple as referencing Arr1 by the index 0 and referencing Arr2 by index 3 all hard coded.

I assume this is not acceptable though (again you are not being clear enough).

Another solution could be to ToList your arrays and then use Linq to query the non empty strings. Then toggle between your Arr1 and Arr2 filling Arr3.
 
Share this answer
 
Comments
Pawan Kiran 24-Sep-10 0:23am    
Array size is not static, it is generated at runtime only, so i can't hard code it.

Clear Description about my problem:

I have a panel in that i am Adding 9 Combo box Dynamically, in that 5 Combo's Having Same Values and Other 4 combo's Will have (And,Or) Items in it.
i want to make a string which is a combination of
combo[0].Text + combo[5].Text + combo[1].Text + combo[6].Text +
combo[2].Text + combo[7].Text + combo[3].Text + combo[8].Text +
combo[4].Text

For assumption only i said 9 combo box, it vary's at runtime.
Here is some pseudo-code:
WHILE Array3 NOT FULL
	WHILE Array1 String Empty
		Next String
	END WHILE
	Add String to Array3
	WHILE Array2 String Empty
		Next String
	END WHILE
	Add String to Array3
END WHILE
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900