Remove parentheses from array value converted to string - ios

I have a textview and when displaying text there are parentheses in the text. I want to eliminate them. This is my code:
self.txtpstn.text = "\(self.no_pstn.self as NSArray)"
self.txtmobile.text = "\(self.no_mobile.self as NSArray)"
This is the display on the application:

Use join:
self.txtpstn.text = "\((self.no_pstn.self as NSArray).componentsJoined(by: ","))"
self.txtmobile.text = "\(self.no_mobile.self as NSArray).componentsJoined(by: ","))"

Related

Delete all character after particular character in string

I have a string and I want to delete some character after particulate character, I tried using with substring but nothings working
"word" is a String Variable here where you can apply your needs and "T" is a character after which you want to delete all other characters
if let index = word.range(of: "T")?.lowerBound {
let substring = word[..<index]
let string = String(substring)
self.birthdayField.text = string
}

Hidden character getting when copied from phonebook and paste in textfield. How to remove it

I Have mobile number as login id when someone copied number and pasted in textfield. it show me "9999999999\u{e2}". "\u{e2}" is Hidden character which is not display in textfield but when check in logs it showing me.
I want to remove that type of characters.
Thanks in advance for your kind reply.
You can try to trim any none-numeric character
let strWithAll = "kdldls155558894"
let number = strWithAll.components(separatedBy: CharacterSet.decimalDigits.inverted).joined()
You can try with this
let myString = "dasdf3453453fsdf23455sf.2234"
let result = String(myString.characters.filter { String($0).rangeOfCharacter(from: CharacterSet(charactersIn: "0123456789")) != nil })
print(result)
\u{e2} actually indicates â. I am not sure why this character is getting copied along with the number but as the special characters are appended at the end of the string, you can try this approach:
let yourText = "9999999999\u{e2}"
textField.text = yourText.components(separatedBy: CharacterSet.decimalDigits.inverted) [0]
Check the functionality of components(separatedBy:) https://developer.apple.com/documentation/foundation/nsstring/1413214-components
If you're sure that the phone number has only digits (i.e. no "-" or "+" or space characters), you can simply filter out the characters that are not digits:
let number = "9999999999\u{e2}"
let filteredNumber = number.filter { "0"..."9" ~= $0 } // 9999999999

How to get frequency of line feed in string by swift

Sorry,I'm new of swift. I want to calculate the target char in string.But I don't know how to do.Have any good suggestion to me?Thanks.
let string = "hello\nNice to meet you.\nMy name is Leo.\n" //I want to get 3
If you simply want a count of newline characters then you can use a filter on the string's characters:
let string = "hello\nNice to meet you.\nMy name is Leo.\n"
let count = string.characters.filter { $0 == "\n" }.count
print(count)
This outputs 3 as expected.
An alternative is to split the lines with the components(separatedBy method:
let string = "hello\nNice to meet you.\nMy name is Leo.\n"
let lineCounter = string.components(separatedBy: "\n").count - 1
or more versatile to consider all kinds of newline characters
let lineCounter = string.components(separatedBy: CharacterSet.newlines).count - 1
Due to the trailing newline character the result is 4. To ignore a trailing new line you have to decrement the result.

How to put variable inside text field AND how to convert all elements to string NOT just 1 at a time?

This is a follow up question to How to have 10 characters total and make sure at least one character from 4 different sets is used randomly
this is my code so far
let sets = ["ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz", "1234567890", "\"-/:;()$&#.,?!'[]{}#%^\\|~<>€£¥•.,"].map { Array($0.characters) }
var randoms = sets.map { $0.random }
while randoms.count < 10 {
randoms.append(sets.random.random)
}
var convertedElems = String()
let something = randoms.shuffled()
for key in something {
convertedElems = String(key)
}
uniqueRoomID.text = randoms.shuffled()
Im getting an error saying cannot convert [Element] to type "String"
So i tried a for loop but that only converts 1 at a time when its supposed to do all 10
my other question is i tried storing a character in a variable and then setting a text field.text equal to that variable and nothing happened
What am i doing wrong here
Your randoms.shuffled() is an array of Characters. You need to convert it back into a String.
Change this:
uniqueRoomID.text = randoms.shuffled()
to this:
uniqueRoomID.text = String(randoms.shuffled())

Printing string arrays to UILabels

Is there any way to print out an Array of Strings to an UILabel without any special characters, i.e. ["number1","number2","number3"]
I want it to look like this in my UILabel Output:
number1
number2
number3
Here is my code:
let addInputs = quantityField.text! + "x " + descriptionField.text!
var listArray: [String] = []
listArray.append("\(addInputs)")
for addInputs in listArray {
//itemListLabel.text = "\n\(addInputs)"
print("\(addInputs)")
itemListLabel.text = "\(listArray)"
}
You can join the items together with \n to add a hard return:
itemListLabel.text = listArray.joinWithSeparator("\n")
You will need to do 2 things:
itemListLabel.numberOfLines = listArray.count
itemListLabel.text = listArray.joinWithSeparator("\n")
The first line sets the number of lines to display in the label. If you know beforehand what you want, you can just set this in Interface Builder. The second line joins the array of strings with a new-line separator between each.
Swift 5.3
itemListLabel.text = listArray.joined(separator: "\n")

Resources