0

If I am using swift, how can I get my program to read through a string of letters to identify something? For example, lets say var aa = "Apple". If I have this string of letters: "aghdbgkldhgaabdfhjk", what code can I use for the program to read through it, identify "aa", and print "apple"? I appreciate any help, thank you!

alessandrio
  • 4,282
  • 2
  • 29
  • 40
  • This is very confusing. Can you please try to clarify what you are trying to do. – rmaddy May 25 '17 at 04:04
  • Yes, sorry it does seem confusing the way I worded it. So lets say a user types a random series of letters. If this series of letters has two "a" right after one another, such as "aa" within that series of letters, I want the console to print "apple". Almost as if this program is reading through the series of letters and identifying "aa". Is this possible? – ScottSchumacker May 25 '17 at 04:13
  • But what is mapping "aa" to "apple"? How does your app know to treat "aa" special as opposed to any other letters in the string the user types? – rmaddy May 25 '17 at 04:15
  • Yea that is what I am trying to figure out. Basically if a user types in a series of letters, presses a button, I want a new view controller to come up saying apple. Basically decoding and identifying "apple" within that series of letters. I was wondering if I could create a variable such as var aa = "apple". I am semi-new to swift and was wondering if there was some type of function within swift that can do this; Read through the letters and look for the match "aa". – ScottSchumacker May 25 '17 at 04:25
  • You could create a dictionary that maps "aa" to "apple". You certainly wouldn't do anything based on variable names. – rmaddy May 25 '17 at 04:27
  • Ok, great! Thank you – ScottSchumacker May 25 '17 at 04:33
  • String has a method name **contains**. Maybe it can help you – Hieu Dinh May 25 '17 at 04:47

1 Answers1

1

you can check if your String contains "aa" in this link then return 'Apple'

e.g:

var string = "aghdbgkldhgaabdfhjk"

if string.range(of: "aa", options: String.CompareOptions.diacriticInsensitive, range: nil, locale: nil) != nil {
    print("apple")
}
Jože Ws
  • 1,754
  • 17
  • 12