This question already has answers here:
How to check what a String starts with (prefix) or ends with (suffix) in Swift
(2 answers)
Closed 5 years ago.
I have a Text Field that a user can use to enter their LinkedIn URL. However, I don't want the user to be able to just enter any URL they want so how can I check that the string they entered exactly starts with
https://www.linkedin.com/in/...
You can use String's hasPrefix function:
if textField.text.hasPrefix("https://www.linkedin.com/in") {
print("string starts with https://www.linkedin.com/in")
} else {
print("string does not start with https://www.linkedin.com/in")
}
Related
This question already has an answer here:
Can we use keywords as parameter names in SWIFT?
(1 answer)
Closed 4 years ago.
I am a new iOS programming. I am creating a sample app which get data and set data using Firestore. For the collection contains some documents which and named those properties like extension... So in programming I need to create variable with that name extension in order to set data to Firestore but Xcode is warning me that I don't have to declare that variable name.
I have been trying to figure it out but not work. So, I decided to ask my own question if there any technique to delacre variable name which is warning by Xcode.
var `extension`: String?
var username: String?
init(`extension`: String, username: String) {
self.`extension` = `extension`
self.username = username
}
I think this will work by using ``.
This question already has answers here:
Any way to replace characters on Swift String?
(23 answers)
Closed 5 years ago.
Alright, so I am pulling weather data into my app, and in order to pull the location based api url, I have to insert the city and the shorthand state (i.e. CA, MO, NY, ... etc.) into the api url. I am able to get both the city and shorthand state, the problem is any spaces in the city need to be formatted as underscores, such as that New York has to equal New_York. How do I go about doing this? The city name is in a String format. I already see answers on Stack Overflow, but none are Swift 4.
Proper implementation is the exact same as Swift 3:
let str = "New York"
let replaced = str.replacingOccurrences(of: " ", with: "_")
This question already has answers here:
What is the most succinct way to remove the first character from a string in Swift?
(13 answers)
Closed 5 years ago.
I have this code:
func barcodeData(_ barcode: String!, type: Int32) {
referenceField.text = barcode!
}
When I scan a barcode the textfield is replaced by it. It's always an "R" in the beginning of the barcode followed by some digits.
I want the textfield to display only the numbers. So if the barcode scanned is "R454512" I want the textfield to show only "454512"
Could someone explain this to me? I'm pretty new in this barcode-world.
I don't have any values yet so how am I supposed to use this?
Thanks
Another way to do this is with a characterSet. This way if there are more letters or spaces at the start or end of the string, you will get the correct result:
barcode.trimmingCharacters(in: CharacterSet.decimalDigits.inverted)
This question already has answers here:
Proper way to return JSON using node or Express
(11 answers)
Closed 6 years ago.
collection.find({"topicname":topicname},function(err,result){
if(result){
console.dir("Success from database =\n"+result.toString());
console.dir(result);
console.log("This is it"+result);
res.send("Hello "+result);
}
});
The result is displayed in console in Json format, but when fetched in the browser, it displays Hello [object Object].
Database used is MongoDb
res.send("Hello " + JSON.stringify(result));
This converts the JSON object into a String and will then render it appropriately on your page when concatenating with another String as you are doing.
JSON.stringify
This question already has answers here:
How to check if an element is in an array
(18 answers)
Closed 7 years ago.
Swift 2 - so, I have an array and a uitextfield that a user inputs a string, I want to check whether the textfield.text is equal to ANY of the values in the array, can I do this with one line of code rather than lots of if's and else if's?!?
This is a generic code that will do what you are looking for. The if statement checks to see if a given value is equal to something that is located in the array. Simply replace the arr.contains() with the output you have given for your UITextfield.text Try to do a little research before you post. I can see that you are new here, so here is a little bit of help.
var arr = [1,2,3,4]
if arr.contains(3) {
//do something
}