Remove first character from string [duplicate] - swift

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)

Related

Why I can't use string by index from array? [duplicate]

This question already has an answer here:
What is the point of cell indexing in MATLAB
(1 answer)
Closed 5 months ago.
I'm new in Matlab, can somebody tell me, why I can't use a string by index like this?
adresuSarasas = {
"C:\\ExperimentData\\volunteer01.txt",
"C:\\ExperimentData\\volunteer02.txt",
"C:\\ExperimentData\\volunteer03.txt",
"C:\\ExperimentData\\volunteer04.txt",
"C:\\ExperimentData\\volunteer05.txt",
"C:\\ExperimentData\\volunteer06.txt",
"C:\\ExperimentData\\volunteer07.txt",
"C:\\ExperimentData\\volunteer08.txt",
"C:\\ExperimentData\\volunteer09.txt",
"C:\\ExperimentData\\volunteer10.txt",
}
skirtukas=';';
pradzia=1;
V1=importdata(adresuSarasas(2), skirtukas, pradzia)
I get error on the last line. error: importdata: FNAME must be a string. Thanks.
cell arrays have different output when indexed differently.
adresuSarasas(2) returns a 1x1 cell array that contains your string, while your function expects a string, not a cell array.
adresuSarasas{2} returns the string inside the cell array which is what your function is expecting.
while later version of matlab have functions accept 1x1 cell arrays (so this code works on newer versions) it's still fruitful to know the difference when working with older versions.

How to replace spaces in string with underscore [duplicate]

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: "_")

check URL text as a string in Xcode [duplicate]

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")
}

Checking all values of an array [duplicate]

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
}

JSON string splitting / parsing [duplicate]

This question already has answers here:
iPhone/iOS JSON parsing tutorial [closed]
(6 answers)
Closed 9 years ago.
I have a string that looks like this:
"[{"ImageTagID":78,"Xpixel":408,"Ypixel":69,"Xpercent":17,"Ypercent":68,"ImageID":45617}]"
How do i break it up to pieces?
Thanks,
This is a dictionary. There are keys and values in a dictionary. If you need a particular value, you must get it using the key.
In iOS it is done this way,
int tagID = [yourdictionary objectForKey:#"ImageTagID"];
Hope this helps you start. If you have more questions, do ask me.
Splitting a string:
Example: NSArray *stringArray = [string componentsSeparatedByString: #","];