Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Concatenating 2 string arrays

Concatenating 2 string arrays

Scheduled Pinned Locked Moved C#
helpquestioncsharpdata-structureslearning
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Matthieu C
    wrote on last edited by
    #1

    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

    I D J 3 Replies Last reply
    0
    • M Matthieu C

      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

      I Offline
      I Offline
      Itanium
      wrote on last edited by
      #2

      Use Concat method of string. s1= "Hello "; s2 = "World" s1 = s1.Concat(string2) //s1 = Hello World sorry for my bad English.

      M 1 Reply Last reply
      0
      • I Itanium

        Use Concat method of string. s1= "Hello "; s2 = "World" s1 = s1.Concat(string2) //s1 = Hello World sorry for my bad English.

        M Offline
        M Offline
        Matthieu C
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • M Matthieu C

          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

          D Offline
          D Offline
          Donald_a
          wrote on last edited by
          #4

          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!!!

          1 Reply Last reply
          0
          • M Matthieu C

            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

            J Offline
            J Offline
            Jeremy Kimball
            wrote on last edited by
            #5

            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

            J 1 Reply Last reply
            0
            • J 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

              J Offline
              J Offline
              Jeff Varszegi
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups