现在在list控件中有若干选项
比如
ppv1_11
pp2_33
PP3_44
P3_99
PP6_22
等等
怎样提取“_”前面的字符(但不包含最后一个数字),不重复,如提取出PPV,PP,P
比如
ppv1_11
pp2_33
PP3_44
P3_99
PP6_22
等等
怎样提取“_”前面的字符(但不包含最后一个数字),不重复,如提取出PPV,PP,P
解决方案 »
- vb中怎样使用updata语句
- 丢失 microsoft data source 定位器
- 一个我始终没有找到答案的问题(控件层次问题)?
- 求助:怎样获取局域网所有机器的ip 和mac 地址?
- 如何通过代理使用winsock发送包?
- 关于日期时间的比较 又一个问题!!!!!
- 求助!有关VB做数据库的问题!
- 如何实现在msflexgrid网格上当鼠标移动在相应的行(row)时,行的背景颜色(backcolor)变高亮色?
- 用listbox可以直接显示数据库的内容吗?
- 怎样做出那种可选择的标签!像屏幕的属性那样!
- 如何在VB中读取treeview最内层的节点的值
- 想调用一个存储过程,总是出错,帮忙看看
二、把取得的字符串添加到一个集合中。添加前要检查这个串是否存在。
最后集合中的就是不重复的内容。
dim str as string
str="P3_99"
dim code as long
dim i as long
for i=1 to len(str)
code=ascw(mid(str,i,1))
if code>=48 and code<=57 then
'数字
exit for
end if
next
ppv1_11
pp2_33
PP3_44
P3_99
PP6_22 下面的方法不用数组,减少数组循环查找重复费时。暂时不是很明确楼主的目的,整了两套方案1.Private Sub Command1_Click()
Dim ss$, s$(), i&
ss = ","
For i = 0 To Form1.List1.ListCount - 1
If 0 < InStr(Form1.List1.List(i), "_") Then
s = Split(Form1.List1.List(i), "_")
If 0 = InStr(UCase(ss), UCase("," & Left(s(0), Len(s(0)) - 1) & ",")) Then
ss = ss & Left(s(0), Len(s(0)) - 1) & "," '固定取消最后一位
End If
End If
Next i
MsgBox Mid(ss, 2)
End Sub
2.Private Sub Command1_Click()
Dim ss$, s$(), i&
ss = ","
For i = 0 To Form1.List1.ListCount - 1
If 0 < InStr(Form1.List1.List(i), "_") Then
s = Split(Form1.List1.List(i), "_")
For j = 1 To Len(s(0))
If IsNumeric(Mid(s(0), j, 1)) = True Then
If 0 = InStr(UCase(ss), UCase("," & Left(s(0), j - 1) & ",")) Then
ss = ss & Left(s(0), Len(s(0)) - 1) & "," '固定取数字前
End If
Exit For
End If
Next j
End If
Next i
MsgBox Mid(ss, 2)
End Sub
只需要把PPV,PP,P这几个字符放到一个数组就可以了,不是要这样按逗号来排列
ss数组里面就是你要的东西,最后那句 MsgBox Mid(ss, 2) 不要就可以了。
StrSS=split(Mid(ss, 2),“,”)
StrSS数组就是你要的东西。
Option ExplicitPrivate Sub Command1_Click()
Dim i As Long
Dim tmp As String
Dim col As Collection
Dim Result() As String
Set col = New Collection
On Error Resume Next
For i = 0 To List1.ListCount - 1
tmp = Mid(List1.List(i), 1, InStr(List1.List(i), "_") - 1)
'如果只要去除最后一位数字(多位数字也是去一位)
If Right(tmp, 1) Like "[0-9]" Then Mid(tmp, Len(tmp), 1) = Chr(32)
'检验重复
col.Add UCase(Trim(tmp)), UCase(Trim(tmp)) '不区分大小写
Next
On Error GoTo 0
'结果放入数组
ReDim Result(col.Count - 1)
For i = 0 To col.Count - 1
Result(i) = col(i + 1)
Next
'打印数组内容
Debug.Print Join(Result)
End SubPrivate Sub Form_Load()
List1.AddItem "ppv1_11"
List1.AddItem "pp2_33"
List1.AddItem "PP3_44"
List1.AddItem "P3_99"
List1.AddItem "PP6_22"
End Sub
Private Sub Command1_Click()
Dim i As Integer, j As Integer
Dim flg As Boolean, fag As Boolean
Dim jg() As String, tmp As String
ReDim jg(0)
For i = 0 To List1.ListCount - 1
tmp = List1.List(i)
tmp = Left(tmp, InStr(tmp, "_") - 2)
If Not fag Then
jg(0) = tmp
fag = True
Else
For j = 0 To UBound(jg)
If UCase(tmp) = UCase(jg(j)) Then '不分大小写,分的话去掉ucase
flg = True
Exit For
End If
Next
If Not flg Then
ReDim Preserve jg(UBound(jg) + 1)
jg(UBound(jg)) = tmp
Else
flg = False
End If End If
Next
End SubPrivate Sub Form_Load()
List1.AddItem "ppv1_11": List1.AddItem "pp2_33"
List1.AddItem "PP3_44": List1.AddItem "P3_99"
List1.AddItem "PP6_22"
End Sub