如何在文本和数字同时带有$符号的文本行之间进行划分?

时间:2018-12-20 03:37:43

标签: vb.net

我想将此文本数组分为3个部分

示例文本:

  

假期人身伤害$ 10,000
  财产损失$ 100,000
  第三方车辆损毁$ 10,000   市区内的财产损失为$ 100,000
  每份保单$ 20,000

它应该这样组织:

  • 第1分部-数字前的全文
  • 第2分部-数字,总是以$
  • 开头
  • 第3分部-数字后的全文

注意:
前后的单词数不一致。只有$符号是一致的。

2 个答案:

答案 0 :(得分:0)

您可以使用它,代码可能看起来很复杂,但是通过研究您会理解它

 Dim test As String = "your string here"
 'Split the text into lines and store in array
 Dim split1() As String = Split(test, vbLf)

 Dim split2() As String
 Dim split3() As String
 For x As Integer = 0 To Ubound(split1) - 1
     split2 = Split(split1(x))
     For y As Integer = 0 To Ubound(split2) - 1
         If split2(y).StartsWith("$") Then
             For z As Integer = 0 To y - 1
                 split3(0) &= split2(z)
             Next
             split3(1) = split2(y)
             If y < Ubound(split2) - 1 Then
                  For a As Integer = y + 1 To Ubound(split2) - 1
                       split3(2) &= split2(y)
                  Next
             End If
         End If
         For b As Integer = 0 To 2
            Console.WriteLine(split3(x)) 
            ' or you could use Textbox.Text += split3(x)
            ' Do whatever you want with those values here
            'split3(0)  = Text before $ sign
            'split3(1)  = Text with $ sign
            'split3(2) = Text after $ sign
         Next

     Next
Next

答案 1 :(得分:0)

正则表达式是理想的选择。我将显示一行示例,但您可以将其应用于循环中的每一行。

Set BlankCells = GetNullValues(Sheet1.Columns(2))

结果得到了这个数组:

enter image description here