1

I am new at swift language and I need to compare some strings or some characters in any string.

first question: The last version of swift doesn't allow equal operation == like this.

if "1" == item.index(item.startIndex, offsetBy: 7){
     print("ok!")
}

item is a string, it has this string "01: 06-08-2017, 13:43" (I can see its inside when I write print)

How can I check some characters in any string?

  • Well, this is gonna get frustrating pretty quickly for you, but I assure you that Strings in Swift has the current implementation for a good reason (Unicode Compatibility). Try giving [Apple's Docs](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html) a read (focus on "Accessing and Modifying a String" for now) and hopefully that'll make things a bit easier to grasp. – Marwan Alani Aug 06 '17 at 21:45

2 Answers2

1

Swift 3:

For comparing with one character, you could achieve this simply by String.CharacterView:

In Swift, every string provides a view of its contents as characters. In this view, many individual characters—for example, “é”, “김”, and “”—can be made up of multiple Unicode code points. These code points are combined by Unicode’s boundary algorithms into extended grapheme clusters, represented by the Character type. Each element of a CharacterView collection is a Character instance.

You can simply cast it as an array and check the desired character based on its index:

let item = "01: 06-08-2017, 13:43"

if "1" == Array(item.characters)[1] {
    print("matched")
}

And for more that one character, you could generate a range to substring:

let item = "01: 06-08-2017, 13:43"

// assuming we will get "06-08-2017"
let range = item.index(item.startIndex, offsetBy: 4) ..< item.index(item.startIndex, offsetBy: 14)

if "06-08-2017" == item.substring(with: range) {
    print("matched")
}

For more information about substring, I recommend to check this Q&A.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
1

The mistake in your code is that the type of item.index(item.startIndex, offsetBy: 7) is not a String, nor a Character. It's of type String.Index (in Swift 3, it's an alias of String.CharacterView.Index), which holds just a position in a String and does not represent any sort of contents in a String.

Your code in question would be rewritten as:

let item = "01: 06-08-2017, 13:43"

if item[item.index(item.startIndex, offsetBy: 7)] == "1" {
    print("ok!")
} else {
    print("invalid") //->invalid
}

You can subscript ([]) to a String with String.Index and get a Character at the position, and compare it to a Character. (In this context, "1" is treated as a Character, not String.)

Subscript for String works also with Range<String.Index>:

let startIndex = item.index(item.startIndex, offsetBy: 4)
let endIndex = item.index(startIndex, offsetBy: 10)

if item[startIndex..<endIndex] == "06-08-2017" {
    print("hit!") //->hit!
}

In Swift 4, many things around String type have changed, but the code above should work both in Swift 3 & 4.

OOPer
  • 47,149
  • 6
  • 107
  • 142