以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  求助:字符串中追加分隔符的执行代码  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=78976)

--  作者:jiterp
--  发布时间:2015/12/21 22:46:00
--  求助:字符串中追加分隔符的执行代码

以下是两组数据,第一列是原数据,第二列是我想希望达到的数据,就是做一个按钮转换功能,在原数据组中插入了分隔符,求代码,谢谢!

 

转换前内容 转换后内容
1000 1000
1000000 1000,000
1000000000 1000,000,000
1000000000000 1000,000,000,000
1000000000001 1000,000,000,001
1000000000002 1000,000,000,002
1000000000003 1000,000,000,003
1000000001 1000,000,001
1000000002 1000,000,002

[此贴子已经被作者于2015/12/22 0:10:33编辑过]

--  作者:blsu33
--  发布时间:2015/12/21 23:26:00
--  
我这个比较笨
主要是这个SubString
遍历所有的行                                     for each dr as datarow in ...
每一行的字符长度与4 7 10 13 比较        if dr.isnull("转换前内容1") andalso dr("转换前内容1").length =4       ......
截取(例如13位)                               dr("转换前内容2")=dr("转换前内容1").substring(0,4) & "," &  dr("转换前内容1").substring(5,3) & "," & dr("转换前内容1").substring(8,3) & "," & dr("转换前内容1").substring(10,13)

数组 JOIN
http://www.foxtable.com/help/topics/1423.htm
红袍老师,
正则是不是可以做到呢


--  作者:大红袍
--  发布时间:2015/12/21 23:59:00
--  

呃,弄成数值列,设置单元格格式

 

http://www.foxtable.com/help/topics/1659.htm

 

如果用字符,直接格式化成千分位的也可以

 

http://www.foxtable.com/help/topics/0361.htm

 


--  作者:jiterp
--  发布时间:2015/12/22 9:00:00
--  

Select Case e.DataCol.Name
    Case "ID"
        If  e.DataRow("ID").length = 4 Then
            e.DataRow("TREE") = e.DataRow("ID").substring(0,4)
        ElseIf e.DataRow("ID").length = 7 Then
            e.DataRow("TREE") = e.DataRow("ID").substring(0,4) & "," &  e.DataRow("ID").substring(5,3)
        ElseIf e.DataRow("ID").length = 10 Then
            e.DataRow("TREE") = e.DataRow("ID").substring(0,4) & "," &  e.DataRow("ID").substring(5,3) & "," & e.DataRow("ID").substring(8,3)
        ElseIf e.DataRow("ID").length = 13 Then
            e.DataRow("TREE") = e.DataRow("ID").substring(0,4) & "," &  e.DataRow("ID").substring(5,3) & "," & e.DataRow("ID").substring(8,3) & "," & e.DataRow("ID").substring(10,3)

        End If
End Select

 

13位的设置是正确的,其他都是错误的,其他的位置提示:

 

调用的目标发生了异常。
索引和长度必须引用该字符串内的位置。
参数名: length


--  作者:Hyphen
--  发布时间:2015/12/22 9:08:00
--  

看3楼

 

e.DataRow("TREE") = Format(val(e.DataRow("ID")),"#,###")


--  作者:jiterp
--  发布时间:2015/12/22 9:21:00
--  

我是要保持前四位不变,就是说格式分段,一定是:####,###,###,###,是4333333一直下去


--  作者:大红袍
--  发布时间:2015/12/22 9:30:00
--  

说明白你的逻辑

 

Dim str As String = "1000000000003"
Dim ns As String = ""
ns = str.SubString(0, 4)
For i As Integer = 4 To str.length - 1
    If (i - 4) Mod 3 = 0 Then
        ns &= "," & str(i)
    Else
        ns &= str(i)
    End If
Next

msgbox(ns)