|
September 18, 2000 Exploding a String Tips: September 2000
Yehuda Shiran, Ph.D.
|
|
Probably, there are many times when you want to create an array from a simple string of values. This simple function can take in a string and a delimiter (which can be anything you like) and it returns an array of those values.
We first copy the parameter
We then loop over all delimiters, and on each iteration we take the first delimited string as the next array element:
and then extracting the rest of the string to
It simply starts just after the delimiter and ends at the last index of the array. Notice the difference between Let's take an example using this function. A string could look like this:
We then take the function and use it like this:
And it returns an array that looks like this:
Its quick and easy, and you may find many uses for it. This tip has been contributed by Chris Motch.
People who read this tip also read these tips: Look for similar tips by subject: |
| Solutions | ||||||
|
||||||
|
||||||
|
||||||
|
||||||
| ||||||||||||||||||||