|
-
Dec 14th, 2010, 09:50 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Get a two dimensional array from a string
Hi I want to convert a stringbuilder to a matrix.
sb.Append("1,1,2,2,0,0,a,b,c,d,1,1,2,2,0,1,e,f,g,h,1,1,2,2,0,1,x,y,x,z");
The expected matrix is
Code:
1,1,2,2,0,0,a,b,c,d
1,1,2,2,0,1,e,f,g,h
1,1,2,2,0,1,x,y,x,z
Suppose the matrix has k rows, here k=3.
Thanks for help.
-
Dec 14th, 2010, 10:07 AM
#2
Re: Get a two dimensional array from a string
Does each row always have 10 columns?
My usual boring signature: Nothing
 
-
Dec 14th, 2010, 10:17 AM
#3
Thread Starter
Hyperactive Member
Re: Get a two dimensional array from a string
Each row has fixed columns.
-
Dec 14th, 2010, 11:55 AM
#4
Re: Get a two dimensional array from a string
May not be elegant but it works off a StringBuilder.ToString method. I did a List(Of String), you can tweak it to an array.
Code:
Private Sub Form1_Load() Handles MyBase.Load
Dim sb As New System.Text.StringBuilder
sb.Append("1,1,2,2,0,0,a,b,c,d,1,1,2,2,0,1,e,f,g,h,1,1,2,2,0,1,x,y,x,z")
Dim Result = sb.ToString.BreakApart
If Result.Count > 0 Then
For Each item In Result
Console.WriteLine("[{0}]", item)
Next
End If
End Sub
Place in code module
Code:
<Runtime.CompilerServices.Extension()> _
Public Function BreakApart(ByVal Values As String) As List(Of String)
Dim Items As New List(Of String)
Dim Count As Integer = Values.Split(",".ToCharArray).Length \ 10
If Count > 0 Then
Items.Add(Values.Substring(0, 19))
If Count > 1 Then
For x As Integer = 1 To Count - 1
Items.Add(Values.Substring(x * 20, 19))
Next
End If
End If
Return Items
End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|