以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  关于混合字符数值的计算问题  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=102949)

--  作者:bootes29
--  发布时间:2017/6/28 9:43:00
--  关于混合字符数值的计算问题
各位老师早上好,
上次有个问题,就是混合字符串 成人3070,儿童3170,房差1570,每个-10元,转到另外一个表,
老师给了非常好的回答。
Dim dtb As DataTable = DataTables("表e")
Dim dtc As DataTable = DataTables("表k")
Dim ns As new List(of String)
ns.AddRange(new String() {"成人
图片点击可在新窗口打开查看此主题相关图片如下:换算.jpg
图片点击可在新窗口打开查看
","儿童","单房差"})
For Each dr As DataRow In dtb.DataRows
    Dim str As String = dr("价格")
    Dim arr() As String = str.Split(",")
    Dim n As String
    Dim p As Double
    str = ""
    For Each Ar As String In arr
        For Each n1 As String In ns
            If ar.Contains(n1) Then
                n = n1
                Exit For
            End If
        Next
        p = val(ar.Replace(n,"")) - 10
        str &= n & p & ","
    Next
    Dim ndr As DataRow = dtc.AddNew
    ndr("价格")=str
Next

--  作者:bootes29
--  发布时间:2017/6/28 9:45:00
--  
现在“价格”里面要改的变成了,“1-10号” “11-31号”
我试着是这样做的
ns.AddRange(new String() {"1-11号","儿童","单房差"})
For Each dr As DataRow In dtb.DataRows



这样就会报错,说是值不能为空,这是为何,各位老师?

--  作者:bootes29
--  发布时间:2017/6/28 9:48:00
--  
ns.AddRange(new String() {"号","儿童","单房差"})
我改成这样,同样的错误

--  作者:bootes29
--  发布时间:2017/6/28 10:43:00
--  
有的色 老师  请百忙之中指点下迷津!
--  作者:有点色
--  发布时间:2017/6/28 11:21:00
--  
\'Dim dtb As DataTable = DataTables("表e")
\'Dim dtc As DataTable = DataTables("表k")
Dim ns As new List(of String)
ns.AddRange(new String() {"儿童","单房差","1-10号"})
\'For Each dr As DataRow In dtb.DataRows
Dim str As String = "成人999,1-10号123,儿童3170,单房差1570" \'dr("价格")
Dim arr() As String = str.Split(",")
Dim n As String
Dim p As Double
str = ""
For Each Ar As String In arr
    For Each n1 As String In ns
        If ar.Contains(n1) Then
            n = n1
            Exit For
        End If
    Next
    If n > "" Then
        p = val(ar.Replace(n,"")) - 10
        str &= n & p & ","
    Else
        str &= ar & ","
    End If

Next
msgbox(str.trim(","))
\'Dim ndr As DataRow = dtc.AddNew
\'ndr("价格")=str
\'Next

--  作者:bootes29
--  发布时间:2017/6/28 13:06:00
--  
刚回来,感谢老师的指导。
实际上我想 让有价格的地方 减10,其他文字仍然保留
目前代码存在两个问题
1、感觉表格数据转换上有点问题,如图。
2、保留其他备注文字

这是我的代码:
Dim dtb As DataTable = DataTables("表e")
Dim dtc As DataTable = DataTables("表k")
Dim ns As new List(of String)
ns.AddRange(new String() {"儿童","单房差","1-10号","11-30号","1-13号"})
For Each dr As DataRow In dtb.DataRows
Dim str As String = dr("价格")
Dim arr() As String = str.Split(",")
Dim n As String
Dim p As Double
str = ""
For Each Ar As String In arr
    For Each n1 As String In ns
        If ar.Contains(n1) Then
            n = n1
            Exit For
        End If
    Next
    If n > "" Then
        p = val(ar.Replace(n,"")) - 10
        str &= n & p & ","
    Else
        str &= ar & ","
    End If
Next
\'msgbox(str.trim(","))
Dim ndr As DataRow = dtc.AddNew
\'ndr("价格")=str.trim(",")
ndr("价格")=str
Next

--  作者:bootes29
--  发布时间:2017/6/28 13:07:00
--  
从这张图来看,有些乱码的感觉,右边有原来的数据,左边是代码 转来的
--  作者:bootes29
--  发布时间:2017/6/28 13:07:00
--  

图片点击可在新窗口打开查看此主题相关图片如下:未标题-3 拷贝.jpg
图片点击可在新窗口打开查看

--  作者:有点蓝
--  发布时间:2017/6/28 15:16:00
--  
这种数据看似有规律,其实没有规律,只会做死自己。

既然是要计算的,为什么不把文字和价格分离存储。在出报表的时候在合并显示好了

--  作者:有点色
--  发布时间:2017/6/28 15:38:00
--  

参考代码,如果有问题,单独贴出测试代码和数据,说明哪里有问题。

 

\'Dim dtb As DataTable = DataTables("表e")
\'Dim dtc As DataTable = DataTables("表k")
Dim ns As new List(of String)
ns.AddRange(new String() {"儿童","单房差","1-10号"})
\'For Each dr As DataRow In dtb.DataRows
Dim str As String = "成人,1-10号,123(国线),儿童3170,单房差1570" \'dr("价格")
Dim arr() As String = str.Split(",")
str = ""
For Each Ar As String In arr
    Dim n As String = ""
    Dim p As Double = 0
    For Each n1 As String In ns
        If ar.Contains(n1) Then
            n = n1
            Exit For
        End If
    Next
    If n > "" Then
        p = val(ar.Replace(n,"")) - 10
        If p = -10
            str &= ar & ","
        Else
            str &= n & p & ","
        End If
    Else
        str &= ar & ","
    End If
Next
msgbox(str.trim(","))
\'Dim ndr As DataRow = dtc.AddNew
\'ndr("价格")=str
\'Next