none
Join string two list to one RRS feed

  • Question

  • Hi All,

    I try to join of string between two string to one.It can be succeed by using this code

      For I As Integer = 0 To AA_.Count - 1
                TextBox1.AppendText(AA_(I) & BB_(I) & vbCrLf)
               
            Next


     But I want try use then "Concat", String.Join or whatever.

    Public Class Form1
        Dim A_ As New List(Of String)
        Dim B_ As New List(Of String)
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            A_.Add("01")
            A_.Add("23")
            A_.Add("74")
    
            B_.Add("2")
            B_.Add("7")
            B_.Add("8")
    
    
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim AA_ As New List(Of String)
            Dim BB_ As New List(Of String)
    
            For I As Integer = 0 To A_.Count - 1
                TextBox1.AppendText(A_(I) & vbCrLf)
                AA_.Add(A_(I) & vbCrLf)
            Next
    
            For I As Integer = 0 To B_.Count - 1
                TextBox2.AppendText(B_(I) & vbCrLf)
                BB_.Add(B_(I) & vbCrLf)
            Next
    
            'Dim MixAB_ = AA_.Concat(BB_)
            'Dim MixAB_ = String.Join(AA_, BB_)
            'Dim MixAB_ = AA_.AddRange(BB_)
    
            For I As Integer = 0 To MixAB_.Count - 1
                TextBox3.AppendText(MixAB_(I) & vbCrLf)
            Next
    
    
    
        End Sub
    End Class

    TextBox3 show the result like "012 , 237, 748"

    Thank.



    • Edited by mipakteh Sunday, January 12, 2020 4:17 AM mistake
    Sunday, January 12, 2020 4:12 AM

Answers

  • Hi,
    you can use Func to concatenate string and then Join to get the result string like in this console demo:

    Imports System.Linq
    
    Module Module1
      Sub Main()
        Try
          Call (New Demo).Execute()
        Catch ex As Exception
          Dim ex1 = ex
          While ex1 IsNot Nothing
            Console.WriteLine(ex1.Message)
            ex1 = ex1.InnerException
          End While
        End Try
        Console.WriteLine("Continue enter key")
        Console.ReadKey()
      End Sub
    
      Friend Class Demo
    
        Friend Sub Execute()
          Dim A_ As New List(Of String)
          Dim B_ As New List(Of String)
    
          A_.Add("01")
          A_.Add("23")
          A_.Add("74")
    
          B_.Add("2")
          B_.Add("7")
          B_.Add("8")
    
          Dim f = New Func(Of List(Of String), List(Of String), IEnumerable(Of String))(Iterator Function(a, b)
                                                                                          For i = 0 To a.Count - 1
                                                                                            Yield a(i) & b(i)
                                                                                          Next
                                                                                        End Function)
    
          Dim result = String.Join(", ", f(A_, B_))
          Console.WriteLine(result)
    
        End Sub
    
      End Class
    
    End Module


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    • Marked as answer by mipakteh Sunday, January 12, 2020 11:42 AM
    Sunday, January 12, 2020 7:06 AM
  • Check this example too:

    Dim Result As String = String.Join(vbCrLf, A_.Zip(B_, Function(a, b) a & b))

    • Marked as answer by mipakteh Sunday, January 12, 2020 11:43 AM
    Sunday, January 12, 2020 8:39 AM

All replies

  • Hi,
    you can use Func to concatenate string and then Join to get the result string like in this console demo:

    Imports System.Linq
    
    Module Module1
      Sub Main()
        Try
          Call (New Demo).Execute()
        Catch ex As Exception
          Dim ex1 = ex
          While ex1 IsNot Nothing
            Console.WriteLine(ex1.Message)
            ex1 = ex1.InnerException
          End While
        End Try
        Console.WriteLine("Continue enter key")
        Console.ReadKey()
      End Sub
    
      Friend Class Demo
    
        Friend Sub Execute()
          Dim A_ As New List(Of String)
          Dim B_ As New List(Of String)
    
          A_.Add("01")
          A_.Add("23")
          A_.Add("74")
    
          B_.Add("2")
          B_.Add("7")
          B_.Add("8")
    
          Dim f = New Func(Of List(Of String), List(Of String), IEnumerable(Of String))(Iterator Function(a, b)
                                                                                          For i = 0 To a.Count - 1
                                                                                            Yield a(i) & b(i)
                                                                                          Next
                                                                                        End Function)
    
          Dim result = String.Join(", ", f(A_, B_))
          Console.WriteLine(result)
    
        End Sub
    
      End Class
    
    End Module


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    • Marked as answer by mipakteh Sunday, January 12, 2020 11:42 AM
    Sunday, January 12, 2020 7:06 AM
  • Check this example too:

    Dim Result As String = String.Join(vbCrLf, A_.Zip(B_, Function(a, b) a & b))

    • Marked as answer by mipakteh Sunday, January 12, 2020 11:43 AM
    Sunday, January 12, 2020 8:39 AM
  • Thank you very Peter.
    Sunday, January 12, 2020 11:42 AM
  • Thank you very much Viorel
    • Marked as answer by mipakteh Sunday, January 12, 2020 11:43 AM
    • Unmarked as answer by mipakteh Sunday, January 12, 2020 11:43 AM
    Sunday, January 12, 2020 11:43 AM
  • Seems pretty efficient:
            A_.Add("01")
            A_.Add("23")
            A_.Add("74")
    
            B_.Add("2")
            B_.Add("7")
            B_.Add("8")
    
            A_.AddRange(B_)

    Wednesday, January 15, 2020 2:50 AM
  • Thank Devon.
    Sunday, January 19, 2020 3:42 AM