【问题标题】:Splitting A String On The Second Space在第二个空格上拆分字符串
【发布时间】:2017-01-06 19:23:24
【问题描述】:

我是 Vb.net 的新手,并且已经了解了如何拆分字符串并将拆分字符串的剩余部分放入这样的数组中......

        Dim str As String
        Dim strArr() As String
        Dim count As Integer
        str = "vb.net split test"
        strArr = str.Split(" ")
        For count = 0 To strArr.Length - 1
            MsgBox(strArr(count))
        Next

输出:

vb.net
split
test

现在我已经弄清楚了,我想知道是否有一种方法可以在第二个空间上进行拆分以提供输出...

vb.net split
test

感谢您的宝贵时间!

【问题讨论】:

  • 你总是可以像以前一样拆分然后字符串。两个两个加入
  • @steve 我想到了,但后来看到有机会在 java 中做到这一点,并希望那里有答案

标签: arrays string vb.net for-loop split


【解决方案1】:

你可以这样做

Dim input = "This is a Long String"
Dim splitted = input.Split(" "c)

Dim result = New List(Of String)()
For x As Integer = 0 To splitted.Length - 1 Step 2
    result.Add(String.Join(" ", splitted.Skip(x).Take(2)))
Next

【讨论】:

  • 运行代码并打印后,我收到了System.Collections.Generic.List1[System.String]`
  • 对不起,我不明白。这不可能是整个错误信息。能再详细一点吗?
  • 没关系,没有咖啡让我发疯。感谢您的帮助!
猜你喜欢
  • 2017-09-03
  • 2021-09-26
  • 2012-01-08
  • 2013-04-19
  • 1970-01-01
  • 2015-11-26
  • 1970-01-01
相关资源
最近更新 更多