自写函数VB6 STUFF函数 和 VB.net 2010 STUFF函数 详解
'*************************************************************************
'**模
块 名:自写函数VB6 STUFF函数 和 VB.net 2010 STUFF函数 详解
'**说 明:蓝凤凰设计商城
浴火凤凰-郭卫 | 蓝凤凰-魔灵 | 郭卫-icecept
'**创 建 人:浴火凤凰-郭卫
'**日
期:2015年10月10日 23:13:55
'**修 改 人:浴火凤凰-郭卫
'**日
期:
'**描 述:QQ:493405998 |
微信\旺旺:icecept
'**版 本:V1.0.0 | http://blog.sina.com.cn/icecept
'*************************************************************************
'删除,替换,插入字符 VB6 STUFF函数
Public Function stuff(ByVal StrSourceA As String, ByVal IntBeginPositionB As Integer, ByVal ModifyCharNumberC As Integer, ByVal ModifyCharD As String) As String
…………
End Function
'--------------------------------------------------------------------------------------
'stuff函数:stuff("原字符串","起始位置","插入(C=0)、替换(C>1)、删除(D="")的字符数","要插入(C=0)、替换(C>1)、删除(D="")的字符"
'重要提示因VB.6字符串的索引值是从1开始,本函数所有操作都是从1开始,字符串最多索引值是字符串总个数
'StrSourceA
是原字符串,
'IntBeginPositionB是起始位置,IntBeginPositionB=0 默认IntBeginPositionB=1
'ModifyCharNumberC是要替换?删除?插入的的字符数?
'ModifyCharD是要修改的字符串,ModifyCharD=""时可删除指定字符。
'ModifyCharNumberC=0
可插入指定字符,当IIntBeginPositionB <= Len(StrSourceA) + 1 时,在字符串位置的Len(StrSourceA) +
1 之间插入字符
'ModifyCharNumberC > 0 和 ModifyCharD 不等于空时,可替换指定字符
----------------------------------------------------------------------------------------------
'删除,替换,插入字符 VB.net 2010 STUFF函数
Public Class Form1
Public Function stuff(ByVal StrSource As String, ByVal IntBeginPosition As Integer, ByVal ModifyPosition As Integer, ByVal StrDestination As String) As String
………………
End Function
End
Class
'删除,替换,插入字符
'stuff函数:stuff("原字符串","起始位置(从零开始的索引值)","替换、删除的字符数(当此参数为零时进行插入操作)","要插入、替换字符(当此参数为空时,删除字符)"
'重要提示 因VB.net
2010字符串的索引值是从零开始,本函数所有的操作都是从0开始,字符串最多索引值是字符串总个数减一
'StrSourceA 是原字符串.
IntBeginPositionB是起始位置,
默认为零,IntBeginPositionB小于零时,默认为0
'ModifyCharNumberC是要替换?删除?插入的的字符数,当ModifyCharNumberC小于零时,默认为0
'ModifyCharD是要修改的字符串,ModifyCharD=""可删除指定字符。
'ModifyCharNumberC=0可插入指定字符,当第二个参数>=0,小于length时,在字符串位置的length之间插入字符
'IntBeginPositionB>=0
or
IntBeginPositionB<原字串.length且ModifyCharNumberC=0时,strD=""时,StrSourceA保持原样不变,(相当于把一个空字符插入到相应的字符处)
'IntBeginPositionB>=0
or
IntBeginPositionB<原字串.length时,ModifyCharNumberC>0,strD<>""可替换指定字符
'========================================================
我非常喜欢编写程序,因为我是学 会计电算化的,所以对Foxbase情有独衷,后来转型为VB,一至到至今用的VB.net 2010 我一直用着这个函数,功能非常强大,一个函数就可以'删除,替换,插入字符。不过我觉得其它编程语言的 Stuff函数太不好理解,我就按我理解的用VB6和VB.net 2010分别编写了Stuff函数,下面功能介绍如下。
程序示例:VB6 STUFF函数
Private ErrDescription As String '定义越位错误信息
Private Sub Command11_Click()
Dim str As String
'VB6的字符串索引值是从1开始的,而VB.NET 2010的字符串索引值是从0开始的
'从第六个字符开始,用123替换2个字符
Debug.Print ErrDescription & "|" & Stuff("abcdefg", 6, 2, "123")
'把123插入到原字符串未尾
Debug.Print ErrDescription & "|" & Stuff("abcdefg", Len("abcdefg") + 1, 0, "123")
'从第六个字符开始,删除两个字符
Debug.Print ErrDescription & "|" & Stuff("abcdefg", 6, 2, vbNullString)
'起始位置是零,默认为一,从第一个字符开始,删除四个字符
Debug.Print ErrDescription & "|", Stuff("abcdefg", 0, 4, vbNullString)
'起始位置是零,默认为一,用123替换从一开始的四个字符
Debug.Print ErrDescription & "|", Stuff("abcdefg", 0, 4, "123")
'起始位置小于零,默认为一,字符数小于零默认为0即插入,把123插入到原字符串原字符串前面
Debug.Print ErrDescription & "|", Stuff("abcdefg", -1, -1, "123")
'起始位置等于0,默认为一,字符数为零,即插入,,把123插入到原字符串原字符串前面
Debug.Print ErrDescription & "|", Stuff("abcdefg", 0, 0, "123")
'起始位置等于0,默认为一,字符数为零,即插入,把空字符串插入到原字符串的开头位置
Debug.Print ErrDescription & "|", Stuff("abcdefg", 0, 0, vbNullString)
'起始位置小于0,默认为一,字符数小于零默认为零,即插入,把空字符串插入到原字符串的开头位置
Debug.Print ErrDescription & "|", Stuff("abcdefg", -1, -1, vbNullString)
'把空字符串插入到原字符串的末尾
Debug.Print ErrDescription & "|", Stuff("abcdefg", Len("abcdefg") + 1, 0, vbNullString)
'把郭插入到第六个字符处
Debug.Print ErrDescription & "|", Stuff("abcdefg", 6, -1, "郭")
'查找逗号并删除
Debug.Print ErrDescription & "|", Stuff("你好,郭卫", InStrRev("你好,郭卫", ","), 1, vbNullString)
str = "你好,郭卫,吃了吗"
'查找郭的位置,并删除从它开始的三个字符
Debug.Print ErrDescription & "|", Stuff(str, InStrRev(str, "郭"), Len("郭卫,"), vbNullString)
'查找郭卫,的位置,并把早上好,插入到它的后面
Debug.Print ErrDescription & "|", Stuff(str, InStrRev(str, "郭") + Len("郭卫,"), 0, "早上好,")
'------------以下是数据越位信息-----------------’
'起始位置大于原字符串长度,报错
Debug.Print ErrDescription & "|", Stuff("abcdefg", 9, 0, vbNullString)
End Sub
运行结果:
Stuff("abcdefg",6,2,"123")|abcde123
Stuff("abcdefg",8,0,"123")|abcdefg123
Stuff("abcdefg",6,2,"")|abcde
Stuff("abcdefg",0,4,"")| efg
Stuff("abcdefg",0,4,"123")| 123efg
Stuff("abcdefg",-1,-1,"123")| 123abcdefg
Stuff("abcdefg",0,0,"123")| 123abcdefg
Stuff("abcdefg",0,0,"")| abcdefg
Stuff("abcdefg",-1,-1,"")| abcdefg
Stuff("abcdefg",8,0,"")| abcdefg
Stuff("abcdefg",6,-1,"郭")| abcde郭fg
Stuff("你好,郭卫",3,1,"")| 你好郭卫
Stuff("你好,郭卫,吃了吗",4,3,"")| 你好,吃了吗
Stuff("你好,郭卫,吃了吗",7,0,"早上好,")| 你好,郭卫,早上好,吃了吗
Stuff("abcdefg",9,0,"")| Stuff("abcdefg",9,0,"")数据越位
Private Sub Form_Click()
Dim str1 As
String
Dim i As Integer, j As
Integer
str1 = "6-15-8-9-5"
For i =
1 To UBound(Split(str1, "-"))
j =
InStr(1, str1, "-")
str1 =
stuff(str1, j, 1, ":" & i & "=")
Next
Debug.Print str1
'6:1=15:2=8:3=9:4=5
End Sub
Private Sub command1_Click()
Dim str1 As
String
str1 = Text1.Text
Dim i As
Integer, j As Integer
For i = 1 To UBound(Split(str1,
"*"))
j = InStr(1, str1,
"*")
str1 = stuff(str1, j, 1,
vbNullString)
Next
Text1.Text =
str1 '1234567
End Sub
Private Sub Command2_Click()
Dim s As String, i As
Integer
s = "ABCDEF"
Text2.Text =
vbNullString
For i = Len(s) To 3 Step
-1
s = stuff(s, 3, 1,
vbNullString)
'字符串每次从第三个字符起取一个删除
Text2.Text =
Text2.Text & "s=" & s & vbCrLf
Next
's = ABDEF
's =
ABEF
's = ABF
's = AB
End Sub
Private Sub Form_Load()
Text1.Text =
"1**2*3***4***56**7"
Text2.Text = "ABCDEF"
End
Sub
‘-------------------------------------------------------------------------------VB.net 2010 STUFF函数
Private ErrDescription As String '定义越位错误信息
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'VB6的字符串索引值是从1开始的,而VB.NET 2010的字符串索引值是从0开始的
Dim Strxx As String = Stuff("abcdefg", 5, 2, "123")
' 从索引值5的位置开始,用123替换2个字符,即:abcde123
Debug.Print(ErrDescription & "|" & Strxx)
Strxx = Stuff("abcdefg", "abcdefg".Length, 0, "123") '字符个数,正好等于字符最后位置的索引值’
' 把字符123插入到字符串末尾,即:abcdefg123
Debug.Print(ErrDescription & "|" & Strxx)
Strxx = Stuff("abcdefg", 0, 0, "123")
' 把字符123插入到字符串开头,即:123abcdefg
Debug.Print(ErrDescription & "|" & Strxx)
Strxx = Stuff("abcdefg", 0, 0, vbNullString)
'以下照原样输出即:abcdefg
Debug.Print(ErrDescription & "|" & Strxx)
Strxx = (Stuff("abcdefg", -1, -1, vbNullString))
'当第二个参数小于零时,默认等于0,当第三个参数小于零时,默认等于0 ,照原样输出。即:abcdefg
Debug.Print(ErrDescription & "|" & Strxx)
Strxx = (Stuff("abcdefg", -1, -1, "123"))
'当第二个参数小于零时,默认等于0,当第三个参数小于零时,默认等于0 ,插入到原字符串最前。即:123abcdefg
Debug.Print(ErrDescription & "|" & Strxx)
Strxx = Stuff("abcdefg", 5, -1, "郭")
'把郭插入到索引值5的位置,即:abcde郭fg,插入位置为负值时,默认为零
Debug.Print(ErrDescription & "|" & Strxx)
Strxx = Stuff("abcdefg", 5, 2, vbNullString)
' 从索引值5的位置开始删除2个字符,即:abcde
Debug.Print(ErrDescription & "|" & Strxx)
Strxx = Stuff("abcdefg", 0, 4, vbNullString)
' 当起始位置为零,当第四个参数为空时,从起始位置开始删除4个字符,即:efg
Debug.Print(ErrDescription & "|" & Strxx)
Strxx = Stuff("abcdefg", 0, 4, "123")
' 起始位置为零, 当第三个参数大于零时,且第四个参数不为空,为替换,即:123efg
Debug.Print(ErrDescription & "|" & Strxx)
Strxx = Stuff("你好,郭卫", "你好,郭卫".IndexOf(","), 1, "")
'把字符串中的郭删除 结果:"你好,卫"
Debug.Print(ErrDescription & "|" & Strxx)
'找到指定的字符,并把它删除 结果:"你好,吃了吗"
Dim str As String = "你好,郭卫,吃了吗"
str = Stuff(str, str.IndexOf("郭卫,"), "郭卫,".Length, "")
Debug.Print(ErrDescription & "|" & str)
str = Stuff(str, str.IndexOf("郭卫,") + "郭卫,".Length, 0, "早上好,")
'在"郭卫,"之后插入字符: 结果:“你好,郭卫,早上好,吃了吗”
Debug.Print(ErrDescription & "|" & str)
'------------以下是数据越位信息-----------------’
str = Stuff("abcdefg", "abcdefg".Length + 1, 0, "123")
' 把字符123插入到原字符串长度+1,将输出数据越位信息
Debug.Print(ErrDescription & "|" & str)
End sub
运行结果:
Stuff("abcdefg",5,2,"123")|abcde123
Stuff("abcdefg",7,0,"123")|abcdefg123
Stuff("abcdefg",0,0,"123")|123abcdefg
Stuff("abcdefg",0,0,"")|abcdefg
Stuff("abcdefg",-1,-1,"")|abcdefg
Stuff("abcdefg",-1,-1,"123")|123abcdefg
Stuff("abcdefg",5,-1,"郭")|abcde郭fg
Stuff("abcdefg",5,2,"")|abcde
Stuff("abcdefg",0,4,"")|efg
Stuff("abcdefg",0,4,"123")|123efg
Stuff("你好,郭卫",2,1,"")|你好郭卫
Stuff("你好,郭卫,吃了吗",3,3,"")|你好,吃了吗
Stuff("你好,吃了吗",2,0,"早上好,")|你好早上好,,吃了吗
Stuff("abcdefg",8,0,"123")|Stuff("abcdefg",8,0,"123")数据越位
如果大家对此函数感兴趣,请到http://blog.sina.com.cn/s/blog_49f7bc810102vwzm.html 下载蓝凤凰-魔灵函数库使用。