Office中国论坛/Access中国论坛

标题: 如何取得字符串中的特定部分 [打印本页]

作者: sgrshh29    时间: 2006-8-28 23:21
标题: 如何取得字符串中的特定部分
有一字符串:aabbbaaaabababbabbbbbbabbaab,用什么方法可以取得其中最长连续的aaaa和最长的bbbbbb(越简单越好)?
作者: wwwwa    时间: 2006-8-29 00:46
1、ff = "aabbbaaaabababbabbbbbbabbaab"
For i = 1 To 2
If i = 1 Then
pd = "a"
Else
pd = "b"
End If
pds = 0
For j = 1 To Len(ff)
ty = Mid(ff, j, 1)
If ty = pd Then
pds = pds + 1
Else
tt = "insert into js(ZF,ZFS) values(" & """" & pd & """" & "," & pds & ")"
If pds >= 2 Then
'MsgBox tt & pds
CurrentDb.Execute (tt)
End If
pds = 0
End If
Next
Next
Set QQRR = CurrentDb.OpenRecordset("SELECT ZF,MAX(ZFS) FROM JS GROUP BY ZF")
Do While Not QQRR.EOF
MsgBox QQRR(0) & QQRR(1)
QQRR.MoveNext
Loop

2、SQL

SELECT INSTR("aabbbaaaabababbabbbbbbabbaab","aa"),("aabbbaaaabababbabbbbbbabbaab","aaa"),("aabbbaaaabababbabbbbbbabbaab","aaa"),("aabbbaaaabababbabbbbbbabbaab","aaaa"),("aabbbaaaabababbabbbbbbabbaab","bb"),("aabbbaaaabababbabbbbbbabbaab","bbb"),("aabbbaaaabababbabbbbbbabbaab","bbbb")FROM TT

这是一个笨方法


作者: sgrshh29    时间: 2006-8-29 01:01
谢谢!找时间试一下.
作者: cbqiang_59    时间: 2006-8-29 04:46
str="aabbbaaaabababbabbbbbbabbaab"
strA=""
strB=""
strC=""
j=Mid(str,1,1)
for i=1 to Len(str)
if Mid(str,i,1)<>j then
  if j="a" then
    if Len(strC)>Len(strA) then
      strA=strC
      strC=""
      strC=mdi(str,i,1)
      j=mdi(str,i,1)
    end if
  else
    if len(strC)>len(strB) then
      strB=strC
      strC=""
      strC=mdi(str,i,1)
      j=mdi(str,i,1)
    end if
  end if
else
  strC=strC & mdi(str,i,1)
  j=mdi(str,i,1)
End if
next i

strA,strB 分别为最大的串
作者: cbqiang_59    时间: 2006-8-29 05:04
str="aabbbaaaabababbabbbbbbabbaab"
strA=""
strB=""
strC=""
j=Mid(str,1,1)
for i=1 to Len(str)
if Mid(str,i,1)<>j then
  if j="a" then
    if Len(strC)>Len(strA) then
      strA=strC
    end if
  else
    if len(strC)>len(strB) then
      strB=strC
    end if
  end if
  strC=""
  strC=mdi(str,i,1)
  j=mdi(str,i,1)
else
  strC=strC & mdi(str,i,1)
  j=mdi(str,i,1)
End if
next i
  
作者: cbqiang_59    时间: 2006-8-29 05:27
Private Sub Command0_Click()
Dim str As String
Dim strA As String
Dim strB As String
Dim strC As String
Dim i As Integer
Dim j As String
str = "aabbbaaaabababbabbbbbbabbaabaaaaaaaa"
strA = ""
strB = ""
strC = ""
j = Mid(str, 1, 1)
For i = 1 To Len(str)
If Mid(str, i, 1) <> j Then
  If j = "a" Then
    If Len(strC) > Len(strA) Then
      strA = strC
    End If
  Else
    If Len(strC) > Len(strB) Then
      strB = strC
    End If
  End If
  strC = ""
  strC = Mid(str, i, 1)
  j = Mid(str, i, 1)
Else
  strC = strC & Mid(str, i, 1)
  j = Mid(str, i, 1)
End If
Next i
If j = "a" Then
    If Len(strC) > Len(strA) Then
      strA = strC
    End If
Else
    If Len(strC) > Len(strB) Then
      strB = strC
    End If
End If
  MsgBox "a最长的是'" & strA & "'"
  MsgBox "b最长的是'" & strB & "'"
End Sub

作者: sgrshh29    时间: 2006-8-29 21:40
都不错,感谢!就是代码长了点.

Function mx(zfc As String, fgf As String) As Integer
Dim i As Integer
Dim n As Integer
n = UBound(Split(zfc, fgf))
mx = Len(Split(zfc, fgf)(0))
For i = 0 To n
If mx < Len(Split(zfc, fgf)(i)) Then mx = Len(Split(zfc, fgf)(i))
Next i
End Function
调用:
maxa = mx("aabbbaaaabababbabbbbbbabbaab", "b"): MsgBox maxa & "连a"
maxb = mx("aabbbaaaabababbabbbbbbabbaab", "a"): MsgBox maxb & "连b"


[此贴子已经被作者于2006-8-29 14:35:38编辑过]


作者: wwwwa    时间: 2006-8-29 23:56
ff = "aabbbaaaabababbabbbbbbabbaab"
For i = 1 To 2
rt = Split(ff, IIf(i = 1, "a", "b"))
For i1 = LBound(rt) To UBound(rt) - 1
For j = i1 + 1 To UBound(rt)
If rt(i1) < rt(j) Then
ty = rt(i1)
rt(i1) = rt(j)
rt(j) = ty
End If
Next
Next
MsgBox rt(0)
Next
作者: sgrshh29    时间: 2006-8-30 00:27
以下是引用wwwwa在2006-8-29 15:56:00的发言:
ff = "aabbbaaaabababbabbbbbbabbaab"
For i = 1 To 2
rt = Split(ff, IIf(i = 1, "a", "b"))
For i1 = LBound(rt) To UBound(rt) - 1
For j = i1 + 1 To UBound(rt)
If rt(i1) < rt(j) Then
ty = rt(i1)
rt(i1) = rt(j)
rt(j) = ty
End If
Next
Next
MsgBox rt(0)
Next

好![em17]




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3