Swift-在字符串的两个位置之间查找子字符串 [英] Swift - Finding a substring between two locations in a string

查看:422
本文介绍了Swift-在字符串的两个位置之间查找子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样格式化的字符串:"XbfdASF; FBACasc | Piida; bfedsSA | XbbnSF; vsdfAs |"

I have a string that is formatted like this: "XbfdASF;FBACasc|Piida;bfedsSA|XbbnSF;vsdfAs|"

通常是其ID; ID |然后重复.

Basiclly its an ID;ID| and then it repeats.

我有第一个ID,我需要找到它的伙伴.示例:我有'Piida',我需要在';'之后找到其后的字符串.这是"bfedsSA"

I have the first ID and I need to find it's partner Example: I have 'Piida' and I need to find the String that follows it after the ';' which is 'bfedsSA'

我该怎么做?

我遇到的问题是ID的长度是动态的,因此我需要获取'|'的索引在我拥有的ID是'Piida'之后,然后获取这些索引之间的字符串,在这种情况下,该字符串应为'bfedsSA'.

The problem I am having is that the length of the IDs is dynamic so I need to get the index of '|' after the ID I have which is 'Piida' and then get the string that is between these indexes which in this case should be 'bfedsSA'.

推荐答案

执行此操作的方法有很多,但最简单的方法是使用分隔符将字符串拆分为数组.

There are many ways to do this, but the easiest is to split the string into an array using a separator.

如果您知道JavaScript,它等效于.split()字符串方法; Swift 具有此功能,但是如您所见,它可能会有些混乱.您可以像这样扩展字符串 使其更加简单.为了完整起见,我将其包括在这里:

If you know JavaScript, it's the equivalent of the .split() string method; Swift does have this functionality, but as you see there, it can get a little messy. You can extend String like this to make it a bit simpler. For completeness, I'll include it here:

import Foundation

extension String {
    public func split(separator: String) -> [String] {
        if separator.isEmpty {
            return map(self) { String($0) }
        }
        if var pre = self.rangeOfString(separator) {
            var parts = [self.substringToIndex(pre.startIndex)]
            while let rng = self.rangeOfString(separator, range: pre.endIndex..<endIndex) {
                parts.append(self.substringWithRange(pre.endIndex..<rng.startIndex))
                pre = rng
            }
            parts.append(self.substringWithRange(pre.endIndex..<endIndex))
            return parts
        } else {
            return [self]
        }
    }
}

现在,您可以在这样的字符串上调用.split():

Now, you can call .split() on strings like this:

"test".split("e") // ["t", "st"]

因此,您首先要做的是使用分隔符(|)将ID字符串分成多个段,因为这是分隔ID的方式:

So, what you should do first is split up your ID string into segments by your separator, which will be |, because that's how your IDs are separated:

let ids: [String] = "XbfdASF;FBACasc|Piida;bfedsSA|XbbnSF;vsdfAs|".split("|")

现在,您的ID的字符串数组如下所示:

Now, you have a String array of your IDs that would look like this:

["XbfdASF;FBACasc", "Piida;bfedsSA", "XbbnSF;vsdfAs"]

您的ID的格式为ID;VALUE,因此您可以像这样再次拆分它们:

Your IDs are in the format ID;VALUE, so you can split them again like this:

let pair: [String] = ids[anyIndex].split(";") // ["ID", "VALUE"]

您可以访问该数组的索引0处的ID和索引1处的值.

You can access the ID at index 0 of that array and the value at index 1.

示例:

let id: String = ids[1].split(";")[0]
let code: String = ids[1].split(";")[1]

println("\(id): \(code)") // Piida: bfedsSA

这篇关于Swift-在字符串的两个位置之间查找子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆