设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 1582|回复: 8
打印 上一主题 下一主题

[与其它组件] 如何取得字符串中的特定部分

[复制链接]
跳转到指定楼层
1#
发表于 2006-8-28 23:21:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
有一字符串:aabbbaaaabababbabbbbbbabbaab,用什么方法可以取得其中最长连续的aaaa和最长的bbbbbb(越简单越好)?
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2006-8-29 00:46:00 | 只看该作者
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

这是一个笨方法

3#
 楼主| 发表于 2006-8-29 01:01:00 | 只看该作者
谢谢!找时间试一下.
4#
发表于 2006-8-29 04:46:00 | 只看该作者
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 分别为最大的串
5#
发表于 2006-8-29 05:04:00 | 只看该作者
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
  
6#
发表于 2006-8-29 05:27:00 | 只看该作者
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
7#
 楼主| 发表于 2006-8-29 21:40:00 | 只看该作者
都不错,感谢!就是代码长了点.

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编辑过]

8#
发表于 2006-8-29 23: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
9#
 楼主| 发表于 2006-8-30 00:27:00 | 只看该作者
以下是引用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]
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-11-3 01:16 , Processed in 0.089789 second(s), 32 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表