Converting string to Bool and testing for IF statement in Swift - swift

So I am trying to take thee first index of a string (fullName) and test it to see if it matches all vowels lower and uppercased... for some reason when I use the .startIndex I test one letter at a time with a If statement.
Ia there a way to test all vowels at once ()I tried || in between each and it gave me the error "cannot convert string to Bool". Appreciate any help.
func lyricsForName(lyricsTemplate: String, fullName: String) -> String {
let shortName = shortNameFromName(name: fullName)
let index = fullName[fullName.startIndex]
if index== ("a","A"){
let lyrics = lyricsTemplate
.replacingOccurrences(of:"<FULL_NAME>", with: fullName)
.replacingOccurrences (of:"<SHORT_NAME>", with: fullName )
return lyrics
}else{
let lyrics = lyricsTemplate
.replacingOccurrences(of:"<FULL_NAME>", with: fullName)
.replacingOccurrences (of:"<SHORT_NAME>", with: shortName )
return lyrics

You can do it as previously answered, but this is a more Swifty way using the available APIs:
extension String {
var firstThreeLettersAreVowels: Bool {
guard characters.count >= 3 else {
return false
}
let firstThreeLetters = substring(to: index(startIndex, offsetBy: 3))
let isAllVowels = CharacterSet(charactersIn: firstThreeLetters).isSubset(of: CharacterSet.vowels)
return isAllVowels
}
var isLower: Bool {
return CharacterSet(charactersIn: self).isSubset(of: CharacterSet.lowercaseLetters)
}
var isUpper: Bool {
return CharacterSet(charactersIn: self).isDisjoint(with: CharacterSet.lowercaseLetters)
}
}
extension CharacterSet {
static var vowels: CharacterSet {
return CharacterSet(charactersIn: "AEIOUYaeiouy")
}
}
Or the one liner (without length check) is
extension String {
var firstThreeLettersAreVowels: Bool {
return CharacterSet(charactersIn: substring(to: index(startIndex, offsetBy: 3)))
.isSubset(of: CharacterSet(charactersIn: "AEIOUYaeiouy"))
}
}
This allows you rely on existing APIs rather than trying to do the work yourself, and it should be pretty fast.

Here is one way to do it. You can use an array of vowels and then test if the array contains the first character:
let fullName = "Albert"
let vowels = "aeiouAEIOU".characters
if let first = fullName.characters.first, vowels.contains(first) {
print("\(fullName) starts with a vowel")
}
Albert starts with a vowel
Note: Using fullName.characters.first is safer than fullName[fullName.startIndex] because the latter will crash for an empty String.

all kudos please to PEEJWEEJ answer
the characterset was his idea :)
i made it only more general
import Foundation
extension String {
func isFirstLetters(count: Int, of characterSet: CharacterSet) -> Bool {
guard characters.count >= count else {
return false
}
let firstLetters = substring(to: index(startIndex, offsetBy: count))
let isInSubset = CharacterSet(charactersIn: firstLetters).isSubset(of: characterSet
)
return isInSubset
}
}
extension CharacterSet {
static var vowels: CharacterSet {
return CharacterSet(charactersIn: "AEIOUYaeiouy")
}
}
some Test cases:
"abcDEf".isFirstLetters(count: 3, of: .vowels) // false
"aioDEf".isFirstLetters(count: 3, of: .vowels) // true
"abcDEf".isFirstLetters(count: 1, of: .vowels) // true
"bbcDEf".isFirstLetters(count: 1, of: .vowels) // false
"ibcDEf".isFirstLetters(count: 1, of: .vowels) // true
"ABcdef".isFirstLetters(count: 3, of: .uppercaseLetters) // false
"ABDdef".isFirstLetters(count: 3, of: .uppercaseLetters) // true
"abBABcdef".isFirstLetters(count: 3, of: .lowercaseLetters) // false
"abbABDdef".isFirstLetters(count: 3, of: .lowercaseLetters) // true
"ABD".isFirstLetters(count: 3, of: CharacterSet(charactersIn: "A"..."C")) // false
"ABD".isFirstLetters(count: 3, of: CharacterSet(charactersIn: "A"..."D")) // true
debug info:
extension CharacterSet {
var characters: [Character] {
var result: [Character] = []
for plane: UInt8 in 0...16 where self.hasMember(inPlane: plane) {
for unicode in UInt32(plane) << 16 ..< UInt32(plane + 1) << 16 {
if let uniChar = UnicodeScalar(unicode), self.contains(uniChar) {
result.append(Character(uniChar))
}
}
}
return result
}
}
print(CharacterSet.uppercaseLetters.description)
//<Foundation._SwiftNSCharacterSet: 0x6000000271a0>
print(String(CharacterSet.vowels.characters))
//AEIOUYaeiouy
print(CharacterSet.vowels.characters)
// ["A", "E", "I", "O", "U", "Y", "a", "e", "i", "o", "u", "y"]

Related

substring(with:)' is deprecated: Please use String slicing subscript [duplicate]

I have the following simple code written in Swift 3:
let str = "Hello, playground"
let index = str.index(of: ",")!
let newStr = str.substring(to: index)
From Xcode 9 beta 5, I get the following warning:
'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator.
How can this slicing subscript with partial range from be used in Swift 4?
You should leave one side empty, hence the name "partial range".
let newStr = str[..<index]
The same stands for partial range from operators, just leave the other side empty:
let newStr = str[index...]
Keep in mind that these range operators return a Substring. If you want to convert it to a string, use String's initialization function:
let newStr = String(str[..<index])
You can read more about the new substrings here.
Convert Substring (Swift 3) to String Slicing (Swift 4)
Examples In Swift 3, 4:
let newStr = str.substring(to: index) // Swift 3
let newStr = String(str[..<index]) // Swift 4
let newStr = str.substring(from: index) // Swift 3
let newStr = String(str[index...]) // Swift 4
let range = firstIndex..<secondIndex // If you have a range
let newStr = = str.substring(with: range) // Swift 3
let newStr = String(str[range]) // Swift 4
Swift 5, 4
Usage
let text = "Hello world"
text[0] // H
text[...3] // "Hell"
text[6..<text.count] // world
text[NSRange(location: 6, length: 3)] // wor
Code
import Foundation
public extension String {
subscript(value: Int) -> Character {
self[index(at: value)]
}
}
public extension String {
subscript(value: NSRange) -> Substring {
self[value.lowerBound..<value.upperBound]
}
}
public extension String {
subscript(value: CountableClosedRange<Int>) -> Substring {
self[index(at: value.lowerBound)...index(at: value.upperBound)]
}
subscript(value: CountableRange<Int>) -> Substring {
self[index(at: value.lowerBound)..<index(at: value.upperBound)]
}
subscript(value: PartialRangeUpTo<Int>) -> Substring {
self[..<index(at: value.upperBound)]
}
subscript(value: PartialRangeThrough<Int>) -> Substring {
self[...index(at: value.upperBound)]
}
subscript(value: PartialRangeFrom<Int>) -> Substring {
self[index(at: value.lowerBound)...]
}
}
private extension String {
func index(at offset: Int) -> String.Index {
index(startIndex, offsetBy: offset)
}
}
Shorter in Swift 4/5:
let string = "123456"
let firstThree = String(string.prefix(3)) //"123"
let lastThree = String(string.suffix(3)) //"456"
Swift5
(Java's substring method):
extension String {
func subString(from: Int, to: Int) -> String {
let startIndex = self.index(self.startIndex, offsetBy: from)
let endIndex = self.index(self.startIndex, offsetBy: to)
return String(self[startIndex..<endIndex])
}
}
Usage:
var str = "Hello, Nick Michaels"
print(str.subString(from:7,to:20))
// print Nick Michaels
The conversion of your code to Swift 4 can also be done this way:
let str = "Hello, playground"
let index = str.index(of: ",")!
let substr = str.prefix(upTo: index)
You can use the code below to have a new string:
let newString = String(str.prefix(upTo: index))
substring(from: index) Converted to [index...]
Check the sample
let text = "1234567890"
let index = text.index(text.startIndex, offsetBy: 3)
text.substring(from: index) // "4567890" [Swift 3]
String(text[index...]) // "4567890" [Swift 4]
Some useful extensions:
extension String {
func substring(from: Int, to: Int) -> String {
let start = index(startIndex, offsetBy: from)
let end = index(start, offsetBy: to - from)
return String(self[start ..< end])
}
func substring(range: NSRange) -> String {
return substring(from: range.lowerBound, to: range.upperBound)
}
}
Example of uppercasedFirstCharacter convenience property in Swift3 and Swift4.
Property uppercasedFirstCharacterNew demonstrates how to use String slicing subscript in Swift4.
extension String {
public var uppercasedFirstCharacterOld: String {
if characters.count > 0 {
let splitIndex = index(after: startIndex)
let firstCharacter = substring(to: splitIndex).uppercased()
let sentence = substring(from: splitIndex)
return firstCharacter + sentence
} else {
return self
}
}
public var uppercasedFirstCharacterNew: String {
if characters.count > 0 {
let splitIndex = index(after: startIndex)
let firstCharacter = self[..<splitIndex].uppercased()
let sentence = self[splitIndex...]
return firstCharacter + sentence
} else {
return self
}
}
}
let lorem = "lorem".uppercasedFirstCharacterOld
print(lorem) // Prints "Lorem"
let ipsum = "ipsum".uppercasedFirstCharacterNew
print(ipsum) // Prints "Ipsum"
You can create your custom subString method using extension to class String as below:
extension String {
func subString(startIndex: Int, endIndex: Int) -> String {
let end = (endIndex - self.count) + 1
let indexStartOfText = self.index(self.startIndex, offsetBy: startIndex)
let indexEndOfText = self.index(self.endIndex, offsetBy: end)
let substring = self[indexStartOfText..<indexEndOfText]
return String(substring)
}
}
Creating SubString (prefix and suffix) from String using Swift 4:
let str : String = "ilike"
for i in 0...str.count {
let index = str.index(str.startIndex, offsetBy: i) // String.Index
let prefix = str[..<index] // String.SubSequence
let suffix = str[index...] // String.SubSequence
print("prefix \(prefix), suffix : \(suffix)")
}
Output
prefix , suffix : ilike
prefix i, suffix : like
prefix il, suffix : ike
prefix ili, suffix : ke
prefix ilik, suffix : e
prefix ilike, suffix :
If you want to generate a substring between 2 indices , use :
let substring1 = string[startIndex...endIndex] // including endIndex
let subString2 = string[startIndex..<endIndex] // excluding endIndex
I have written a string extension for replacement of 'String: subString:'
extension String {
func sliceByCharacter(from: Character, to: Character) -> String? {
let fromIndex = self.index(self.index(of: from)!, offsetBy: 1)
let toIndex = self.index(self.index(of: to)!, offsetBy: -1)
return String(self[fromIndex...toIndex])
}
func sliceByString(from:String, to:String) -> String? {
//From - startIndex
var range = self.range(of: from)
let subString = String(self[range!.upperBound...])
//To - endIndex
range = subString.range(of: to)
return String(subString[..<range!.lowerBound])
}
}
Usage : "Date(1511508780012+0530)".sliceByString(from: "(", to: "+")
Example Result : "1511508780012"
PS: Optionals are forced to unwrap. Please add Type safety check wherever necessary.
If you are trying to just get a substring up to a specific character, you don't need to find the index first, you can just use the prefix(while:) method
let str = "Hello, playground"
let subString = str.prefix { $0 != "," } // "Hello" as a String.SubSequence
When programming I often have strings with just plain A-Za-z and 0-9. No need for difficult Index actions. This extension is based on the plain old left / mid / right functions.
extension String {
// LEFT
// Returns the specified number of chars from the left of the string
// let str = "Hello"
// print(str.left(3)) // Hel
func left(_ to: Int) -> String {
return "\(self[..<self.index(startIndex, offsetBy: to)])"
}
// RIGHT
// Returns the specified number of chars from the right of the string
// let str = "Hello"
// print(str.left(3)) // llo
func right(_ from: Int) -> String {
return "\(self[self.index(startIndex, offsetBy: self.length-from)...])"
}
// MID
// Returns the specified number of chars from the startpoint of the string
// let str = "Hello"
// print(str.left(2,amount: 2)) // ll
func mid(_ from: Int, amount: Int) -> String {
let x = "\(self[self.index(startIndex, offsetBy: from)...])"
return x.left(amount)
}
}
Hope this will help little more :-
var string = "123456789"
If you want a substring after some particular index.
var indexStart = string.index(after: string.startIndex )// you can use any index in place of startIndex
var strIndexStart = String (string[indexStart...])//23456789
If you want a substring after removing some string at the end.
var indexEnd = string.index(before: string.endIndex)
var strIndexEnd = String (string[..<indexEnd])//12345678
you can also create indexes with the following code :-
var indexWithOffset = string.index(string.startIndex, offsetBy: 4)
with this method you can get specific range of string.you need to pass start index and after that total number of characters you want.
extension String{
func substring(fromIndex : Int,count : Int) -> String{
let startIndex = self.index(self.startIndex, offsetBy: fromIndex)
let endIndex = self.index(self.startIndex, offsetBy: fromIndex + count)
let range = startIndex..<endIndex
return String(self[range])
}
}
This is my solution, no warning, no errors, but perfect
let redStr: String = String(trimmStr[String.Index.init(encodedOffset: 0)..<String.Index.init(encodedOffset: 2)])
let greenStr: String = String(trimmStr[String.Index.init(encodedOffset: 3)..<String.Index.init(encodedOffset: 4)])
let blueStr: String = String(trimmStr[String.Index.init(encodedOffset: 5)..<String.Index.init(encodedOffset: 6)])
var str = "Hello, playground"
let indexcut = str.firstIndex(of: ",")
print(String(str[..<indexcut!]))
print(String(str[indexcut!...]))
You can try in this way and will get proper results.
the simples way that I use is :
String(Array(str)[2...4])
Swift 4, 5, 5+
Substring from Last
let str = "Hello World"
let removeFirstSix = String(str.dropFirst(6))
print(removeFirstSix) //World
Substring from First
let removeLastSix = String(str.dropLast(6))
print(removeLastSix) //Hello
Hope it would be helpful.
extension String {
func getSubString(_ char: Character) -> String {
var subString = ""
for eachChar in self {
if eachChar == char {
return subString
} else {
subString += String(eachChar)
}
}
return subString
}
}
let str: String = "Hello, playground"
print(str.getSubString(","))

Swift 4 How to find Index of a particular substring in String [duplicate]

I'm used to do this in JavaScript:
var domains = "abcde".substring(0, "abcde".indexOf("cd")) // Returns "ab"
Swift doesn't have this function, how to do something similar?
edit/update:
Xcode 11.4 • Swift 5.2 or later
import Foundation
extension StringProtocol {
func index<S: StringProtocol>(of string: S, options: String.CompareOptions = []) -> Index? {
range(of: string, options: options)?.lowerBound
}
func endIndex<S: StringProtocol>(of string: S, options: String.CompareOptions = []) -> Index? {
range(of: string, options: options)?.upperBound
}
func indices<S: StringProtocol>(of string: S, options: String.CompareOptions = []) -> [Index] {
ranges(of: string, options: options).map(\.lowerBound)
}
func ranges<S: StringProtocol>(of string: S, options: String.CompareOptions = []) -> [Range<Index>] {
var result: [Range<Index>] = []
var startIndex = self.startIndex
while startIndex < endIndex,
let range = self[startIndex...]
.range(of: string, options: options) {
result.append(range)
startIndex = range.lowerBound < range.upperBound ? range.upperBound :
index(range.lowerBound, offsetBy: 1, limitedBy: endIndex) ?? endIndex
}
return result
}
}
usage:
let str = "abcde"
if let index = str.index(of: "cd") {
let substring = str[..<index] // ab
let string = String(substring)
print(string) // "ab\n"
}
let str = "Hello, playground, playground, playground"
str.index(of: "play") // 7
str.endIndex(of: "play") // 11
str.indices(of: "play") // [7, 19, 31]
str.ranges(of: "play") // [{lowerBound 7, upperBound 11}, {lowerBound 19, upperBound 23}, {lowerBound 31, upperBound 35}]
case insensitive sample
let query = "Play"
let ranges = str.ranges(of: query, options: .caseInsensitive)
let matches = ranges.map { str[$0] } //
print(matches) // ["play", "play", "play"]
regular expression sample
let query = "play"
let escapedQuery = NSRegularExpression.escapedPattern(for: query)
let pattern = "\\b\(escapedQuery)\\w+" // matches any word that starts with "play" prefix
let ranges = str.ranges(of: pattern, options: .regularExpression)
let matches = ranges.map { str[$0] }
print(matches) // ["playground", "playground", "playground"]
Using String[Range<String.Index>] subscript you can get the sub string. You need starting index and last index to create the range and you can do it as below
let str = "abcde"
if let range = str.range(of: "cd") {
let substring = str[..<range.lowerBound] // or str[str.startIndex..<range.lowerBound]
print(substring) // Prints ab
}
else {
print("String not present")
}
If you don't define the start index this operator ..< , it take the starting index. You can also use str[str.startIndex..<range.lowerBound] instead of str[..<range.lowerBound]
Swift 5
Find index of substring
let str = "abcdecd"
if let range: Range<String.Index> = str.range(of: "cd") {
let index: Int = str.distance(from: str.startIndex, to: range.lowerBound)
print("index: ", index) //index: 2
}
else {
print("substring not found")
}
Find index of Character
let str = "abcdecd"
if let firstIndex = str.firstIndex(of: "c") {
let index = str.distance(from: str.startIndex, to: firstIndex)
print("index: ", index) //index: 2
}
else {
print("symbol not found")
}
In Swift 4 :
Getting Index of a character in a string :
let str = "abcdefghabcd"
if let index = str.index(of: "b") {
print(index) // Index(_compoundOffset: 4, _cache: Swift.String.Index._Cache.character(1))
}
Creating SubString (prefix and suffix) from String using Swift 4:
let str : String = "ilike"
for i in 0...str.count {
let index = str.index(str.startIndex, offsetBy: i) // String.Index
let prefix = str[..<index] // String.SubSequence
let suffix = str[index...] // String.SubSequence
print("prefix \(prefix), suffix : \(suffix)")
}
Output
prefix , suffix : ilike
prefix i, suffix : like
prefix il, suffix : ike
prefix ili, suffix : ke
prefix ilik, suffix : e
prefix ilike, suffix :
If you want to generate a substring between 2 indices , use :
let substring1 = string[startIndex...endIndex] // including endIndex
let subString2 = string[startIndex..<endIndex] // excluding endIndex
Doing this in Swift is possible but it takes more lines, here is a function indexOf() doing what is expected:
func indexOf(source: String, substring: String) -> Int? {
let maxIndex = source.characters.count - substring.characters.count
for index in 0...maxIndex {
let rangeSubstring = source.startIndex.advancedBy(index)..<source.startIndex.advancedBy(index + substring.characters.count)
if source.substringWithRange(rangeSubstring) == substring {
return index
}
}
return nil
}
var str = "abcde"
if let indexOfCD = indexOf(str, substring: "cd") {
let distance = str.startIndex.advancedBy(indexOfCD)
print(str.substringToIndex(distance)) // Returns "ab"
}
This function is not optimized but it does the job for short strings.
There are three closely connected issues here:
All the substring-finding methods are over in the Cocoa NSString world (Foundation)
Foundation NSRange has a mismatch with Swift Range; the former uses start and length, the latter uses endpoints
In general, Swift characters are indexed using String.Index, not Int, but Foundation characters are indexed using Int, and there is no simple direct translation between them (because Foundation and Swift have different ideas of what constitutes a character)
Given all that, let's think about how to write:
func substring(of s: String, from:Int, toSubstring s2 : String) -> Substring? {
// ?
}
The substring s2 must be sought in s using a String Foundation method. The resulting range comes back to us, not as an NSRange (even though this is a Foundation method), but as a Range of String.Index (wrapped in an Optional, in case we didn't find the substring at all). However, the other number, from, is an Int. Thus we cannot form any kind of range involving them both.
But we don't have to! All we have to do is slice off the end of our original string using a method that takes a String.Index, and slice off the start of our original string using a method that takes an Int. Fortunately, such methods exist! Like this:
func substring(of s: String, from:Int, toSubstring s2 : String) -> Substring? {
guard let r = s.range(of:s2) else {return nil}
var s = s.prefix(upTo:r.lowerBound)
s = s.dropFirst(from)
return s
}
Or, if you prefer to be able to apply this method directly to a string, like this...
let output = "abcde".substring(from:0, toSubstring:"cd")
...then make it an extension on String:
extension String {
func substring(from:Int, toSubstring s2 : String) -> Substring? {
guard let r = self.range(of:s2) else {return nil}
var s = self.prefix(upTo:r.lowerBound)
s = s.dropFirst(from)
return s
}
}
Swift 5
let alphabet = "abcdefghijklmnopqrstuvwxyz"
var index: Int = 0
if let range: Range<String.Index> = alphabet.range(of: "c") {
index = alphabet.distance(from: alphabet.startIndex, to: range.lowerBound)
print("index: ", index) //index: 2
}
Swift 5
extension String {
enum SearchDirection {
case first, last
}
func characterIndex(of character: Character, direction: String.SearchDirection) -> Int? {
let fn = direction == .first ? firstIndex : lastIndex
if let stringIndex: String.Index = fn(character) {
let index: Int = distance(from: startIndex, to: stringIndex)
return index
} else {
return nil
}
}
}
tests:
func testFirstIndex() {
let res = ".".characterIndex(of: ".", direction: .first)
XCTAssert(res == 0)
}
func testFirstIndex1() {
let res = "12345678900.".characterIndex(of: "0", direction: .first)
XCTAssert(res == 9)
}
func testFirstIndex2() {
let res = ".".characterIndex(of: ".", direction: .last)
XCTAssert(res == 0)
}
func testFirstIndex3() {
let res = "12345678900.".characterIndex(of: "0", direction: .last)
XCTAssert(res == 10)
}
In the Swift version 3, String doesn't have functions like -
str.index(of: String)
If the index is required for a substring, one of the ways to is to get the range. We have the following functions in the string which returns range -
str.range(of: <String>)
str.rangeOfCharacter(from: <CharacterSet>)
str.range(of: <String>, options: <String.CompareOptions>, range: <Range<String.Index>?>, locale: <Locale?>)
For example to find the indexes of first occurrence of play in str
var str = "play play play"
var range = str.range(of: "play")
range?.lowerBound //Result : 0
range?.upperBound //Result : 4
Note : range is an optional. If it is not able to find the String it will make it nil. For example
var str = "play play play"
var range = str.range(of: "zoo") //Result : nil
range?.lowerBound //Result : nil
range?.upperBound //Result : nil
Leo Dabus's answer is great. Here is my answer based on his answer using compactMap to avoid Index out of range error.
Swift 5.1
extension StringProtocol {
func ranges(of targetString: Self, options: String.CompareOptions = [], locale: Locale? = nil) -> [Range<String.Index>] {
let result: [Range<String.Index>] = self.indices.compactMap { startIndex in
let targetStringEndIndex = index(startIndex, offsetBy: targetString.count, limitedBy: endIndex) ?? endIndex
return range(of: targetString, options: options, range: startIndex..<targetStringEndIndex, locale: locale)
}
return result
}
}
// Usage
let str = "Hello, playground, playground, playground"
let ranges = str.ranges(of: "play")
ranges.forEach {
print("[\($0.lowerBound.utf16Offset(in: str)), \($0.upperBound.utf16Offset(in: str))]")
}
// result - [7, 11], [19, 23], [31, 35]
Have you considered using NSRange?
if let range = mainString.range(of: mySubString) {
//...
}

Check for duplicate characters in string using for-loops in swift

I did this using while loops but I'm wondering if there's a way to do this with for loops. I'm trying to write this clean so I can write it on a whiteboard for people to understand.
var str = "Have a nice day"
func unique(_ str: String) -> String {
var firstIndex = str.startIndex
while (firstIndex != str.endIndex) {
var secondIndex = str.index(after: firstIndex)
while (secondIndex != str.endIndex) {
if (str[firstIndex] == str[secondIndex]) {
return "Not all characters are unique"
}
secondIndex = str.index(after: secondIndex)
}
firstIndex = str.index(after: firstIndex)
}
return "All the characters are unique"
}
print("\(unique(str))")
You can use the indices of the characters:
var str = "Have a nice day"
func unique(_ str: String) -> String {
for firstIndex in str.characters.indices {
for secondIndex in str.characters.indices.suffix(from: str.index(after: firstIndex)) {
if (str[firstIndex] == str[secondIndex]) {
return "Not all characters are unique"
}
}
}
return "All the characters are unique"
}
print("\(unique(str))")
I used a hash to do it. Not sure how fast it is but it doesn't need to be in my case. (In my case I'm doing phone numbers so I get rid of the dashes first)
let theLetters = t.components(separatedBy: "-")
let justChars = theLetters.joined()
var charsHash = [Character:Int]()
justChars.forEach { charsHash[$0] = 1 }
if charsHash.count < 2 { return false }
... or more compact, as an extension...
extension String {
var isMonotonous: Bool {
var hash = [Character:Int]()
self.forEach { hash[$0] = 1 }
return hash.count < 2
}
}
let a = "asdfasf".isMonotonous // false
let b = "aaaaaaa".isMonotonous // true
Here is the for-loop version of your question.
let string = "Have a nice day"
func unique(_ string: String) -> String {
for i in 0..<string.characters.count {
for j in (i+1)..<string.characters.count {
let firstIndex = string.index(string.startIndex, offsetBy: i)
let secondIndex = string.index(string.startIndex, offsetBy: j)
if (string[firstIndex] == string[secondIndex]) {
return "Not all characters are unique"
}
}
}
return "All the characters are unique"
}
There are a lot of ways this can be achieved and this is just one way of doing it.
As #adev said, there are many ways to finish this. For example, you can use only one for-loop with a dictionary to check the string is unique or not:
Time complexity: O(n). Required O(n) additional storage space for the dictionary.
func unique(_ input: String) -> Bool {
var dict: [Character: Int] = [:]
for (index, char) in input.enumerated() {
if dict[char] != nil { return false }
dict[char] = index
}
return true
}
unique("Have a nice day") // Return false
unique("Have A_nicE-dⒶy") // Return true
let str = "Hello I m sowftware developer"
var dict : [Character : Int] = [:]
let newstr = str.replacingOccurrences(of: " ", with: "")
print(newstr.utf16.count)
for i in newstr {
if dict[i] == nil {
dict[i] = 1
}else{
dict[i]! += 1
}
}
print(dict) // ["e": 5, "v": 1, "d": 1, "H": 1, "f": 1, "w": 2, "s": 1, "I": 1, "m": 1, "o": 3, "l": 3, "a": 1, "r": 2, "p": 1, "t": 1]
You can find any value of char how many times write in string object.
this is my solution
func hasDups(_ input: String) -> Bool {
for c in input {
if (input.firstIndex(of: c) != input.lastIndex(of: c)) {
return true
}
}
return false
}
let input = "ssaajaaan"
var count = 1
for i in 0..<input.count
{
let first = input[input.index(input.startIndex, offsetBy: i)]
if i + 1 < input.count
{
let next = input[input.index(input.startIndex, offsetBy: i + 1)]
if first == next
{
count += 1
}
else
{
count = 1
}
}
if count >= 2
{
print(first," = ",count)
}
}
let inputStr = "sakkett"
var tempStr = String()
for char in inputStr
{
if tempStr.contains(char) == false
{
tempStr = tempStr.appending(String(char))
let filterArr = inputStr.filter({ $0 == char})
if filterArr.count > 1 {
print("Duplicate char is \(char)")
}
}
}
//Output:
Duplicate char is k
Duplicate char is t

How to check last 3 character in string contain numbers in Swift?

For a given String instance, I want to check whether the last three characters are numeric characters (0, 1, 2, ..., 9) or not.
For example, the string
let str1 = "SACH092"
should return true for such a query, whereas e.g.
let str2 = "SACHA92"
should return false for the query.
I am using Xcode 7.3.1.
(As pointed out by #NiravD, for pre Swift 3, use where to join parts of multi-clause conditions. For Swift 3, parts of multi-clause conditions are simply joined by ,. For both methods below, both Swift 2.2 and 3 versions are included)
Use pattern matching for numeric characters "0"..."9"
Swift 2.2
extension String {
var lastThreeLettersAreNumbers: Bool {
if case let chars = characters.suffix(3) where chars.count > 2 {
let numbersPattern = Character("0")..."9"
return chars.reduce(true) { $0 && (numbersPattern ~= $1) }
}
return false
}
}
Swift 3
extension String {
var lastThreeLettersAreNumbers: Bool {
if case let chars = characters.suffix(3), chars.count > 2 {
let numbersPattern = Character("0")..."9"
return chars.reduce(true) { $0 && (numbersPattern ~= $1) }
}
return false
}
}
/* example usage, common for both Swift 2.2/3 version */
let str1 = "SACH092"
let str2 = "SACH0B2"
print(str1.lastThreeLettersAreNumbers) // true
print(str2.lastThreeLettersAreNumbers) // false
Make use of nil-return Int by String initializer, with flatMap
You can make use of the fact that the Int by String initializer returns nil for strings that cannot be represented as integers.
Swift 2.2
extension String {
var lastThreeLettersAreNumbers: Bool {
if case let chars = characters.suffix(3) where chars.count > 2 {
return chars.flatMap{Int(String($0))}.count == 3
}
return false
}
}
Swift 3
extension String {
var lastThreeLettersAreNumbers: Bool {
if case let chars = characters.suffix(3), chars.count > 2 {
return chars.flatMap{Int(String($0))}.count == 3
}
return false
}
}
/* example usage, common for both Swift 2.2/3 version */
let str1 = "SACH092"
let str2 = "SACH0B2"
print(str1.lastThreeLettersAreNumbers) // true
print(str2.lastThreeLettersAreNumbers) // false
For getting last 3 characters,
let exampleString = "SACH092"
let last3Char = exampleString.substringFromIndex(exampleString.endIndex.advancedBy(-3))
Check if last3Char contains all Digits,
let badCharacters = NSCharacterSet.decimalDigitCharacterSet().invertedSet
if last3Char.rangeOfCharacterFromSet(badCharacters) == nil {
print("String contains all digits")
} else {
print("String contains non-digit characters")
}
The best way to achieve string control is to use Regular Expressions.
For you :
var str = "SACH092"
let pattern = "^.*[0-9]{3,3}$"
let regexp = try! NSRegularExpression(pattern: pattern, options: [])
let matches = regexp.matches(in: str, options: [], range: NSMakeRange(0, str.characters.count))
print("End with 3 numbers : \(matches.count > 0)")
You can use like this
let s : NSString = "SACH092"
let trimmedString: String = (s as NSString).substringFromIndex(max(s.length-3,0))
print(trimmedString.isNumeric) // return true or false
//Make Extension of String
extension String {
var isNumeric: Bool {
let nums: Set<Character> = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
return Set(self.characters).isSubsetOf(nums)
}
}
To get the last three letters of a string
let oldString = "yourString"
let newString = a.substringFromIndex(a.endIndex.advancedBy(-3))
To check those characters are numbers,
func isNumber(num: String) -> Bool {
let numberCharacters = NSCharacterSet.decimalDigitCharacterSet().invertedSet
return !num.isEmpty && num.rangeOfCharacterFromSet(numberCharacters) == nil
}

Index of a substring in a string with Swift

I'm used to do this in JavaScript:
var domains = "abcde".substring(0, "abcde".indexOf("cd")) // Returns "ab"
Swift doesn't have this function, how to do something similar?
edit/update:
Xcode 11.4 • Swift 5.2 or later
import Foundation
extension StringProtocol {
func index<S: StringProtocol>(of string: S, options: String.CompareOptions = []) -> Index? {
range(of: string, options: options)?.lowerBound
}
func endIndex<S: StringProtocol>(of string: S, options: String.CompareOptions = []) -> Index? {
range(of: string, options: options)?.upperBound
}
func indices<S: StringProtocol>(of string: S, options: String.CompareOptions = []) -> [Index] {
ranges(of: string, options: options).map(\.lowerBound)
}
func ranges<S: StringProtocol>(of string: S, options: String.CompareOptions = []) -> [Range<Index>] {
var result: [Range<Index>] = []
var startIndex = self.startIndex
while startIndex < endIndex,
let range = self[startIndex...]
.range(of: string, options: options) {
result.append(range)
startIndex = range.lowerBound < range.upperBound ? range.upperBound :
index(range.lowerBound, offsetBy: 1, limitedBy: endIndex) ?? endIndex
}
return result
}
}
usage:
let str = "abcde"
if let index = str.index(of: "cd") {
let substring = str[..<index] // ab
let string = String(substring)
print(string) // "ab\n"
}
let str = "Hello, playground, playground, playground"
str.index(of: "play") // 7
str.endIndex(of: "play") // 11
str.indices(of: "play") // [7, 19, 31]
str.ranges(of: "play") // [{lowerBound 7, upperBound 11}, {lowerBound 19, upperBound 23}, {lowerBound 31, upperBound 35}]
case insensitive sample
let query = "Play"
let ranges = str.ranges(of: query, options: .caseInsensitive)
let matches = ranges.map { str[$0] } //
print(matches) // ["play", "play", "play"]
regular expression sample
let query = "play"
let escapedQuery = NSRegularExpression.escapedPattern(for: query)
let pattern = "\\b\(escapedQuery)\\w+" // matches any word that starts with "play" prefix
let ranges = str.ranges(of: pattern, options: .regularExpression)
let matches = ranges.map { str[$0] }
print(matches) // ["playground", "playground", "playground"]
Using String[Range<String.Index>] subscript you can get the sub string. You need starting index and last index to create the range and you can do it as below
let str = "abcde"
if let range = str.range(of: "cd") {
let substring = str[..<range.lowerBound] // or str[str.startIndex..<range.lowerBound]
print(substring) // Prints ab
}
else {
print("String not present")
}
If you don't define the start index this operator ..< , it take the starting index. You can also use str[str.startIndex..<range.lowerBound] instead of str[..<range.lowerBound]
Swift 5
Find index of substring
let str = "abcdecd"
if let range: Range<String.Index> = str.range(of: "cd") {
let index: Int = str.distance(from: str.startIndex, to: range.lowerBound)
print("index: ", index) //index: 2
}
else {
print("substring not found")
}
Find index of Character
let str = "abcdecd"
if let firstIndex = str.firstIndex(of: "c") {
let index = str.distance(from: str.startIndex, to: firstIndex)
print("index: ", index) //index: 2
}
else {
print("symbol not found")
}
In Swift 4 :
Getting Index of a character in a string :
let str = "abcdefghabcd"
if let index = str.index(of: "b") {
print(index) // Index(_compoundOffset: 4, _cache: Swift.String.Index._Cache.character(1))
}
Creating SubString (prefix and suffix) from String using Swift 4:
let str : String = "ilike"
for i in 0...str.count {
let index = str.index(str.startIndex, offsetBy: i) // String.Index
let prefix = str[..<index] // String.SubSequence
let suffix = str[index...] // String.SubSequence
print("prefix \(prefix), suffix : \(suffix)")
}
Output
prefix , suffix : ilike
prefix i, suffix : like
prefix il, suffix : ike
prefix ili, suffix : ke
prefix ilik, suffix : e
prefix ilike, suffix :
If you want to generate a substring between 2 indices , use :
let substring1 = string[startIndex...endIndex] // including endIndex
let subString2 = string[startIndex..<endIndex] // excluding endIndex
Doing this in Swift is possible but it takes more lines, here is a function indexOf() doing what is expected:
func indexOf(source: String, substring: String) -> Int? {
let maxIndex = source.characters.count - substring.characters.count
for index in 0...maxIndex {
let rangeSubstring = source.startIndex.advancedBy(index)..<source.startIndex.advancedBy(index + substring.characters.count)
if source.substringWithRange(rangeSubstring) == substring {
return index
}
}
return nil
}
var str = "abcde"
if let indexOfCD = indexOf(str, substring: "cd") {
let distance = str.startIndex.advancedBy(indexOfCD)
print(str.substringToIndex(distance)) // Returns "ab"
}
This function is not optimized but it does the job for short strings.
There are three closely connected issues here:
All the substring-finding methods are over in the Cocoa NSString world (Foundation)
Foundation NSRange has a mismatch with Swift Range; the former uses start and length, the latter uses endpoints
In general, Swift characters are indexed using String.Index, not Int, but Foundation characters are indexed using Int, and there is no simple direct translation between them (because Foundation and Swift have different ideas of what constitutes a character)
Given all that, let's think about how to write:
func substring(of s: String, from:Int, toSubstring s2 : String) -> Substring? {
// ?
}
The substring s2 must be sought in s using a String Foundation method. The resulting range comes back to us, not as an NSRange (even though this is a Foundation method), but as a Range of String.Index (wrapped in an Optional, in case we didn't find the substring at all). However, the other number, from, is an Int. Thus we cannot form any kind of range involving them both.
But we don't have to! All we have to do is slice off the end of our original string using a method that takes a String.Index, and slice off the start of our original string using a method that takes an Int. Fortunately, such methods exist! Like this:
func substring(of s: String, from:Int, toSubstring s2 : String) -> Substring? {
guard let r = s.range(of:s2) else {return nil}
var s = s.prefix(upTo:r.lowerBound)
s = s.dropFirst(from)
return s
}
Or, if you prefer to be able to apply this method directly to a string, like this...
let output = "abcde".substring(from:0, toSubstring:"cd")
...then make it an extension on String:
extension String {
func substring(from:Int, toSubstring s2 : String) -> Substring? {
guard let r = self.range(of:s2) else {return nil}
var s = self.prefix(upTo:r.lowerBound)
s = s.dropFirst(from)
return s
}
}
Swift 5
let alphabet = "abcdefghijklmnopqrstuvwxyz"
var index: Int = 0
if let range: Range<String.Index> = alphabet.range(of: "c") {
index = alphabet.distance(from: alphabet.startIndex, to: range.lowerBound)
print("index: ", index) //index: 2
}
Swift 5
extension String {
enum SearchDirection {
case first, last
}
func characterIndex(of character: Character, direction: String.SearchDirection) -> Int? {
let fn = direction == .first ? firstIndex : lastIndex
if let stringIndex: String.Index = fn(character) {
let index: Int = distance(from: startIndex, to: stringIndex)
return index
} else {
return nil
}
}
}
tests:
func testFirstIndex() {
let res = ".".characterIndex(of: ".", direction: .first)
XCTAssert(res == 0)
}
func testFirstIndex1() {
let res = "12345678900.".characterIndex(of: "0", direction: .first)
XCTAssert(res == 9)
}
func testFirstIndex2() {
let res = ".".characterIndex(of: ".", direction: .last)
XCTAssert(res == 0)
}
func testFirstIndex3() {
let res = "12345678900.".characterIndex(of: "0", direction: .last)
XCTAssert(res == 10)
}
In the Swift version 3, String doesn't have functions like -
str.index(of: String)
If the index is required for a substring, one of the ways to is to get the range. We have the following functions in the string which returns range -
str.range(of: <String>)
str.rangeOfCharacter(from: <CharacterSet>)
str.range(of: <String>, options: <String.CompareOptions>, range: <Range<String.Index>?>, locale: <Locale?>)
For example to find the indexes of first occurrence of play in str
var str = "play play play"
var range = str.range(of: "play")
range?.lowerBound //Result : 0
range?.upperBound //Result : 4
Note : range is an optional. If it is not able to find the String it will make it nil. For example
var str = "play play play"
var range = str.range(of: "zoo") //Result : nil
range?.lowerBound //Result : nil
range?.upperBound //Result : nil
Leo Dabus's answer is great. Here is my answer based on his answer using compactMap to avoid Index out of range error.
Swift 5.1
extension StringProtocol {
func ranges(of targetString: Self, options: String.CompareOptions = [], locale: Locale? = nil) -> [Range<String.Index>] {
let result: [Range<String.Index>] = self.indices.compactMap { startIndex in
let targetStringEndIndex = index(startIndex, offsetBy: targetString.count, limitedBy: endIndex) ?? endIndex
return range(of: targetString, options: options, range: startIndex..<targetStringEndIndex, locale: locale)
}
return result
}
}
// Usage
let str = "Hello, playground, playground, playground"
let ranges = str.ranges(of: "play")
ranges.forEach {
print("[\($0.lowerBound.utf16Offset(in: str)), \($0.upperBound.utf16Offset(in: str))]")
}
// result - [7, 11], [19, 23], [31, 35]
Have you considered using NSRange?
if let range = mainString.range(of: mySubString) {
//...
}