This question already has answers here:
How to convert an Int to a Character in Swift
(9 answers)
Closed 6 years ago.
I'm rewriting a library from java which uses something like
public int myFunc(char c){
return c+200
}
because in java 'a' + 1 will be 'b'
in Swift I cannot simply add an Int to a Character. Google search doesn't know anything about that.
Swift is a Strong Type Language. So the typecast is strict.
If you get ASCII value, you can like this:
let asciiValues = string.utf8.map{ Int($0) }
I hope that helps.
Related
This question already has answers here:
Need to check that braces in given array are balanced or not
(6 answers)
Closed 8 months ago.
I am trying to solve the problem of whether the parentheses in the array are closed correctly or not. For example, if two pairs of parentheses are closed correctly, there are two cases: "()()" "(())". I want to first find the number of all cases of 2 pairs of parentheses, and then verify that it is closed correctly or not.
What I'm thinking is that when I try to find 2 pairs, first of all, "(", "(", ")", ")" is to make all cases first with 2 open and 2 closed parentheses. How can I get all the cases with 2 open parentheses + 2 closed parentheses? Please help me.
In pseudo code:
unClosedCounter = 0
For char in string{
if char == "("
{
unClosedCounter++
}
if char == ")"{
if unClosedCounter == 0
{
return false
}
unClosedCounter--
}
}
Return unClosedCounter == 0
This question already has answers here:
Precision String Format Specifier In Swift
(31 answers)
Closed 2 years ago.
I need to transfer the double variable to the localization file, if I write %d I can only pass int, but I need to pass the double
You can pass the double with format string %f. To restrict the number of digits after the decimal point you can format is like this %0.2f. This will allow only 2 digits after the decimal point.
Sample Code:
let num = 10.5
let outputStr = String(format:"I am printing %0.1f", arguments:[num])
print(outputStr)
This question already has answers here:
Finding out whether a string is numeric or not
(18 answers)
Closed 7 years ago.
How can I check, if searchView contains just numbers?
I found this:
if newText.isMatchedByRegex("^(?:|0|[1-9]\\d*)(?:\\.\\d*)?$") { ... }
but it checks if text contains any number. How can I do, that if all text contains just numbers in Swift?
Here is the solution you can get all digits from String.
Swift 3.0 :
let testString = "asdfsdsds12345gdssdsasdf"
let phone = testString.components(separatedBy: CharacterSet.decimalDigits.inverted).joined(separator: "")
print(phone)
you can use "^[0-9]+$" instade "^(?:|0|[1-9]\\d*)(?:\\.\\d*)?$"
This will accept one or more digits, if you want to accept only one digit then remove +
This question already has answers here:
Objective c doesn't like my unichars?
(3 answers)
Closed 9 years ago.
I'm trying to add some specific characters to my
unichar rusLetter [] = { 'Ж', 'Й', }
I want actually to add all letters from russian alphabet. Using above line of code, i get an error:
Character too large for enclosing character literal type
Any ideas how to fix that? And maybe there is an easier way to add all letters, not to type all of them.
Thanks
the ' literal (single quote) means a char and that is too small to hold the symbols.
In ObjC use an NSString to hold it
NSString *rusLetters = #"ЖЙ";
unichar c1 = [rusLetters characterAtIndex:0];
unichar c2 = [rusLetters characterAtIndex:1];
This question already has answers here:
Convert erlang terms to string, or decode erlang binary
(2 answers)
Closed 9 years ago.
Is there a way how to convert a tuple to a string ?
Consider I have the following list :
[{atom,5,program},{atom,5,receiving},{nil,5}]
I wish to convert this into the following string:
"{atom,5,program},{atom,5,receiving},{nil,5}"
I have tried using erlang:tuple_to_list on each element in the list, which returns
A = [atom,5,program]
Eventually, I can't concatenate that with "{" ++ A ++ "}"
Any ideas how I can turn that to a string ?
Term = [{atom,5,program},{atom,5,receiving},{nil,5}].
lists:flatten(io_lib:format("~p", [Term])).