Hi,
I'd like to separate each paragraph of a text in a list, I'm trying to find how to do it, I know it's certainly a dumb question but I'm new at coding, please be kind haha, may someone help me?
thanks a million
Hi,
I'd like to separate each paragraph of a text in a list, I'm trying to find how to do it, I know it's certainly a dumb question but I'm new at coding, please be kind haha, may someone help me?
thanks a million
Welcome to the forum.
You should show the code you tried so far, that's much easier to build a precise answer.
You want to separate in a List. SwiftUI List ?
Let's try any way.
How are paragraphs delimited ? By a newLine character (\n) ?
If so, here is a way to do it:
let myLongText = """
Hello,
this is a text with several paragraphs.
I want to split them into distinct paragraphs.
I'd like to separate each paragraph of a text in a list, I'm trying to find how to do it, I know it's certainly a dumb question but I'm new at coding, please be kind haha, may someone help me?
"""
let paragraphArray = myLongText.components(separatedBy: CharacterSet.newlines)
print(paragraphArray)
In SwiftUI code, you would use as follows:
struct ContentView: View {
let myLongText =
"""
Hello,
this is a text with several paragraphs.
I want to split them into distinct paragraphs.
I'd like to separate each paragraph of a text in a list, I'm trying to find how to do it, I know it's certainly a dumb question but I'm new at coding, please be kind haha, may someone help me?
"""
@State var paragraphArray: [String] = []
var body: some View {
VStack {
Spacer()
List(paragraphArray, id: \.self) { line in Text("\(line)") }
Spacer()
Button(action: {
paragraphArray = myLongText.components(separatedBy: CharacterSet.newlines)
}) {
Text("Split")
}
}
}
}
And get:
If that answers your question, don't forget to close the thread by marking the answer as correct.
Otherwise, please explain the problem.