Results 1 to 2 of 2

Thread: Combining two strings

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    9

    Question Combining two strings

    Hi there,
    I have two multi-line strings which I would like to be able to read line by line and then combine so that they alternate. Like this:

    My two strings:

    String 1:
    Code:
    Geoff                      
                                
    Bob
                                
    Jim

    String 2:
    Code:
          
    Steve
    
    Chris
    
    Tom
    And I would like them to be combined in to one string that looks like this:
    Geoff
    Chris
    Bob
    Tom
    Jim
    Steve
    And then each line placed in to a listbox.

    Any help would be much appreciated.

    Thanks in advance,

    H

  2. #2
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: Combining two strings

    Quote Originally Posted by harryjjacobs View Post
    Hi there,
    I have two multi-line strings which I would like to be able to read line by line and then combine so that they alternate. Like this:

    My two strings:

    String 1:
    Code:
    Geoff                      
                                
    Bob
                                
    Jim

    String 2:
    Code:
          
    Steve
    
    Chris
    
    Tom
    And I would like them to be combined in to one string that looks like this:
    Geoff
    Chris
    Bob
    Tom
    Jim
    Steve
    And then each line placed in to a listbox...
    I can't really figure out which order you are using. Also are there always an equal amount of strings (presumeably seperated by a new line) in each string?

    #EDIT: If I'm right in assuming, that you made a typo, and that the names are to be in alternating order, you can try the following (also assuming that the strings are seperated by a single newline):
    vb.net Code:
    1. Private Function join_strings(ByVal names_a As String, ByVal names_b As String) As String
    2.  
    3.         Dim n_a() As String = names_a.Replace(Environment.NewLine, ";").Split(";")
    4.         Dim n_b() As String = names_b.Replace(Environment.NewLine, ";").Split(";")
    5.  
    6.         If n_a.Count = n_b.Count Then
    7.  
    8.             Return String.Join(Environment.NewLine, Enumerable.Range(0, n_a.Count).Select( _
    9.                                Function(i) n_a(i) & Environment.NewLine & n_b(i)))
    10.  
    11.         End If
    12.  
    13.         Return String.Empty
    14.  
    15.     End Function
    and use the function as follows:
    vb.net Code:
    1. 'Question 1: Joining the 2 strings into a single string with alternating names:
    2.         Dim alt_names As String = join_strings(string1, string2)
    3.  
    4.         MessageBox.Show(alt_names)
    5.  
    6.         'Question 2: Filling them into a listbox.
    7.         ListBox1.Items.AddRange(alt_names.Replace(Environment.NewLine, ";").Split(";"))

    Regards Tom
    Last edited by ThomasJohnsen; Nov 4th, 2012 at 11:33 AM.
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width