Merging Two String Array

Below function can be used to merge two string array,This function will combine two string array into one,eleminating duplicate element.


'Pass the Destination and Source string array that need to be merged
Private Function MergeArray(ByVal arrDestination() As String, ByVal arrSource() As String) As String()
Dim intElement As Integer
Try
For intElement = 0 To arrSource.Length - 1
'Check if element already exist,in no Re-Dimension the Destination array and add the New element in it.
If Array.IndexOf(arrDestination, arrSource(intElement)) = -1 Then
ReDim Preserve arrDestination(arrDestination.Length)
arrDestination(arrDestination.Length - 1) = arrSource(intElement)
End If
Next
'Return the Resultant mergred array
Return arrDestination
Catch exc As Exception
Throw exc
End Try
End Function

'e.g
Dim arrArray1() As String = {"My", "Name"}
Dim arrArray2() As String = {"Name", "Is", "ABC"}
'After mergin above array the fginal value in arrArray1 will be {"My,"Name", "Is", "ABC"}
arrArray1 = MergeArray(arrArray1, arrArray2)


PS:Same logic can be used to merge anytype of array.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: