bloodless2010
bloodless2010

Reputation: 349

Split each character in a string into an array so long as theres a space?

I'm working on a little side project and I need to turn a string like this: ab c de f g

to an array like so: ab,c,de,f,g I have tried using

     Dim string As String = "ab c de f g" 
     Dim charArray() As Char = string.ToCharArray

but that splits EVERY character into the array (a,b, ,c, ,d,e, ,f, ,g), how can I make it only split between the spaces?

Upvotes: 0

Views: 1625

Answers (2)

Steve
Steve

Reputation: 216293

It seems a job for String.Split

 Dim source As String = "ab c de f g" 
 Dim result AS String() = source.Split(" "c)  
 For Each s in result
      Console.WriteLine(s)
 Next

Upvotes: 1

user3426148
user3426148

Reputation: 3

I would read the entire string and use the space as delimeter. Means: create a pointer to the string and skip till you find character, then use strncpy, to copy the substring you need. repeat it while(*ptr).

Upvotes: 0

Related Questions