Removing lines in a string into separate strings

Just getting started with swift and have c c++ knowledge. Below I have listed what I am trying to learn, the middle comments explain what I want to do. In c this is simple by using pointers or indexing, but cannot find the tools to do it in swift. I don't want to use any c or c++

var str1 = """
the apple is red
the orange is orange
the banana is yellow
"""
var str2 = ""
var str3 = ""
var str4 = ""

// how to code to take text above?
// and end up with values below?
// extract each line into a separate string...

str2 = "the apple is red"
str3 = "the orange is orange"
str4 = "the banana is yellow"

These might be useful

str1.split { $0.isNewline } will give you an array of Substrings. If you want the formal type String out, you can do str1.split { $0.isNewline }.map { String($0) }.

1 Like

First thanks for help!

Easy enough to split the sentences into an array, then using remove.(at: count) they are removed according to position (count), but don't know how the code below gets such results. If count is increased, the results are produced and it skips every other line. If not increasing count it goes through the loop printing each element in order then obvious error which is hard to understand. Increasing the count should produce correct results.

let sentences = """
the apple is red
the orange is orange
the banana is yellow
"""
var splitSentences = sentences.split { $0.isNewline }

var count = 0
while count != 2 {
let subStrings = splitSentences.remove(at: count)
print("Count = (count), the substring = (subStrings)")
//count+=1
}

results increasing count: But line is missing
Count = 0, the substring = the apple is red
Count = 1, the substring = the banana is yellow
Program ended with exit code: 0

results not increasing, changing count:
Count = 0, the substring = the apple is red
Count = 0, the substring = the orange is orange
Count = 0, the substring = the banana is yellow
Fatal error: Index out of range

remove removes the element from the array, so the array is then one element shorter.

let sentences = """
the apple is red
the orange is orange
the banana is yellow
"""
var splitSentences = sentences.split { $0.isNewline }
// “the apple is red”, “the orange is orange”, “the banana is yellow”

var count = 0
if count != 2 { // It isn’t yet.
    // splitSentences is “the apple is red”, “the orange is orange”, “the banana is yellow”
    // count is 0.
    let subStrings = splitSentences.remove(at: count)
    // subStrings: “the apple is red”
    // splitSentences: “the orange is orange”, “the banana is yellow”
    print("Count = \(count), the substring = \(subStrings)")
    // “Count = 0, the substring = the apple is red”
    count += 1
    // count: 1
}

if count != 2 { // It isn’t yet.
    // splitSentences is “the orange is orange”, “the banana is yellow”
    // count is 1.
    let subStrings = splitSentences.remove(at: count)
    // subStrings: “the banana is yellow”
    // splitSentences: “the orange is orange”
    print("Count = \(count), the substring = \(subStrings)")
    // “Count = 1, the substring = the banana is yellow”
    count += 1
    // count: 2
}

if count != 2 { // It is now.
    // Skipped.
}

What you want is any of these:

var count = 0 // Track the count ourselves.
while count != 2 {
    // Of whatever remains, always remove the *first* one:
    let subStrings = splitSentences.removeFirst()
    print("Count = \(count), the substring = \(subStrings)")
    count += 1
}
var count = 0 // Track the count ourselves.
while count != 2 {
    let subStrings = splitSentences[count] // Read without removing.
    print("Count = \(count), the substring = \(subStrings)")
    count += 1
}
// Most natural Swift:
for (position, sentence) in splitSentences.enumerated().prefix(2) {
    print("Count = \(position), the substring = \(sentence)")
}

Thanks, could tell the element was being removed and it was shorter, but could not decide how to count, the "Most Natural Swift" is nice!

Terms of Service

Privacy Policy

Cookie Policy