How to convert a character to an integer in Swift 3 [duplicate] - swift3

This question already has answers here:
Convert Character to Int in Swift 2.0
(5 answers)
Closed 6 years ago.
I am trying to simply convert a character or string to an integer in Swift 3. I am a long time C++ developer who is just learning Swift and finding it extremely frustrating to do some of the simplest things in Swift 3 (ie, indexing into a string, converting a character to an int).
Can someone please help me convert characters to ints, specifically in Swift 3 Xcode 8?
Thank you!

It's as simple as this.
let myString = "5"
let sum = 3 + Int(myString)! // Produces 8
print(Int(myString)!)
Indexing
let str = "My test String Index"
let index = str.index(str.startIndex, offsetBy: 4)
str[index] // Returns e from test
let endIndex = str.index(str.endIndex, offsetBy:-2)
str[Range(index ..< endIndex)] // returns String "est String Ind"
str.substring(from: index) // returns String "est String Index"
str.substring(to: index) // returns String "My t"

let string = "1"
if let integer = Int(string) {
print(integer)
}

Related

Can someone tell me the output and provide explanation of the following c++ code? [duplicate]

This question already has answers here:
C++ character concatenation with std::string behavior. Please explain this
(3 answers)
Closed 1 year ago.
cout<<"#" + 'a'<<endl;
string s = "#";
s += 'a';
cout<<s<<endl;
I am not able to figure out how the typecasting is working in the case "#" + 'a'
In cpp string works like an array of characters, so when you assign s = '#' it compiles like this:
s[0] = '#'
and in the second line it actually compiles like this:
s[1] = 'a'
finally, s is:
#a

Strange behaviour while adding string to another string in C++ [duplicate]

This question already has answers here:
How can I repeat a string a variable number of times in C++?
(10 answers)
Closed 2 years ago.
one is 2, and ans is "000000".
string ans = "000000";
ans += string("1", one);
cout<<ans<<endl;
The output is:
0000001�
But I want the output:
00000011
What am I doing wrong?
string("1", one) does not do what you think it does. It does not duplicate the "1" string one number of times. It instead copies the 1st one number of chars from "1", which in this case is the '1' character and the '\0' null-terminator that follows it, which is where the � is coming from in the output. That is not what you want.
Use string(one, '1') instead. That will duplicate the '1' character one number of times, like you want, eg:
ans = "000000";
ans += string(one, '1');
cout << ans << endl;
Just use c++ strings and use + operator to catenate strings.

How to add elements at the beginning of an Arduino String. Similar to JS unshift();

I have an unknown size String, but with a maximum size of 8 and I want to make sure its size will always be 8 by adding 0(zeroes) at the beginning of the String.
For example the inputted String is "5687" and I have to add 0 until it's size is 8: "00005687".
String str = "5687";
while(str.length() < 8) {
// add 0 at the beginning of str
}
Unlike JavaScript, Arduino C++ doesn't have anything like unshift() or does it ?
How can I achieve my goal ? Thank you.
P.S: I am using an ESP32 and only a couple of Strings, because the handler of ESPAsyncWebServer usually return String parameters.
String str = "5687";
while(str.length() < 8) {
str = String("0") + str;
}

How to limit input length in JFX textField? [duplicate]

This question already has answers here:
JavaFX TextField Array max length of text value
(1 answer)
How to restrict TextField so that it can contain only one '.' character? JavaFX
(3 answers)
Java 8 U40 TextFormatter (JavaFX) to restrict user input only for decimal number
(1 answer)
Numeric TextField for Integers in JavaFX 8 with TextFormatter and/or UnaryOperator
(4 answers)
Closed 5 years ago.
I need to limit input length to 4 digits in JFX textField (field can be empty or have value 0-9999). Below solution works only partially - I can input only digits , but as many as i want - limit do not work. Even if I remove {0,4} from regex and
change IF condition to:
if(newText.matches("\d") && newText.length()>=0 && newText.length()<5)
it doesn't work too. Where is the error?
JFXtextField.textProperty().addListener((obs, oldText, newText) ->
{
if(newText.matches("\\d{0,4}"))
{
newText = newText;
}
else
{
newText = oldText;
}
});

Efficient extraction of several numbers from a character string in R [duplicate]

This question already has answers here:
R: convert list of numbers from character to numeric
(3 answers)
Closed 7 years ago.
Suppose I have the character string
x <- " 1.1325 -0.9022 -0.1832 -0.5479 0.1236 -0.6556 -1.0599 -0.8881 -0.2136"
and I want to extract the floats to end up with this vector as output:
c(1.1325, -0.9022, -0.1832, -0.5479, 0.1236, -0.6556, -1.0599, -0.8881, -0.2136)
What I managed to achieve is:
na.omit(as.numeric(strsplit(samp, split = " ")[[1]]))
My question: Is there a more efficient way?
We can use scan
scan(text=x, what=numeric(), quiet=TRUE)
#[1] 1.1325 -0.9022 -0.1832 -0.5479 0.1236 -0.6556 -1.0599 -0.8881 -0.2136