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

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 }

Related

'characters' is unavailable: Please use String directly [duplicate]

This question already has an answer here:
'characters is unavailable' please use string directly
(1 answer)
Closed 3 years ago.
Пожалуйста помогите
'characters' is unavailable: Please use String directly
Swift strings are different now. There's no characters property anymore. This should do the trick:
extension URL {
func makeStandardizedLastPathComponent() -> String {
return lastPathComponent.trimmingCharacters(in: .whitespaces)
}
func makeStandardizedFirstCharacterOfLastPathComponent() -> Character? {
return makeStandardizedLastPathComponent().localizedUppercase.first
}
}

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"

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

How do I use ?: in Swift? [duplicate]

This question already has answers here:
Does Swift have a null coalescing operator and if not, what is an example of a custom operator?
(6 answers)
Closed 6 years ago.
I love this syntax in Objective-C, where a question mark and colon let you use a backup value:
NSString const name = [self getName] ?: #"backup";
and I want to use the same in Swift, but I get this when I try:
Is there any way to do this in Swift? If not, can I write a custom infix operator to do it?
It's called a null (or nil) coalescing operator, and the Swift syntax is:
let name = getName() ?? "backup";

What to use in place of stringByAppendingPathComponent in Swift 2 [duplicate]

This question already has answers here:
stringByAppendingPathComponent is unavailable
(11 answers)
Closed 7 years ago.
I have just installed Xcode 7 with the new Swift 2, and I now have 50+ errors saying that "stringByAppendingPathComponent" is unavailable, and that I should use "URLByAppendingPathComponent" instead. I have been setting all of my texture properties like so:
let dropTexture = SKTexture(image: UIImage(
contentsOfFile:NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent(
"P04_rainDrop1.png"))!)
I have been doing this so they do not stay in memory when the SKScene is changed and it has been working perfectly. However directly replacing "URLByAppendingPathComponent" does not fix the errors.
How can I change this to fix the error and get the same SKTexture?
All you have to do is cast to NSString to recover stringByAppendingPathComponent, like this:
let dropTexture = SKTexture(image: UIImage(
contentsOfFile:(NSBundle.mainBundle().resourcePath! as NSString).stringByAppendingPathComponent(
"P04_rainDrop1.png"))!)
As Leo Dabus rightly says, you can save yourself from all that casting by adding an extension to String. However, you should not, as he suggests, call NSString(string:), which generates an extra string. Just cast:
extension String {
func stringByAppendingPathComponent(pathComponent: String) -> String {
return (self as NSString).stringByAppendingPathComponent(pathComponent)
}
}