0

I'm trying to find the index of a string that matches a specified value. One way to do this would be to use an instance method, but this assumes I'm working with an array; I'm working with a multi-line string literal. Much of the documentation on this topic concerns making multi-line string literals in Swift with pre-defined values.

Let's say I have the following multi-line string literal:

"""
Product 1 description
Product 1 color
Product 2 description
Product 2 color
"""

I'd like to define two variables: one to check if a global variable matches a string in the list above, and a second to act as the index. For example:

[In]  [1]: let keyword = "Product 2 description"
[In]  [2]: if keyword in multi-line string literal {
              print(index of match as integer)
           }
[Out] [3]: 2

I've managed to fulfil the first variable in my program by using the Levenshtein distance calculator to find and define a match, but I'm having trouble fulfilling the second one. I've tried splitting the list and making tuples, but each string yielded an index of 0 when I enumerated() over the list. That said, if anyone could help me with any the following, I would much appreciate the help:

  • Getting index values of the above multi-line string literal
  • Defining a count variable whose value is an integer that changes over each string iteration
  • Turning the above multi-line string literal into a list whereby each string has an index
solo
  • 743
  • 2
  • 6
  • 17
  • 1
    Turn your *multiline literal* into an `[String]` with `let arr = mll.split(separator: "\n")`. – vacawama Dec 31 '17 at 19:58
  • This will work for most strings, but not for those in my multiline literal — they contain special characters, so the code above turns each string into its own list item (i.e. `["Product 1 description"]\n["Product 1 color"]]\n["Product 2 description"]\n["Product 2 color"]`). Thanks nonetheless! – solo Dec 31 '17 at 21:40
  • @solo https://stackoverflow.com/questions/32021712/how-to-split-a-string-by-new-lines-in-swift/32021850?s=1|44.6687#32021850 – Leo Dabus Dec 31 '17 at 23:04
  • @LeoDabus I tried the three solutions that one user laid out in that thread — `componentsSeparatedByCharactersInSet`, `split`, and `enumerateLines` — but my output hasn't changed; I keep getting each string as its own list item. Maybe this is because I'm getting my array from a `node` while parsing HTML, as I keep getting errors related to `node.text!` not being a `non-function type,` and I can't parse it for indexes. – solo Dec 31 '17 at 23:40
  • componentsSeparatedByCharactersInSet is the same as the accepted answer written with Swift 2 syntax – Leo Dabus Dec 31 '17 at 23:41
  • @LeoDabus Yes, `componentsSeparatedByCharactersInSet` works for the question's [MCV](http://stackoverflow.com/help/mcve). I'm now working on getting it to work for my program. – solo Dec 31 '17 at 23:45

1 Answers1

4

The simplest solution is to create an array of the string by separating the lines

let string =
"""
Product 1 description
Product 1 color
Product 2 description
Product 2 color
"""

let list = string.components(separatedBy: CharacterSet.newlines)

Then you can get the index with

let indexOfProduct2Description = list.index(of: "Product 2 description")
vadian
  • 274,689
  • 30
  • 353
  • 361