Adding a letter to a string variable in swift [duplicate] - swift

This question already has answers here:
Swift - encode URL
(19 answers)
Closed 5 years ago.
For example, this variable needs to add "25" after the "%" character. How can I do it?
var message = "cars %50 discount"
must be "cars %2550 discount"

You can do that by replacing '%' with '%25' or whatever characters you'd like to insert after '%':
var message = "cars %50 discount"
message = message.replacingOccurrences(of: "%", with: "%25")
This approach is dangerous, check the this answer for more information and for an alternative approach.

I believe this link provides the answer you need:
How can I add Variables into a String?(Swift)
For your example do:
var discountAmount = 2550
var message = "cars %\(discountAmount) discount"
(and the message prints "cars %2550 discount"

Related

How to Print a shuffled array without brackets and commas in swift [duplicate]

This question already has answers here:
How do I convert a Swift Array to a String?
(25 answers)
Closed 2 years ago.
How do i Print a shuffled array without brackets and commas in swift?
For example if I create an array:
var names = ["john", "connor", "Sarra"]
print(names.shuffled())
It will print the names array with commas and brackets - I want it to be printed without anything !
You can use the joined(separator:) method:
var names = ["john", "connor", "Sarra"]
print(names.shuffled()) // ["john", "connor", "Sarra"]
print(names.shuffled().joined(separator: "")) // connorSarrajohn
print(names.shuffled().joined(separator: " ")) // connor Sarra john

What does backslash do in Swift?

In the following line of code, what is the backslash telling Swift to do?
print("The total cost of my meal is \(dictionary["pizza"]! + dictionary["ice cream"]!)")
The backslash has a few different meanings in Swift, depending on the context. In your case, it means string interpolation:
print("The total cost of my meal is \(dictionary["pizza"]! + dictionary["ice cream"]!)")
...is the same as:
print("The total cost of my meal is " + String(dictionary["pizza"]! + dictionary["ice cream"]!))
But the first form is more readable. Another example:
print("Hello \(person.firstName). You are \(person.age) years old")
Which may print something like Hello John. You are 42 years old. Much clearer than:
print("Hello " + person.firstName + ". You are " + String(person.age) + " years old")
That's called String interpolation. When you want to embed the value of a variable in a String, you have to put the variable name between parentheses and escape the opening parentheses with a backslash. This way the compiler knows that it has to substitute the value of the variable there instead of using the String literal of the variable name.
For more information on the topic, have a look at the String interpolation part of the Swift Language Guide.

How can I remove the last character of a String in Swift 4? [duplicate]

This question already has answers here:
Remove last character from string. Swift language
(23 answers)
Remove Last Two Characters in a String
(5 answers)
Closed 5 years ago.
How do I remove the last character of a string in Swift 4? I used to use substring in earlier versions of Swift, but the substring method is deprecated.
Here's the code I have.
temp = temp.substring(to: temp.index(before: temp.endIndex))
dropLast() is your safest bet, because it handles nils and empty strings without crashing (answer by OverD), but if you want to return the character you removed, use removeLast():
var str = "String"
let removedCharacter = str.removeLast() //str becomes "Strin"
//and removedCharacter will be "g"
A different function, removeLast(_:) changes the count of the characters that should be removed:
var str = "String"
str.removeLast(3) //Str
The difference between the two is that removeLast() returns the character that was removed, while removeLast(_:) does not have a return value:
var str = "String"
print(str.removeLast()) //prints out "g"
You can use dropLast()
You can find more information on Apple documentation
A literal Swift 4 conversion of your code is
temp = String(temp[..<temp.index(before: temp.endIndex)])
foo.substring(from: index) becomes foo[index...]
foo.substring(to: index) becomes foo[..<index]
and in particular cases a new String must be created from the Substring result.
but the solution in the4kmen's answer is much better.

How to eliminate a space character from a String? [duplicate]

This question already has answers here:
Does swift have a trim method on String?
(16 answers)
Closed 5 years ago.
In my app I have a text field, and when the user presses a button the text he have entered is added to a label. The problem is that if the user add an space at the end, the txt of the label looks bad.
Text field: "Diego "
Label: Great Diego !
Perform this on your String:
let trimmedString = yourstring.trimmingCharacters(in: .whitespacesAndNewlines)
It returns a new string made by removing from both ends of the String characters contained in a given character set, which in this case are whitesspaces and newlines. If you don't want to trim newlines, replace .whitespaceAndNewLines with .whitespace.
You can use String's trimming method passing in the characters to be trimmed. One way to do this is:
let string = " something "
string.trimmingCharacters(in: .whitespacesAndNewlines)
Which will remove whitespace and newlines from the beginning and the end of the string.

Swift: Create String given length same character [duplicate]

This question already has answers here:
Create a string with n blank spaces or other repeated character
(4 answers)
Closed 6 years ago.
How can you create a String of a given length consisting of the same character - without using a loop?
Ie: create a String 10 characters in length where each character is an asterisk: **********
Similar to this approach in Java: new String(new char[n]).replace("\0", s);
There's a String initializer for that:
init(repeating repeatedValue: String, count: Int)
Description Creates
a new string representing the given string repeated the specified
number of times.
let string = String(repeating: "*", count: 10)