如何判断一个字符串中是否包含特定字符串 并标记出来
举例a=西瓜1 菠萝5 蜜桃3
b=菠萝 桔子 西瓜 番茄 苹果 柚子
首先判断a中的水果是否都在b中,b中没有蜜桃,最终返回的结果是“西瓜1 菠萝5 蜜桃3”蜜桃标记为红色加粗字体 其余不变。
帮忙写一段详细的代码,非常感谢。
Private Sub Form_Load() Dim A, B, I As Integer, J As Integer, C() As String A = Array("西瓜1", "菠萝5", "蜜桃3") B = Array("菠萝", "桔子", "西瓜", "番茄", "苹果", "柚子") ReDim C(0 To UBound(A)) 'C的下标应该与A的下标相同 For I = 0 To UBound(A) For J = 0 To UBound(B) If InStr(1, A(I), B(J), 1) > 0 Then C(I) = "存在" End If Next J Next I For I = 0 To UBound(A) If C(I) = "存在" Then Label1.Caption = Label1.Caption & A(I) & Space(6) ElseIf C(I) = "" Then Label2.Caption = Label2.Caption & A(I) & Space(6) End If Next I End Sub
Private Sub Command1_Click() Dim B, I As Integer, J As Integer, C() As String, D() As String Label1.Caption = "" Label2.Caption = "" Text1.Text = Replace(Text1.Text, " ", ",") D() = Split(Text1.Text, ",") 'Print D(3) B = Array("菠萝", "桔子", "西瓜", "番茄", "苹果", "柚子") ReDim C(0 To UBound(D)) 'C的下标应该与A的下标相同 For I = 0 To UBound(D) For J = 0 To UBound(B) If InStr(1, D(I), B(J), 1) > 0 Then C(I) = "存在" End If Next J Next I For I = 0 To UBound(D) If C(I) = "存在" Then RichTextBox1.Text = Label1.Caption & D(I) & Space(1) ElseIf C(I) = "" Then Label2.Caption = Label2.Caption & D(I) & Space(1) End If Next I Text1.Text = Replace(Text1.Text, ",", " ") End Sub