L
L
Lici2015-03-15 15:57:13
Objective-C
Lici, 2015-03-15 15:57:13

How to parse a complex string in Swift?

There is a line like:

{тут может быть текст} и тут может быть текст {и тут} {и тут} {и просто пробелы между ячейками}

And finally pack it into an array:
"тут может быть текст"
" и тут может быть текст "
"и тут"
" "
"и тут"
" "
"и просто пробелы между ячейками"

Roughly speaking, everything inside the brackets and everything outside the brackets into array elements
var firstBlockStruct = Array<String>()
I'm already starting to think about replacing the characters } with { and then
for c in incomeText {
    if (c == "}") {c = "{"}
}

firstBlockStruct = split(incomeText) {$0 == "{"}

But I don't know how to replace a certain character in a string with another one.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Ruchkin, 2015-03-15
@Lici

I don't know swift, but taking into account your "and then" I suggest:
split(incomeText) {$0 == "{" || $0 == "}"}

C
capitan_of_the_mr, 2015-08-18
@capitan_of_the_mr

func replace (var str:String, var replace:Character, var replaced:Character) -> String{
        var str1 = ""
        println(str)
        for Character in str{
            var i = Character
            if i == replace{
                i = replaced
            }
            
            str1.append(i)
        }
    
    println(str1)
    return str1
    } // Функция замены символов в строке

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question