将输出整数分成两个值

时间:2012-11-01 21:03:00

标签: javascript split integer output

var currentPageURL = "www.javascript.com/dun/1.70 80";
var url = getMeasurementsFromUrl(currentPageURL)

document.write(url);

function getMeasurementsFromUrl(url)
{

var urlMeasurmentString = url.substring(url.lastIndexOf('/dun/')+5);

urlMeasurmentString =urlMeasurmentString.replace("abcdefghijklmnopqrstuvwxyz",""); 
urlMeasurmentString =urlMeasurmentString.replace(",","."); 
urlMeasurmentString =urlMeasurmentString.replace("/","."); 
urlMeasurmentString =urlMeasurmentString.replace("-",".");
urlMeasurmentString =urlMeasurmentString.replace(",","."); 
urlMeasurmentString =urlMeasurmentString.replace("/","."); 
urlMeasurmentString =urlMeasurmentString.replace("-",".");
return urlMeasurmentString;
}

我需要将输出“1.70 80”分成两个单独的整数。 http://jsfiddle.net/mqWGm/

3 个答案:

答案 0 :(得分:0)

如果你想要1.70和80,那么你可以做到

urlMeasurementString.split(" ");

这将返回一个数组,其中第一个元素是1.70,第二个元素是80;

答案 1 :(得分:0)

var currentPageURL = "www.javascript.com/dun/1.70 80";
var url = getMeasurementsFromUrl(currentPageURL)
var integers = url.split(' ');
for(var i = 0; i<integers.length; i++)
{
    integers[i] = parseFloat(integers[i],2);
}
console.log(integers);

function getMeasurementsFromUrl(url)
{

var urlMeasurmentString = url.substring(url.lastIndexOf('/dun/')+5);

urlMeasurmentString =urlMeasurmentString.replace("abcdefghijklmnopqrstuvwxyz","");
urlMeasurmentString =urlMeasurmentString.replace(",",".");
urlMeasurmentString =urlMeasurmentString.replace("/",".");
urlMeasurmentString =urlMeasurmentString.replace("-",".");
urlMeasurmentString =urlMeasurmentString.replace(",",".");
urlMeasurmentString =urlMeasurmentString.replace("/",".");
urlMeasurmentString =urlMeasurmentString.replace("-",".");
return urlMeasurmentString;
}

或更好:

var currentPageURL = "www.javascript.com/dun/1.70 80";
console.log(getIntegers(currentPageURL));

function getIntegers(url){
    url = url.split('/');
    url = url[url.length-1].split(' ');
    for(var i = 0; i<url.length; i++)
    {
        url[i] = parseFloat(url[i],2);
    }
    return url;
}

答案 2 :(得分:0)

1.70不是整数,但是如果你需要字符串中的数字&#34; 1.70 80&#34;你可以做以下的事情:

var atoms = url.split(" ");
var num1 = parseFloat(atoms[0]);
var num2 = parseFloat(atoms[1]);