Working on a project where I get an input where its a comma delimited string like:
"255,10000000,42949672950254,12,22".
and then the numbers (no commas), would be split into 6 Long variables like
var1 = 255
var2 = 10000000
...
I wanted a function like this:
void extractIntegersFromString(String str, int& integer1, int& integer2, int& integer3, int& integer4, int& integer5, int& integer6, int& integer7) {
int count = 0;
String temp = "";
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == ',') {
if (count == 0)
integer1 = String(temp).toInt();
else if (count == 1)
integer2 = String(temp).toInt();
else if (count == 2)
integer3 = String(temp).toInt();
else if (count == 3)
integer4 = String(temp).toInt();
else if (count == 4)
integer5 = String(temp).toInt();
else if (count == 5)
integer6 = String(temp).toInt();
else if (count == 6)
integer7 = String(temp).toInt();
count++;
temp = "";
} else if (c >= '0' && c <= '9') {
temp += c;
}
}
// Store the last integer if the string doesn't end with a comma
if (temp != "") {
if (count == 0)
integer1 = String(temp).toInt();
else if (count == 1)
integer2 = String(temp).toInt();
else if (count == 2)
integer3 = String(temp).toInt();
else if (count == 3)
integer4 = String(temp).toInt();
else if (count == 4)
integer5 = String(temp).toInt();
else if (count == 5)
integer6 = String(temp).toInt();
else if (count == 6)
integer7 = String(temp).toInt();
}
}
This function makes it work through ints, but I need the larger size of a Long.
P.S. I've tried the atol()
function (and similar others), but that didn't work...