Swift Searching for key words in a string [duplicate] - swift

This question already has answers here:
Swift: String contains String (Without using NSString)?
(4 answers)
How do I check if a string contains another string in Swift?
(28 answers)
Closed 5 years ago.
I am creating a simple chat bot using swift 3 in Xcode 8, and have been looking for a way to search for a specific word in a string.
For example:
If the user inputs "I would like to have a cup of coffee please."
The program then checks the string for the word "coffee" then finding "coffee" in the string it then returns bool.
I know that you can do this in python:
phrase = "I would like to have a cup of coffee please."
if "coffee" in phrase
print('hi there')
How would you do this in swift 3?
Thanks

Swift 4...
Using contains(_:) (See String's ref in Swift 4):
let haystack = "I would like to have a cup of coffee please."
let needle = "like"
print(haystack.contains(needle))
Swift 3..<4
Using range(of:..) (See String's ref in Swift 3):
let haystack = "I would like to have a cup of coffee please."
let needle = "like"
if let exists = haystack.range(of: needle) {
// Substring exists
}

Related

Swift: remove all whitespace characters from a string [duplicate]

This question already has answers here:
How to remove whitespaces in strings in Swift?
(11 answers)
Closed 4 months ago.
I'd like to use CharacterSet.whitespacesAndNewlines to remove whitespace from a string.
However, the code below does not compile.
myString.replacingOccurrences(of: CharacterSet.whitespacesAndNewlines, with: "")
// Instance method 'replacingOccurrences(of:with:options:range:)' requires that 'CharacterSet' conform to 'StringProtocol'
What is a simple way to achieve the intended result?
You can use
let result = myString.filter { !$0.isWhitespace }

How to fix 'characters' is unavailable: Please use String directly in swift 5 [duplicate]

This question already has answers here:
How to filter characters from a string in Swift 4
(2 answers)
Closed 3 years ago.
i've some code to filtering number inside variable.
Here's the code:
var numbers = String(anotherNumbers.characters.filter { "01234567890.".characters.contains($0) })
In the swift 3, this code working correctly. But in the Swift 5, i get an error 'characters' is unavailable: Please use String directly
How to fix this error?
Thankyou.
You can remove characters to use String directly.
For example
var anotherNumbers = "0123456789"
var numbers = String(anotherNumbers.filter { "01234567890.".contains($0) })
returns "0123456789"

Print contents of array with custom class swift [duplicate]

This question already has answers here:
Overriding description method in NSObject on swift
(1 answer)
How can I change the textual representation displayed for a type in Swift?
(7 answers)
Closed 5 years ago.
I have an array of type custom class as seen below. It has strings, ints, and double values.
class TrendData: NSObject {
var earlyTime = Date()
var recentTime = Date()
var earlyTimePrice = Double()
var recentTimePrice = Double()
}
I have an array as follows
let dataArray = [TrendData]()
I have filled in 2 values into the dataArray.
Right now when I use a print command as follows
print ("Label: \(dataArray)")
it prints this Label: [<App.TrendData: 0x600000472440>] ** [<App.TrendData: 0x600000472440>]
This is the correct behavior but I want to see all the values within each of these elements in the array. What is the best way to do that? I don't want to explicitly list out each element or even put it into a loop if I can avoid it. I am able to do it, it is just really messy right now. Is there any function or command that does this automatically?

Can we quick let String become [Character] in swift? [duplicate]

This question already has answers here:
Convert Swift string to array
(14 answers)
Closed 6 years ago.
I have a String and I want get an Array of the String's Characters.
I can already do it, like this:
var Carr = [Character]()
for c in s.characters {
Carr.append(c)
}
As you can see, it's not beautiful or efficient.
In Java, I can use char[] sa = s.toCharArray(); to get a char[]. Is there a similarly simple way to do this in Swift?
let charArray = Array(s.characters)
String.characters is a String.CharacterView. It conforms to BidirectionalCollection, which inherits from Collection, and ultimately Sequence.
Because it conforms to Sequence, it can be used in a for loop, as you showed. But also, it can be used in the initializer of Array that takes a sequence.
You are already using the right function:
let yourString = "a string"
let characters = yourString.characters
let count = characters.count
This gives you the collection of characters contained in your string

Delete all characters after a certain character from a string in Swift [duplicate]

This question already has answers here:
What is the more elegant way to remove all characters after specific character in the String object in Swift
(6 answers)
Closed 6 years ago.
I have a textField and I would like to remove all character after a certain character.
For instance if what I have in the textField is the word Orange and I want to remove all characters after the n I would like to get Ora after the deletion.
How can I delete all characters after a certain character from a string in Swift?
Thanks
You can use StringProtocol method range(of string:), get the resulting range lowerBound, create a PartialRangeUpTo with it and subscript the original string:
Swift 4 or later
let word = "orange"
if let index = word.range(of: "n")?.lowerBound {
let substring = word[..<index] // "ora"
// or let substring = word.prefix(upTo: index) // "ora"
// (see picture below) Using the prefix(upTo:) method is equivalent to using a partial half-open range as the collection’s subscript.
// The subscript notation is preferred over prefix(upTo:).
let string = String(substring)
print(string) // "ora"
}
You could do it like this:
guard let range = text.rangeOfString("Your String or Character here") else {
return the text
}
return text.substringToIndex(range.endIndex)
// depending on if you want to delete before a certain string, you would use range.startIndex