第1部分:应用场景
华仔在《Visual Basic(vb.net)中如何对字符串进行处理,数据采集方案——取中间文本》一文中提到过,
有的时候我们可以制作自己的方法进行数据处理,
且一劳永逸,受益无穷,
今天华仔继续介绍字符串处理,
加上上次讲的,
要处理一些文字——比如网站源码,
那是绰绰有余了,今天继续分享
第2部分:方法及源代码
1、替换文本,通过前后文字进行替换,可以选择保留头尾
rem 替换文本 rem PreStr 是原文本;StartStr 是前导文本;EndStr 是结束文本;ReplaceStr 是要替换为的文本(替换后);RetainStartAndEnd 是确定是否保留头尾(及前导和结束文本),真则保留,假则不保留 Public Function ReplaceMiddleStr(ByVal PreStr As String, ByVal StartStr As String, ByVal EndStr As String, ByVal ReplaceStr As String, Optional ByVal RetainStartAndEnd As Boolean = False) As String Try If InStr(PreStr, StartStr) = 0 Then Throw New Exception("前导文本不存在") End If If InStr(PreStr, EndStr) = 0 Then Throw New Exception("结束文本不存在") End If Dim MiddleStr As String MiddleStr = Split(Split(PreStr, StartStr)(1), EndStr)(0) Dim DealResult As String If RetainStartAndEnd = True Then DealResult = Replace(PreStr, MiddleStr, ReplaceStr) Else DealResult = Replace(PreStr, StartStr + MiddleStr + EndStr, ReplaceStr) End If Return DealResult Catch ex As Exception Throw ex End Try End Function
1、替换所有中间文本,通过前后文字进行替换,可以选择保留头尾
rem 替换所有中间文本 rem PreStr 是原文本;StartStr 是前导文本;EndStr 是结束文本;ReplaceStr 是要替换为的文本(替换后);RetainStartAndEnd 是确定是否保留头尾(及前导和结束文本),真则保留,假则不保留 Public Function ReplaceAllMiddleStr(ByVal PreStr As String, ByVal StartStr As String, ByVal EndStr As String, ByVal ReplaceStr As String, Optional ByVal RetainStartAndEnd As Boolean = False) As String Try If InStr(PreStr, StartStr) = 0 Then Throw New Exception("前导文本不存在") End If If InStr(PreStr, EndStr) = 0 Then Throw New Exception("结束文本不存在") End If Dim DealArr() As String DealArr = Split(PreStr, StartStr) Dim DealArrList As ArrayList = New ArrayList(DealArr) DealArrList.RemoveAt(0) Dim ReplaceArrList As New ArrayList() For n As Integer = 0 To DealArrList.Count - 1 If InStr(DealArrList(n), EndStr) > 0 Then DealArrList(n) = Split(DealArrList(n), EndStr)(0) DealArrList(n) = Replace(DealArrList(n), ReplaceStr, "") Else DealArrList(n + 1) = DealArrList(n) + DealArrList(n + 1) ReplaceArrList.Add(DealArrList(n)) End If Next If ReplaceArrList.Count > 0 Then For n As Integer = 0 To ReplaceArrList.Count - 1 DealArrList.Remove(ReplaceArrList(n)) Next End If Dim DealResult As String = PreStr For n As Integer = 0 To DealArrList.Count - 1 If RetainStartAndEnd = True Then DealResult = Replace(DealResult, DealArrList(n), ReplaceStr) Else DealResult = Replace(DealResult, StartStr + DealArrList(n) + EndStr, ReplaceStr) End If Next Return DealResult Catch ex As Exception Throw ex End Try End Function
第3部分:结论
华仔就是通过这四个方法函数,在互联网中爬取信息
而且华仔在尝试着建立属于自己的类库,这样,就不用靠别人的,而可以一劳永逸
这是一名程序员所必须的素质,不然每次重写,效率是极滴的
© 版权声明
THE END
暂无评论内容