从字符串中删除重复的子串

时间:2016-10-18 04:45:06

标签: ios swift nsstring

我想不出一个从我的字符串中删除重复子字符串的函数。我的字符串看起来像这样:

"<bold><bold>Rutger</bold> Roger</bold> rented a <bold>testitem zero dollars</bold> from <bold>Rutger</bold>."

如果<bold>后跟另一个<bold>,我想删除第二个<bold>。删除第二个<bold>时,我还想删除后面的第一个</bold>

所以我正在寻找的输出应该是:

"<bold>Rutger Roger</bold> rented a <bold>testitem zero dollars</bold> from <bold>Rutger</bold>."

任何人都知道如何在Swift(2.2)中实现这一目标吗?

2 个答案:

答案 0 :(得分:1)

试试这个,我刚刚做了。希望这有用。

<html>
<head lang="en">
    <meta charset="UTF-8"/>
</head>
<body>
    <div id="myGrid" style="width: 500px; height: 200px;" class="ag-fresh"></div>
</body>

答案 1 :(得分:1)

我使用正则表达式编写了一个解决方案,假设标记不会出现在嵌套内容中超过1次。换句话说,它只是清除双标签而不是更多。您可以使用相同的代码和递归调用来清理任意数量的嵌套重复标记:

class Cleaner {

    var tags:Array<String> = [];

    init(tags:Array<String>) {
        self.tags = tags;
    }

    func cleanString(html:String) -> String {

        var res = html

        do {

            for tag in tags {

                let start = "<\(tag)>"
                let end = "</\(tag)>"

                let pattern = "\(start)(.*?)\(end)"

                let regex = try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.caseInsensitive)

                let matches = regex.matches(in: res, options: [], range: NSRange(location: 0, length: res.utf16.count))

                var diff = 0;
                for match in matches {

                    let outer_range = NSMakeRange(match.rangeAt(0).location - diff, match.rangeAt(0).length)
                    let inner_range = NSMakeRange(match.rangeAt(1).location - diff, match.rangeAt(1).length)
                    let node = (res as NSString).substring(with: outer_range)
                    let content = (res as NSString).substring(with: inner_range)

                    // look for the starting tag in the content of the node
                    if content.range(of: start) != nil {
                        res = (res as NSString).replacingCharacters(in: outer_range, with: content);

                        //for shifting future ranges
                        diff += (node.utf16.count - content.utf16.count)
                    }
                }
            }
        }
        catch {
            print("regex was bad!")
        }

        return res
    }
}

let cleaner = Cleaner(tags: ["bold"]);
let html = "<bold><bold>Rutger</bold> Roger</bold> rented a <bold><bold>testitem</bold> zero dollars</bold> from <bold>Rutger</bold>."

let cleaned = cleaner.cleanString(html: html)
print(cleaned)
//<bold>Rutger Roger</bold> rented a <bold>testitem zero dollars</bold> from <bold>Rutger</bold>.