Concatenating 2 string arrays
-
Hi, First of all, I'm new on CodeProject, so quick introducing: my names is Matthieu, I'm 26yo and live in France. I start learning C# and I get stuck at the (easy?) following: I have 2 string arrays, and I want to copy the first at the end of the second. I used Array.CopyTo method but it fails because the destination array is not large enough. How can I increase the size of the dest string array ? Thanks a lot for help, Matthieu
-
Hi, First of all, I'm new on CodeProject, so quick introducing: my names is Matthieu, I'm 26yo and live in France. I start learning C# and I get stuck at the (easy?) following: I have 2 string arrays, and I want to copy the first at the end of the second. I used Array.CopyTo method but it fails because the destination array is not large enough. How can I increase the size of the dest string array ? Thanks a lot for help, Matthieu
-
Use Concat method of string. s1= "Hello "; s2 = "World" s1 = s1.Concat(string2) //s1 = Hello World sorry for my bad English.
Thanks Itanium, but that's not what I want to do. s1[0]="hello" s1[1]="world" s2[0]="good" s2[1]="luck" and I want to create a new string array like this: s1[0]="hello" s1[1]="world" s1[2]="good" s1[3]="luck" Thanks a lot for help Matthieu
-
Hi, First of all, I'm new on CodeProject, so quick introducing: my names is Matthieu, I'm 26yo and live in France. I start learning C# and I get stuck at the (easy?) following: I have 2 string arrays, and I want to copy the first at the end of the second. I used Array.CopyTo method but it fails because the destination array is not large enough. How can I increase the size of the dest string array ? Thanks a lot for help, Matthieu
you cannot re-dimension an array in C# like you could in VB with redim. You'll need to do something like:
//Two arrays string[] arrayOne = new string[]{"Hello", "World"}; string[] arrayTwo = new string[]{"Have", "a", "nice", "day"}; //Length of both together int newArrayLength = arrayOne.Length + arrayTwo.Length; //Set first to variable string[] arrayThree = arrayOne; //resize first to sum of lengths arrayOne = new String[newArrayLength]; //Add values from first (now held in temp variable) int iterator = 0; foreach(string s in arrayThree) { arrayOne[iterator] = s; iterator++; } //Add values from second foreach(string s in arrayTwo) { arrayOne[iterator] = s; iterator++; }
I'm sure theres a more diginfied way, but it does work!!!
-
Hi, First of all, I'm new on CodeProject, so quick introducing: my names is Matthieu, I'm 26yo and live in France. I start learning C# and I get stuck at the (easy?) following: I have 2 string arrays, and I want to copy the first at the end of the second. I used Array.CopyTo method but it fails because the destination array is not large enough. How can I increase the size of the dest string array ? Thanks a lot for help, Matthieu
You've got two options here: 1. Create a new array of length= array1.length + array2.length, then use two consecutive Array.CopyTo() calls, one using zero as the index to start at, one using array1.length. 2. If you don't mind the minor performance hit from using a ICollection based object, take a look at System.Collections.ArrayList:
using System.Collections;
// stuff omitted
ArrayList array1 = new ArrayList();
array1.Add("hello");
array1.Add("world");
ArrayList array2 = new ArrayList();
array2.Add("Bonjour");
array2.Add("le monde");
array1.AddRange( array2 );now array1 should contain (boxed as objects):
"hello",
"world",
"Bonjour",
"le monde"Hope that helps... Jeremy Kimball
-
You've got two options here: 1. Create a new array of length= array1.length + array2.length, then use two consecutive Array.CopyTo() calls, one using zero as the index to start at, one using array1.length. 2. If you don't mind the minor performance hit from using a ICollection based object, take a look at System.Collections.ArrayList:
using System.Collections;
// stuff omitted
ArrayList array1 = new ArrayList();
array1.Add("hello");
array1.Add("world");
ArrayList array2 = new ArrayList();
array2.Add("Bonjour");
array2.Add("le monde");
array1.AddRange( array2 );now array1 should contain (boxed as objects):
"hello",
"world",
"Bonjour",
"le monde"Hope that helps... Jeremy Kimball
I don't think that boxing takes place on strings, does it? Forgive me if I'm being dumb, as I have way more experience in Java than in .NET, but I believe that all strings are objects, aren't they? Class String extends/inherits from Object, not ValueType. Also, the relative performance hit of using an ArrayList is not so minor in this case. I think that people should always program with performance in mind. Still, it's always nice to show people how to do things in different ways. Regards, Jeff Varszegi