2

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

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

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'

How do I do this?

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'.

Display name
  • 1,761
  • 2
  • 14
  • 16
  • Encoding a data structure into a string has its place but you'd be unwise to forgo the other structuring types for a string. So, once you get your string split you'll want to use an 'Array of Pairs' or, if all the first elements are unique, simply one dictionary. Why do I mention this in a question on 'how to split'? Because of the `StringLiteralConvertible` protocol! Implement that protocol on Dictionary (or better your own data struct) and then you can use `var data : [String:String] = "XBfdASF;....As|" – GoZoner Dec 06 '14 at 15:57

1 Answers1

3

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

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]
        }
    }
}

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

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

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("|")

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

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

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

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

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

Example:

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

println("\(id): \(code)") // Piida: bfedsSA
Community
  • 1
  • 1
AstroCB
  • 12,337
  • 20
  • 57
  • 73
  • 2
    Actually Swift *has* a split() function which you could use here (see http://stackoverflow.com/a/25229901/1187415 for an example). – Martin R Dec 06 '14 at 15:32
  • @MartinR Wow, that's interesting. I think extending String makes the syntax a bit simpler, but I'll keep that in mind. – AstroCB Dec 06 '14 at 15:34
  • Great answer. Quick question: How do I find the index of the ID in the array? i.e I have ID 'Piida' What is the short way of finding it's index in the array? – Display name Dec 06 '14 at 15:44
  • 1
    @Displayname Sure: you could use the `find()` method if you split that first array again by `;` (rather than splitting one element in it) and then use the index that `find()` returns + 1 to get the second ID. I'd look at [this question](http://stackoverflow.com/questions/24028860/how-to-find-index-of-list-item-in-apples-swift) for some other ways, as well. – AstroCB Dec 06 '14 at 15:56
  • 1
    @Displayname [Here's](https://gist.github.com/AstroCB/00fc3c906cd74af17bf4) how you would use `find()`. – AstroCB Dec 06 '14 at 16:04