字串切割及取值问题 使用vb.net

这个是我之前有提过的问题,这是有点变化

字串为:"2,1,4,Beijing, 2018-07-05 17:26:45, name北京亚丁湾商务酒店, 40101215, 朝阳区\n民族园路9号"

一字串,其前面有3个数值,或是都都没有
我要将结果为
A值2
B值1
C值4
D值:Beijing, 2018-07-05 17:26:45, nam..........
也有可能A、B、C值均没有:Beijing, 2018-07-05 17:26:45, name北京亚丁....
那ABC值都传出0就好了

谢谢

dim s as string = "2,1,4,Beijing, 2018-07-05 17:26:45, name北京亚丁湾商务酒店, 40101215, 朝阳区\n民族园路9号"
if regexp.ismatch(s, "^\d+\,\d+\,\d+,") then
a = val(split(s, ",")(0))
b = val(split(s, ",")(1))
c = val(split(s, ",")(2))
d = string.Join(",", split(s, ",").skip(3))
else
a = 0
b = 0
c = 0
d = s
end if

手敲的,你试试

dim str as string="2,1,4,Beijing, 2018-07-05 17:26:45, name北京亚丁湾商务酒店, 40101215, 朝阳区\n民族园路9号"
dim a() as string
dim A,B,C,D as string

a=split(str,",")
if ubound(a)>=7 then
for i=ubound(a) to 0 step -1
if i>=3 then
D=a(i) & D
elseif i=2 then
C=a(i)
elseif i=1 then
B=a(i)
elseif i=0 then
A=a(i)
end if
next
else
D=str
A=0
B=0
C=0

end if

    Dim str As String = "2,1,4,Beijing, 2018-07-05 17:26:45, name北京亚丁湾商务酒店, 40101215, 朝阳区\n民族园路9号"
    Dim a, b, c As Integer
    Dim d As String
    Dim aStr() As String = Split(str, ",")
    If (IsNumeric(aStr(0))) Then
        a = CInt(aStr(0))
        b = CInt(aStr(1))
        c = CInt(aStr(2))
        d = Mid(str, InStr(str, aStr(3)))
    Else
        a = 0
        b = 0
        c = 0
        d = str
    End If
    Console.WriteLine("a={0}", a)
    Console.WriteLine("b={0}", b)
    Console.WriteLine("c={0}", c)
    Console.WriteLine("d={0}", d)
    Console.ReadKey()

to Caozhy 有点小问题

图片说明

Dim str As String = "2,1,4,Beijing, 2018-07-05 17:26:45, name北京亚丁湾商务酒店, 40101215, 朝阳区\n民族园路9号"
Dim a, b, c As Integer
Dim d As String
Dim aStr() As String = Split(str, ",")
If (IsNumeric(aStr(0))) Then
a = CInt(aStr(0))
b = CInt(aStr(1))
c = CInt(aStr(2))
d = Mid(str, InStr(str, aStr(3)))
Else
a = 0
b = 0
c = 0
d = str
End If
Console.WriteLine("a={0}", a)
Console.WriteLine("b={0}", b)
Console.WriteLine("c={0}", c)
Console.WriteLine("d={0}", d)
Console.ReadKey()

可以用split分割,split(字符串,分隔符)