0

I'm having a problem getting the substring of a string which includes two double-quotes ". For example: my desString is equal to ["39013"]

I need to get: 39013

But the problem is when I'm looking for the index of " signs, Swift sees that as a normal code, not as a specific character. How can I fix this?

This is not duplicate, in this question he is asking ' sign, but I am asking about " which is similar sign when you are printing a specific value (for example: print("dup"). This means, when I am trying to find the index of " char with index(of """) there are three of them and Xcode says one of them is missing. Actually it's not missing, I am looking for it. But THAT'S THE PROBLEM.

Here is my code to find the indexes with "[" and "]".

let first = desString.index(of: "[") ?? desString.startIndex
let last = desString.index(of: " ") ?? desString.endIndex
let substring1 = desenString[first..<last]
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 2
    Seems like JSON array to me. I'd recommend then to use JSONSerialization instead of "playing with strings methods". – Larme Feb 11 '18 at 21:27
  • Something like `let jsonArray = JSONSerialization.jsonObject(withData:myString.data(encoding: .utf8, options:[]) as? [String], let value = jsonArray[0]`. Now my question is: How did you get `desString`? I fear a misuse of JSON/Strings, and then I wouldn't recommend duplicate question marked by Rashwan L. – Larme Feb 11 '18 at 21:30
  • yes, desString is an array which has datas retrieved from json data, since i can't do Label1.text = (ARRAYDATA) I'm transforming my data to string with these codes: let desenNoToString = item.DesenNo let desString = "\(desenNoToString)" –  Feb 11 '18 at 21:39
  • can you show me the link of the question that im asking the same? couldn't see it –  Feb 11 '18 at 21:41
  • 1
    This was the link: https://stackoverflow.com/questions/31725424/swift-get-string-between-2-strings-in-a-string – rob mayoff Feb 11 '18 at 21:44

3 Answers3

2

In order to put a " inside of quotation marks, you need to escape it by adding a \:

let quote = "\""

or

let string = "Put \"this\" in quotes."

BTW, this is how I'd probably attack the problem itself, but the various answers in the question discussed in the comments are fine, too:

let string = "[\"39013\"]"

let quote: Character = "\""

let num: Substring = string
    .drop(while: { $0 != quote }).dropFirst() // Drop everything until the first "
    .prefix(while: { $0 != quote }).dropLast() // Take until the next "
Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • Thanks for your solutions and your explanations. That was useful for me!! –  Feb 11 '18 at 23:29
  • I prefer this over the many other solutions (like Swift extensions), but it seems the `dropLast()` on the `.prefix` line is unnecessary. Actually, it removes the final character **before** the final quote. I think the inner `while:` already discards the quote itself. – Sahbi Jun 27 '23 at 20:41
2

Just for fun. First of all I think that it worths mentioning that you should be parsing your JSON array using JSONSerialization or using Codable protocol. If you would like to approach it doing a regular String search you can use index(of: Character) to find the first occurrence and then repeat your index search at the substring from the index after the first index found:

let string = #"["39013"]"#

if let index = string.firstIndex(of: "\""),
    case let start = string.index(after: index),
    let end = string[start...].firstIndex(of: "\"") {
    let substring = string[start..<end]
    print(substring)   //  "39013\n"
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • I am new to this json operations and trying to improve myself. While i am a newbie, i prefer regular string operations :) thanks for your helps it is really useful!!! Can you also check out my other problem please: https://stackoverflow.com/questions/48757592/searching-in-nsarray-elements –  Feb 13 '18 at 00:55
1

I like regular expressions:

let desString = "[\"39013\"]"
let regex = try! NSRegularExpression(pattern:"\"(.*)\"")
if let match = regex.firstMatch(
    in: desString, range:NSMakeRange(0,desString.utf16.count)) {
        let result = (desString as NSString).substring(with: match.range(at:1)) 
        // 39013
}
matt
  • 515,959
  • 87
  • 875
  • 1,141