string - Swift rangeOfString index 0 or 1 -


i have array of names , want filter out has first or second letter "e", following array

var usernames = ["john","tom","ed","ben","albert"] 

the outputshould ["ed","ben"]. albert should not included position not 1st or second

let filterednames = username.filter { (inputstr) -> bool in       if let inputrang =  inputstr.range(of: "e", options: .caseinsensitive, range: nil, locale: nil)      {         //how check position 0 or 1 here          return true     }     return false } 

let result = usernames.filter { $0.lowercased().characters.prefix(2).contains("e") }  print(result) // ["ed", "ben"] 

this should it.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -