2017-09-15 65 views
-1

獨立和組有#without #獨立和組有#和無#

Dim Strfinal as String = "#Cccccc,#Aaaaaa,Aaaaa,Baaaa,Caaaa,#Bbbbbb,Abbbbb,Bbbbbb,Cbbbbb"

弦弦我想這樣
#Cccccc, #Aaaaaa(Aaaaa,Baaaa and Caaaa) and #Bbbbbb(Abbbbb,Bbbbbb and Cbbbbb)

輸出我用這個代碼分開沒有的字符#

Dim rws As String 

If Aaaaa.Checked = True Then 
    CheckBox1.Checked = True 
    close_parenthesis.Checked = True 

    If rws = "" Then 
     rws = "(" + Aaaaa.Text 
    Else 
     rws = "(" + Aaaaa.Text 
    End If 
End If 

If Aaaaa.Checked = False Then 
    rws = "" 
End If 

If Baaaa.Checked = True Then 
    CheckBox1.Checked = True 
    close_parenthesis.Checked = True 
    If rws = "" Then 
     rws = "(" + Baaaa.Text 
    Else 
     rws = rws & ", " & Baaaa.Text 
    End If 
End If 

If Caaaa.Checked = True Then 
    CheckBox1.Checked = True 
    close_parenthesis.Checked = True 
    If rws = "" Then 
     rws = "(" + Caaaa.Text 
    Else 
     rws = rws & ", " & Caaaa.Text 
    End If 
End If 

If close_parenthesis.Checked = True Then 
    If rws = "" Then 

    Else 
     rws = rws + close_parenthesis.Text 
    End If 
End If 

CheckBox1.Text = rws.ToString 
Me.CagayanReplace.PerformClick() 

我使用此代碼爲,風雲變幻的字and括號內

Dim Strng As String = Me.CheckBox1.Text 
'now find the position of last appearing "," 
Dim comaposition As Integer 
comaposition = Strng.LastIndexOf(",") 'it is zero based 

'if not found, it will return -1 and u can exit, no need to do the work 
If comaposition = "-1" Then 
    Exit Sub 
End If 

'remove the comma 
Dim String_After_Removing_Comma As String 
String_After_Removing_Comma = Strng.Remove(comaposition, 1) 

'add "and" in the same position where comma was found 
Dim final_string As String 
final_string = String_After_Removing_Comma.Insert(comaposition, " and ") 

'show it on the textbox 
CheckBox1.Text = final_string` 

請幫我解決這個問題。

+0

這種很爛 - 我希望我能做點什麼。 – EJoshuaS

+0

這是什麼語言?你已經標記了它[vb.net],但代碼不是VB.NET。 – Enigmativity

+0

我使用Vb.net 2017 –

回答

0

這裏不是一個漂亮的解決方案,可以優化很多。但是它幹得不錯:

Dim Strfinal As String = "#Cccccc,#Aaaaaa,Aaaaa,Baaaa,Caaaa,#Bbbbbb,Abbbbb,Bbbbbb,Cbbbbb" 
Dim splittedString As String() = Strfinal.Split(",") 
Dim a As List(Of String) = New List(Of String) 
Dim newstr As String = "" 

For Each str As String In splittedString 
     If Not str.Contains("#"c) Then 
      a.Add(str) 
      Continue For 
     End If 
     If a.Count > 0 Then 
      Dim b = String.Join(",", a) 
      Dim i = b.LastIndexOf(","c) 
      b = b.Remove(i, 1) 
      b = b.Insert(i, " and ") 
      newstr += "(" + b + ")," + str 
      a.Clear() 
     Else 
      newstr += str + "," 
     End If 
Next 

If a.Count > 0 Then 
     Dim b = String.Join(",", a) 
     Dim i = b.LastIndexOf(","c) 
     b = b.Remove(i, 1) 
     b = b.Insert(i, " and ") 
     newstr += "(" + b + ")" 
     a.Clear() 
End If 

newstr = newstr.Replace(",(", " (") 

Dim z = newstr.LastIndexOf(",#") 
newstr = newstr.Remove(z, 1) 
newstr = newstr.Insert(z, " and ") 

Console.WriteLine(newstr) 

輸出:

#Cccccc,#Aaaaaa (Aaaaa,Baaaa and Caaaa) and #Bbbbbb(Abbbbb,Bbbbbb and Cbbbbb) 

檢查它Fiddle

+0

我嘗試你的代碼,但當我改變時: Dim Strfinal As String「#Cccccc,#Aaaaaa,Aaaaa,Baaaa,Caaaa,# Bbbbbb,Abbbbb,Bbbbbb,Cbbbbb「Dim Dim Strfinal As String = Me.checkbox13.text messagebox say」StartIndex不能小於零「如何解決它我不知道 –

+0

Checkbox13.text'顯示覆選框的輸出點擊 –

+0

謝謝,我知道了..... !!!!! –