输入和获取值时出现问题。输入要求我输入借方金额和备注,如果要输入多个项目,我总是可以添加一行。我现在遇到的问题是默认分隔符是逗号,我的备注字段也包含逗号。因此,备注分割不正确。
E.g。
Row Code Amount Remarks
----------------------------------------------------------------------------------------
1 LPENALTY 200.00 Late payment penalty, 13Mar2014
2 CURFEE 1200.00 Additional items
3 PROCFEE 150.00 Processing fee undercharged, was previously charged 100.00 only
我从这些输入中获得的报告是:
LPENALTY 200.00 Late payment penalty
CURFEE 1200.00 13Mar2014
PROCFEE 150.00 Additional items
我有以下声明和作业:
Dim strCodeArr() As String
Dim strDebitArr() As String
Dim strRemarksArr() As String
strCodeArr = Split(Request("FeeCode"), ",")
strRemarksArr = Split(Request("Remarks"), ",")
strDebitArr = Split(Request("Debit"), ",")
..
..
..
..
For i = 0 To UBound(strDebitArr)
If strDebitArr(i) <> "" Then
objAcc.AccountItems(x).Code = CDec(strCodeArr(i))
objAcc.AccountItems(x).Amount = CDec(strDebitArr(i))
objAcc.AccountItems(x).Remarks = strRemarksArr(i)
x = x + 1
End If
Next
..
..
..
有人可以帮忙吗?
答案 0 :(得分:0)
如果您检查拆分中有多少项,您可以对备注中的逗号进行反应。
以下是执行此操作的基本功能,并返回包含3个项目的数组:
Private Function ParseLine(ByVal rowString As String) As String()
Dim result(2) As String
Dim parsedItems() As String
parsedItems = rowString.Split(",")
If UBound(parsedItems) = 2 Then
' No commas in remarks
result = parsedItems
Else
result(0) = parsedItems(0)
result(1) = parsedItems(1)
For index = 2 To UBound(parsedItems)
If index > 2 Then
' Prefix with comma (removed in Split)
result(2) &= ","
End If
' Append each string in split
result(2) &= parsedItems(index)
Next
End If
Return result
End Function
更好的方法是在引号中用逗号括起任何数据,以镜像CSV处理。
Late payment penalty, 13Mar2014 --> "Late payment penalty, 13Mar2014"
然后,您可以创建一个在单行上运行的CSV解析器,并可在其他应用程序中重复使用。
你可以做得更好,并使其成为字符串扩展名:
Imports System.Runtime.CompilerServices
Module MyExtensions
<Extension()>
Public Function ParseCSVLine(ByVal stringValue As String) as String
Dim result as String
' Code to parse a single line
return result
End Sub
End Module
这样每个字符串变量现在都有一个ParseCSVLine()方法:
For Each rowLine In rowItems
Console.WriteLine(rowLine.ParseCSVLine())
Next
答案 1 :(得分:0)
如果我理解你的问题,你有这个输入数据:
LPENALTY,200.00,Late payment penalty, 13Mar2014
CURFEE,1200.00,Additional items
PROCFEE,150.00,Processing fee undercharged, was previously charged 100.00 only
您想要用逗号分隔,但备注也有逗号。
所以这将是我的方法。从您的数据开始:
Dim data = New String() _
{ _
"LPENALTY,200.00,Late payment penalty, 13Mar2014", _
"CURFEE,1200.00,Additional items", _
"PROCFEE,150.00,Processing fee undercharged, was previously charged 100.00 only" _
}
然后应用此查询:
Dim query = _
From d In data _
Let ds = d.Split(","C) _
Select New With _
{ _
.Code = ds(0), _
.Amount = Decimal.Parse(ds(1)), _
.Remarks = String.Join(",", ds.Skip(2)) _
}
我得到了这样的结果: