Reputation: 1871
I am using Swift 3 and have a long string containing 0's and 1's.
Ex. "1111001111111111001111111111000111000000000111000111111111100000000111100111100111111111100000000011100011111111110011110011110000000000000000000"
I am trying to split up this string into two arrays, to determine how many 1's and 0's are repeating after another.
When I call the method let oneArray = binString.components(separatedBy: "0")
and let zeroArray = binString.components(separatedBy: "1")
The returned arrays we get for our example string is:
["1111", "", "1111111111", "", "1111111111", "", "", "111", "", "", "", "", "", "", "", "", "111", "", "", "1111111111", "", "", "", "", "", "", "", "1111", "", "1111", "", "1111111111", "", "", "", "", "", "", "", "", "111", "", "", "1111111111", "", "1111", "", "1111", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
["", "", "", "", "00", "", "", "", "", "", "", "", "", "", "00", "", "", "", "", "", "", "", "", "", "000", "", "", "000000000", "", "", "000", "", "", "", "", "", "", "", "", "", "00000000", "", "", "", "00", "", "", "", "00", "", "", "", "", "", "", "", "", "", "000000000", "", "", "000", "", "", "", "", "", "", "", "", "", "00", "", "", "", "00", "", "", "", "0000000000000000000"]
And we don't understand why we can't just return arrays with just the 0's and 1's and not the empty strings as array elements. It doesn't seem that anyone else has asked this kind of question before, and we are confused because this seems to be the general method used to delimit strings from a character.
I'm wondering how to separate these strings the way we want. Seems like a weird situation here
Upvotes: 1
Views: 1203
Reputation: 154573
Every occurrence of the delimiter string will split the original string into a new string, so consecutive occurrences will result in empty strings.
You are close to what you want. Just add a filter { !$0.isEmpty }
statement to the end of each line to remove the unwanted empty strings:
let binString = "1111001111111111001111111111000111000000000111000111111111100000000111100111100111111111100000000011100011111111110011110011110000000000000000000"
let oneArray = binString.components(separatedBy: "0").filter { !$0.isEmpty }
let zeroArray = binString.components(separatedBy: "1").filter { !$0.isEmpty }
print(oneArray)
print(zeroArray)
Output:
["1111", "1111111111", "1111111111", "111", "111", "1111111111", "1111", "1111", "1111111111", "111", "1111111111", "1111", "1111"] ["00", "00", "000", "000000000", "000", "00000000", "00", "00", "000000000", "000", "00", "00", "0000000000000000000"]
Upvotes: 1
Reputation: 42143
The components function uses the character as delimiter so it will not include it in the resulting array and will treat consecutive delimiters as an empty string element.
What you could do is this:
let binary = "1111001111111111001111111111000111000000000111000111111111100000000111100111100111111111100000000011100011111111110011110011110000000000000000000"
let series = binary.replacingOccurrences(of: "10", with: "1,0")
.replacingOccurrences(of: "01", with: "0,1")
.components(separatedBy:",")
By inserting a comma between every break in 1/0 series, you get the delimiters exactly where you need them.
The series array will contain consecutive 1s and consecutive 0s in alternance. You can sort it if you want series of all 1s and all 0s grouped together.
Upvotes: 1