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)
Related
It used to be you could use substring to get a portion of a string. That has been deprecated in favor on string index. But I can't seem to make a string index out of integers.
var str = "hellooo"
let newindex = str.index(after: 3)
str = str[newindex...str.endIndex]
No matter what the string is, I want the second 3 characters. So and str would contain "loo". How can I do this?
Drop the first three characters and the get the remaining first three characters
let str = "helloo"
let secondThreeCharacters = String(str.dropFirst(3).prefix(3))
You might add some code to handle the case if there are less than 6 characters in the string
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
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.
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.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Regular Expression to match outer brackets
Can I use Perl regular expressions to match balanced text?
i would like to extract a whole text which has matching numbers of closing brackets. Here is the example text.
Big String 1 {sub string 1 {{something} something {}} sub string 2{something} sub string {{something}{something else}}}
Is there an easy way for me to extract whole string which belongs to Big String 1 only? The results i would like to have is as such:
Big String 1 = "sub string 1 {{something} something {}} sub string 2{something} sub string {{something}{something else}}"
Thanks,