0

I want to check a String to see if it contains a single letter. Here's the code:

func CheckLetter(letter:String,word:String) -> String{

    var checkFlag = false
    var tempWord = [""]

    for n in 0...(word.count-1){
        if tempWord[n] == letter[0]{

        }
    }

}

And the error is: 'subscript(_:)' is unavailable: cannot subscript String with an Int, use a String.Index instead.

SinaSB
  • 175
  • 2
  • 11
  • This is not the error you are getting. You are getting that `"Binary operator '==' cannot be applied to operands of type 'String' and 'Character'"`. The error you are describing will show if you fix the binary operator error. What is your goal? Are you trying to check if a string contains a character? – Leo Dabus Apr 21 '20 at 21:58
  • Does this answer your question? https://stackoverflow.com/questions/24034043/how-do-i-check-if-a-string-contains-another-string-in-swift – NicolasElPapu Apr 21 '20 at 23:41
  • FYI, method and variable names should be lowercase. Only class names are uppercase. – LinusGeffarth Apr 22 '20 at 12:19

2 Answers2

1

Checkout Swift String Cheat Sheet, by Keith Harrison

You can use:

func checkLetter(letter: String, word: String) -> String {

    return word.contains(letter).description
}
Roi Zakai
  • 252
  • 3
  • 3
  • You answer is the closest one but there is something I forgot to tell, that if the word contains the letter, I want to know what is the index of that letter? – SinaSB Apr 26 '20 at 08:37
  • @SinaSB I added a link to an article you will find useful. – Roi Zakai Apr 27 '20 at 08:39
0

try this !!!

func CheckLetter(letter:String,word:String) -> Bool{
        var checkFlag = false
        if word.contains(letter) {
            checkFlag = true
        }
        return checkFlag
    }
Yash Gotecha
  • 144
  • 3