在VB中使用逗号后拆分字符串 [英] Split string after x commas in VB
本文介绍了在VB中使用逗号后拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好我在这里是一个新的蜜蜂。
我需要在x个逗号之后拆分一个字符串并将其移动到list.For示例
String inputString ='1,2,3,4,5,6,7,8,9,10,11,12,13,14,..... .......'
如果我在五个逗号之后拆分,那么结果应该是
Dim outputsting As 新列表( 字符串)
outputsting [ 0 ] = 1 , 2 , 3 , 4 , 5
outputsting [ 1 ] = 6 , 7 , 8 , 9 , 10
输出[ 2 ] = 11 ,< span class =code-digit> 12 , 13 , 14 , 15
和所以 。
循环可能吗?
提前致谢。
解决方案
使用string.Split将其分解为多个部分。
然后使用Linq方法提取块:
< pre lang =VB.NET> Dim 输入作为 String = 1,2,3,4,5,6,7,8,9,10 ,11,12,13,14
Dim 部分作为 String ()= input.Split( , C)
Dim first5 As String ()= parts.ToList()。GetRange( 0 , 5 )。ToArray()
Dim second5 作为 字符串()= parts.ToList()。GetRange( 5 , 5 )。ToArray()
将字符串拆分为字符串数组,然后将数组拆分为两部分。加入单个数组以获取字符串。
Dim inputString 正如 [字符串] = 1 ,2,3,4,5,6,7,8,9,10,11,12,13,14
Dim str As String ()= inputString.Split( , C)
//前五个元素
Dim str1 作为 字符串 = 字符串 .Join(< span class =code-string> ,,str, 0 ,< span class =code-digit> 5 )
// Rest元素
Dim str2 As String = 字符串 .Join( ,,str, 5 ,str.Length - 5 )
查看morelinq
NuGet包
Dim 输入作为 String = 1,2,3,4,5,6,7,8,9,10 ,11,12,13,14
对于 每个元素 As IEnumerable( Of String )在 input.Split( , c).Batch ( 5 )
' 这里的代码..
Console.WriteLine( String .Join( ,,element))
下一步
Quote:1,2, 3,4,5
6,7,8,9,10
11,12,13,14
Hello all I am a new bee here .
I need to split a string after x number of comma and move it to list.For example
String inputString='1,2,3,4,5,6,7,8,9,10,11,12,13,14,............'
If I split after five commas then result should be
Dim outputsting As New List(Of String)
outputsting[0] = 1,2,3,4,5
outputsting[1] = 6,7,8,9,10
outputsting[2]= 11,12,13,14,15
and so on.
Is this possible in loop ?
Thanks in advance.
解决方案
Use string.Split to break it into parts.
Then use Linq methods to extract the chunks:
Dim input As String = "1,2,3,4,5,6,7,8,9,10,11,12,13,14" Dim parts As String() = input.Split(","C) Dim first5 As String() = parts.ToList().GetRange(0, 5).ToArray() Dim second5 As String() = parts.ToList().GetRange(5, 5).ToArray()
Split the string to string array and then break the array in two part. Join the individual array to get the string.
Dim inputString As [String] = "1,2,3,4,5,6,7,8,9,10,11,12,13,14" Dim str As String() = inputString.Split(","C) // First five elements Dim str1 As String = String.Join(",", str, 0, 5) // Rest element Dim str2 As String = String.Join(",", str, 5, str.Length - 5)
have a look atmorelinq
NuGet Package
Dim input As String = "1,2,3,4,5,6,7,8,9,10,11,12,13,14" For Each element As IEnumerable(Of String) In input.Split(","c).Batch(5) 'your code here.. Console.WriteLine(String.Join(", ", element)) Next
Quote:1, 2, 3, 4, 5
6, 7, 8, 9, 10
11, 12, 13, 14
这篇关于在VB中使用逗号后拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文