‘*************************************************************************
' * * Module name: Join function and Split function Refinement example ' * * said Ming: Blue Phoenix Design Mall fire Phoenix-Guo Wei | Blue Phoenix-Magic Spirit | Guo Wei-icecept ' * * Creator: Bath fire Phoenix-Guo Wei ' * * Period: October 11, 2015 17:49:52 ' * * Modified by: Shower Fire Phoenix-Guo Wei ' * * Day Period: ' * * Description & nbsp: qq:493405998 | \ Desire: Icecept ' * * version Ben: V1.0.0 | http://blog.sina.com.cn/icecept ' ************************************ ' Have you ever encountered the need to save the contents of an array to the Tag property of the registry, file, or control? In VB6 and. NET, the join function and the Split function provide you with an easy way to do this? These two functions also have the ability to import a one-dimensional array into a string and recover an array from a string? ' The parameters of the join function are an array and a delimiter (optional delimiter), which imports all elements of an array into a string, separates the elements with delimiters, and returns the string. ' Join function usage: return string = Join (original array, delimited string) ' description ' returns a string that is created by connecting multiple substrings in an array. ' Syntax ' join (list[, delimiter]) ' Join function syntax has the following parts: ' list required? ' A one-dimensional array containing concatenated substrings? ' Delimiter is optional. The character used to delimit substrings in the return string. If the key is omitted, a space ("") is used to delimit the substring. If delimiter is a 0-length string (""), all items in the list are concatenated, with no delimiters in the middle. '--------------------------------------------------------------------------------------------------- ' Split function Usage: return array = Split (original string [, String to find] [, split into several arrays] [, Comparison mode]) ' description ' returns a zero-based, one-dimensional array that contains the specified number of sub-charactersString. ' Grammar ' Split (expression[, delimiter[, count[, compare]]) ' Split function syntax has the following parts: ' expression Required. A string expression that contains substrings and delimiters. If expression is a zero-length string (""), split returns an empty array, that is, an array with no elements and data. ' Delimiter is optional. A string character used to identify the bounds of a substring. If omitted, an empty characters ("") is used as the delimiter. If delimiter is a zero-length string, the returned array contains only one element, the complete expression string. ' Count is optional. The number of substrings to return, 1 means that all substrings are returned. ' Compare is optional. Numeric values, which are used to discriminate substrings (that is, to indicate case sensitivity): Perform binary comparisons (differentiate), perform text comparisons (no distinction) ' Set value ' the setting value of the Compare parameter is as follows: ' constant values description ' vbusecompareoption -1 &NBSP ; Perform a comparison with the set value in the Option Compare statement. ' Vbbinarycompare 0 perform binary comparison. ' vbTextCompare 1 perform text comparisons. ' Vbdatabasecompare 2 for Microsoft Access only. Performs a comparison based on information from your database. The ' Join function and the Split function are useful in situations where you need to convert the contents of an array to a string and restore an array from a string. For example, convert the width of each column of a table to a string saved to the registry and restore the column width from the registry. Using these two functions simplifies saving and working with array content. option explicit private Sub Command1_Click () Dim MyStR as string mystr = "1234567123456712345" mystrs = Split (MyStr, "" ") for each STRs In mystrs Debug.Print strs nextend Sub ' output: "12345", "12345", "12345" "========= ======================================================== ' This VB program is to ask 10 students for the average score of test scores. For example 95 85 70 75 80 90 60 65 95 100 ' The average score of these 10 people ... private Sub Command2_Click () Dim a$ (), I as Long, IntB As String, S as integer If Dir ("d:\ average") = vbNullString then Open "d:\ average score. Dat" For Output as #1 Print #1, "All-in-A-Z," Close # 1 End if Open "d:\ average score. Dat" for Input as #1 Input #1, intb Close #1 &nbs P A = Split (IntB, space$ (1)) For i = 0 to UBound (a) Debug.Print A (i); space$ (1); s = s + A (i) Nexti Debug.Print ", the average score for 10 students is:" & S/10end sub private Sub Command3_Click () Dim astring As string Dim R () as String ' put variable Follow the "," split array astring = "Advanced, intermediate, Low, advanced" r = Split (astring, ",") The third data in the ' string with ', ' split into array Debug.Print ' Array is: ' & R (2) ' output in another way ' When the parentheses are written in 0 o'clock, the first element in the array is returned, and the parentheses write 1 o'clock returns the second element in the array. And so on, when you return data in this notation,, ' must separate the string with a single space, and the other characters as only one data. Because the string you want is omitted, the default is a space. Example: Dim astring as string astring = "Advanced intermediate low Level advanced" Debug.Print Split (astring) (0) &nbs P ' with Debug.Print split (Astring,space (1)) (0) Same Debug.Print split (astring) (1) Debug.Print Split (astring) (2) Debug.Print Split (astring) (3) Debug.Print " The following are the wrong uses-------------------------------------" astring = "Advanced, intermediate, Low, advanced" Debug.Print Split (astring) (0) ' This will produce an error, because the string to be found is ', ' while omitting the string to find after Debug.Print Split (Astring) (1) ' The default delimiter is a space, and this segment is "," so only return advanced, intermediate, low, advanced only as a string Debug.Print Split ( astring) (2) ' The other three produce subscript out-of-bounds error Debug.Print Split (astring) (3) End sub ' ================================ ================================ private Sub Command4_click () Dim s as String, T () as string &nbs P s = "1a2a3a4a5a6a7" ' count use example, return four strings ' return value: A is a delimiter, the last one takes the remaining as a string back ' 1 & nbsp ' 2 ' 3 ' 4a5a6a7 t () = Split (S, "A", 4) Debug.Print Join (T, vbCrLf)   ; End sub private Sub Command5_click () Dim s as String, T () as string s = "1a2a3a4a5a6a7" &nbs P ' Compare use example, ignoring case, default: Vbbinarycompare case differs Debug.Print "Ignore case" & vbcrlf T () = Spli T (S, "a",, vbTextCompare) DEbug. Print Join (T, vbCrLf) ' return value: ' 1 ' 2 ' 3 ' 4 ' 5&N Bsp ' 6 ' 7End sub private Sub Command6_click () Dim s as String, T () as string   ; s = "1a2a3a4a5a6a7" ' compare use example, ignoring case, default: Vbbinarycompare case difference Debug.Print "Do not ignore case" & VB crlf t () = Split (S, "a",, Vbbinarycompare) Debug.Print Join (T, vbCrLf) ' return value: ' 1a2a3a4a5a6a7end sub private Sub Command7_click () Dim I as Integer ' Loop variable Dim s As string Dim D As string Dim iCount As Integer &NB Sp ' Total number Dim arr () as string s = 567904 ' string 1 d = 574   ; ' string 2 for i = 1 to Len (s) arr = Split (S, mid$ (d, I, 1)) ICount = ICount + UBound (arr) Next i MsgBox "in & D &" with "& ICount & "Characters are the same as the characters in & S &." End sub private Sub Command8_click () Dim StrTest1 () As Variant, Strins () As Variant ' This is the two original arrays that need to be merged, Strins is the array that needs to be inserted into the StrTest1 array ' T1-t4 for intermediate variables Dim T1 As String, T2 as String, T3 as Strin G, focus position when T4 as string ' segmented string Dim Position as Integer, Sss as String, strsource () as string ' When an array is assigned an initial value, the array must be variant type STRTEST1 = Array ("A", "B", "0", "C", "1", "2", "3", "4", "5", "D") &nb Sp ' original array one data Strins = Array ("V", "XM", "U", "1") ' original array two data T1 = Join (StrTest1 ( ), ",") ' Convert the original array to a T1 variable T2 = Join (Strins (), ",") ' the same as the original number Group Two with "," converted into a string to deposit T2 variable Position = InStr (T1, "2") ' Find array two locations to insert &nbsP T3 = mid$ (T1, 1, Position + 1) ' Take the string T1 from the beginning to the insertion position plus a place, deposit T3 string variable T4 = mid$ (T1, Position + 1) ' Take the string T1 from the insertion position, and all the characters until the end of the T4 variable SSS = T3 & T2 & T4 ' combine three string variables as required into the dimension of the SSS variable ' statistic original array, the type must be consistent at both ends of the equals sign strsource = Split (Sss, ",") Debug.Print "Two arrays before merging:" & vbcrlf Debug.Print "StrTest1:" & Join (StrTest1, space$ (1)) & VBCRLF&NB Sp Debug.Print "Strins:" & Join (Strins, space$ (1)) & vbcrlf Debug.Print "Two arrays merged with a unified array strsource: "& vbcrlf Debug.Print Join (strsource, space$ (2)) ' two arrays before merging: ' &NB Sp ' strtest1:a B 0 C 1 2 3 4 5 d ' ' strins:v XM U 1 ' ' two arrays merged with unified array Strsou rce: ' ' A b 0 c 1 2 v xm u 1 3   ; 4 5 dend Sub private Sub Command9_click () Dim aaa (3) as String, BBB (3) as string Dim Sss as string& nbsp AAA (0) = "111" AAA (1) = "222" AAA (2) = "333" AAA (3) = "444" BBB (0) = "VVV" BBB (1) = "CCC" BBB (2) = "xxx" BBB (3) = "zzz" SSS = Join (aa A) & space$ (1) & Join (BBB) MsgBox Sssend Sub
Join function and Split function fine solution example