字串切割及取值问题 使用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就好了

谢谢

net

6个回答

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

手敲的,你试试

qq_40866415
西瓜123456789 substring
大约 2 年之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复weixin_40187983: 你是vs2008?那么d = string.Join(",", split(s, ",").skip(3).toarray())
大约 2 年之前 回复
weixin_40187983
weixin_40187983 请帮我看一下 无法将类型为“<SkipIterator>d__4d`1[System.String]”的对象强制转换为类型“System.String[]”是什么问题呢
大约 2 年之前 回复

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(字符串,分隔符)

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐