No, no function provides the automatic cast. However you can cast with this simple hack (the cast is automatically made by PHP in internal):
float = "1.23"+0;
for generic number:
$yourNumberCasted = $yourStringNumber + 0;
With a function:
function castToNumber($genericStringNumber) {
return $genericStringNumber+0;
}
$yourNumberCasted = castToNumber($yourStringNumber);
Answer from antoox on Stack OverflowHack: Cast of any type to int / float / string is permitted
php - Cast string to either int or float - Stack Overflow
c# - Converting string to integer by selecting the elements to stay - Stack Overflow
`String -> int` function
Videos
No, no function provides the automatic cast. However you can cast with this simple hack (the cast is automatically made by PHP in internal):
float = "1.23"+0;
for generic number:
$yourNumberCasted = $yourStringNumber + 0;
With a function:
function castToNumber($genericStringNumber) {
return $genericStringNumber+0;
}
$yourNumberCasted = castToNumber($yourStringNumber);
You can fulfil your requirement using following line.
$i = "1.23";
echo $c = (double)$i; // will return 1.23
$i = "123";
echo $c = (double)$i; // will return 123
This might do the trick for you
string serialcomnum = Regex.Match(serialcom,"\\d+").ToString();
int ints = Convert.ToInt32(serialcomnum);
Another way could be
string serialcom = "$67%!$127a%";
var serialcomnum = Regex.Replace(serialcom, @"[^0-9]+", ",");
serialcomnum = serialcomnum.TrimStart(',').TrimEnd(',');
int[] serialints = serialcomnum.Split(',').Select(int.Parse).ToArray();
Probably a simple Regex is the easiest solution without using other string parsing methods
string input = "$67%!$127a%";
var matches = Regex.Matches(input, @"[0-9]+", RegexOptions.Compiled);
var result = matches.Cast<Match>()
.Select(x => Int32.Parse(x.Value)).ToArray();
foreach (int x in result)
Console.WriteLine(x);
Here the example expects more than one number inside your input string and returns an array of integers. If this is not the case (only one integer expected) then you can replace the ToArray with a SingleOrDefault() and assign the result to an integer variable.
Also, if you have numbers bigger than Int32.MaxValue, then you should change the Int32.Parse with Int64.Parse.
You can use the strval() function to convert a number to a string.
From a maintenance perspective its obvious what you are trying to do rather than some of the other more esoteric answers. Of course, it depends on your context.
$var = 5;
// Inline variable parsing
echo "I'd like {$var} waffles"; // = I'd like 5 waffles
// String concatenation
echo "I'd like ".$var." waffles"; // I'd like 5 waffles
// The two examples above have the same end value...
// ... And so do the two below
// Explicit cast
$items = (string)$var; // $items === "5";
// Function call
$items = strval($var); // $items === "5";
There's many ways to do this.
Two examples:
$str = (string) $int;
$str = "$int";
See the PHP Manual on Types Juggling for more.
tested it for 10m assignments of the number 10
One:
real 0m5.610s
user 0m5.098s
sys 0m0.220s
Two:
real 0m6.216s
user 0m5.700s
sys 0m0.213s
Three:
real 0m12.986s
user 0m11.767s
sys 0m0.489s
One seems to win
Edit: JVM is standard '/usr/bin/java' under Mac OS X 10.5
java version "1.5.0_16" Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_16-b06-284) Java HotSpot(TM) Client VM (build 1.5.0_16-133, mixed mode, sharing)
More edit:
Code as requested
public class One {
public static void main(String[] args) {
int someValue = 10;
for (int i = 0; i < 10000000; i++) {
String stringValue = Integer.toString(someValue);
}
}
}
case 2 and 3 similarly
run using
javac *.java; time java One; time java Two; time java Three
Even though according to the measurements of cobbal, #1 seems to be the fastest, I'd strongly recommend the usage of String.valueOf(). My reason for that is that this call does not explicitly contain the type of the argument, so if later on you decide to change it from int to double, there is no need to modify this call. The speed gain on #1 compared to #2 is only minimal, and as we all know, "premature optimization is the root of all evil".
The third solution is out of the question, since it implicitly creates a StringBuilder and appends the components (in this case, the number and the empty string) to that, and finally converts that to a string.