2015-11-22 22 views
2

「我有下面的字符串..以‘#’的VBScript:度假村串並組新的拆分項

myStr = "78,6$25,01|25,02|25,03|25,04#74,5$15,01|15,02|15,03|15,04#70,1$25,06|25,07|25,08|25,09#77,3$25,07|25,08|25,09|25,10#78,2$10,05|10,06|10,07|10,08" 

splited」,我想再打這與新字符串新的分組...

「我應該組splited字符串以美元($)chrachter後第一個值

但我不ķ我現在應該怎麼排序和分組到新的期望結果

myStrDesired = "78,2$10,05|10,06|10,07|10,[email protected],5$15,01|15,02|15,03|15,[email protected],6$25,01|25,02|25,03|25,04#70,1$25,06|25,07|25,08|25,09#77,3$25,07|25,08|25,09|25,10" 

我的腳本:

Function GroupArrays() 
    myStr = "78,6$25,01|25,02|25,03|25,04#74,5$15,01|15,02|15,03|15,04#70,1$25,06|25,07|25,08|25,09#77,3$25,07|25,08|25,09|25,10#78,2$10,05|10,06|10,07|10,08" 

    'And I want resort to this New String with New Grouping... 
    'i should group splited string with first value after dollar($) chrachter 

    myStrDesired = "78,2$10,05|10,06|10,07|10,[email protected],5$15,01|15,02|15,03|15,[email protected],6$25,01|25,02|25,03|25,04#70,1$25,06|25,07|25,08|25,09#77,3$25,07|25,08|25,09|25,10" 

    arrMyStr = Split(myStr,"#") 
    arrMyStrDesired = "" 
    for i = 0 to UBound(arrMyStr) 
    ' find group id from each string 
    groupVal = Split(Split(arrMyStr(i),"$")(1),",")(0) 

    ' put the same groups together and split them by "#" And finally the isolation of other disciplines with "@" 

     arrMyStrDesired = arrMyStrDesired & arrMyStr(i) 
    next 

    GroupArrays = arrMyStrDesired 

End Function 

新說明:*

  1. 斯普利特主串「#」號。

  2. 在splited每個零件......看到「$」,並將其命名爲「的groupId」後的第一個值。 (它是用於分組的重要參數和排序)

  3. 每個部件的所有具有相同的groupId應該由並排放置,並通過「#」加入。

  4. 上述步驟後......我們應該通過「@」與其他groupId加入所有新字符串....與...相同...(00 $ 01,05#01,06#... @ 02,07 @ 03,4 .....)

+0

的答案是我的問題太辛苦? 我以爲我很初學。 – sadrasjd

+0

我已經多次查看這個問題,並發現措辭相當混亂。我認爲* *您想拆就''#,用後'$'由價值決定的自定義排序順序生成的數組排序,然後用''#歸隊他們 - 但措辭非常模糊,如果這是你想要什麼。 –

+0

@約翰·科爾曼:我很抱歉壞字眼...... PLZ看到功能 – sadrasjd

回答

1

下應該在任何Windows機器上工作,與斑點網運行。如果由於某種原因,你沒有這樣的 - 需要自定義排序:

myStr = "78,6$25,01|25,02|25,03|25,04#74,5$15,01|15,02|15,03|15,04#70,1$25,06|25,07|25,08|25,09#77,3$25,07|25,08|25,09|25,10#78,2$10,05|10,06|10,07|10,08" 

myDesiredStr = "78,2$10,05|10,06|10,07|10,[email protected],5$15,01|15,02|15,03|15,[email protected],6$25,01|25,02|25,03|25,04#70,1$25,06|25,07|25,08|25,09#77,3$25,07|25,08|25,09|25,10" 

Function GroupVal(group) 
    A = Split(group,"$") 
    B = Split(A(1),",") 
    GroupVal = CInt(B(0)) 
End Function 

Function ReSort(str) 
    Set D = CreateObject("Scripting.Dictionary") 
    Set keyList = CreateObject("System.Collections.ArrayList") 
    groups = Split(str,"#") 
    For i = 0 to UBound(groups) 
     group = groups(i) 
     v = GroupVal(group) 
     If D.Exists(v) Then 
      D.Item(v) = D.Item(v) & "#" & group 
     Else 
      D.Add v,group 
      keyList.Add v 
     End If 
    Next 

    keyList.Sort() 
    newGroups = Array() 
    ReDim newGroups(Ubound(groups)) 
    i = -1 
    For Each v In keyList 
     i = i + 1 
     newGroups(i) = D.item(v) 
    Next 
    ReDim Preserve newGroups(i) 
    Resort = Join(newGroups,"@") 
End Function 

MsgBox myDesiredStr = Resort(myStr) 

的MSGBOX彈出True

+0

謝謝你非常匹配先生@John Coleman。 你對我很有幫助。 – sadrasjd