Javascript:如何获取字符串中的前 4 个数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11096977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: How to get the first 4 numbers in a string?
提问by like-a-trainee
mystring = "9862 ....... -pack size 1 - SST Unspun Label (Roll) CAT#: 9862"
I want to get the first 4 numbers in this string "9862". How do I get the first sequence of numbers in this string. And store it in anothoer variable?
我想获取此字符串“9862”中的前 4 个数字。如何获取此字符串中的第一个数字序列。并将其存储在另一个变量中?
Javascript doesn't seem to be recognizing this variable. I don't know why. if I do:
Javascript 似乎无法识别此变量。我不知道为什么。如果我做:
alert(mystring); //I don't get an alert pop up nor does it show any errors.
Could there something be wrong with the text I'm trying to store in 'mystring' variable?
我试图存储在“mystring”变量中的文本是否有问题?
回答by Someth Victory
mystring = "9862 ....... -pack size 1 - SST Unspun Label (Roll) CAT#: 9862";
mystring = mystring.substring(0, 4);
alert(mystring.trim());
回答by Mageek
You can do either:
您可以执行以下任一操作:
mystring=mystring.substring(from,to);
or
或者
mystring=mystring.substr(start,length);
If the start is 0 (like your case), the two ways will be the same.
如果开始是 0(就像你的情况),两种方式是一样的。
Sources:
资料来源:
http://www.w3schools.com/jsref/jsref_substr.asp
http://www.w3schools.com/jsref/jsref_substring.asp
http://www.w3schools.com/jsref/jsref_substr.asp
http://www.w3schools.com/jsref/jsref_substring.asp
回答by chrs
var extractInts = function(str) {
r = [];
for ( var i = 1, l = str.length; i <= l; ++i ) {
if ( !isNaN(parseInt(str.substring(i-1, i),0))) {
str = str.substring(i-1);
r.push(parseInt(str, 0));
str = str.substring( parseInt(str,0).toString().length);
i = 1;
}
}
return r;
};
var str = "Hi, here is an int 1, and another 3, and finally 37";
var i_array = extractInts(str);
console.log("extractInts: " + i_array[0]); //Prints 1
The above function will extract every integer into an array, and hence you can just extract the first.
上面的函数会将每个整数提取到一个数组中,因此您可以只提取第一个。
回答by Wiktor Stribi?ew
Since the question is to find the first 4 digits in any string, a regex solution is also possible: /\d{4}/
or /[0-9]/
. See a regex demo.
由于问题是在任何字符串中找到前 4 位数字,因此也可以使用正则表达式解决方案:/\d{4}/
或/[0-9]/
. 请参阅正则表达式演示。
A regex solution is more flexible since it allows to extract the four digits in various contexts:
正则表达式解决方案更灵活,因为它允许在各种上下文中提取四位数字:
/^\d{4}/ // To match any first 4 digits at the start of a string
/\d{4}/ // To match any first 4 digits anywhere inside a string
/word.*?(\d{4})/ // To match and capture any first 4 digits after a specific `word`
See a demo below:
请参阅下面的演示:
var mystring = "9862 ....... -pack size 1 - SST Unspun Label (Roll) CAT#: 9862";
console.log(mystring.match(/^\d{4}/)[0]); // The first 4 digits at the start of a string
console.log("Year: 2018".match(/\d{4}/)[0]); // The first 4 digits anywhere in a string
console.log("Year: 2012".match(/Year.*(\d{4})/)[1]); // The first 4 digits after Year
回答by Laughing
Try this:
尝试这个:
mystring = "9862 ....... -pack size 1 - SST Unspun Label (Roll) CAT#: 9862";
var str=mystring.substr(0,4);
alert(str);
回答by RAKESH HOLKAR
You can use to function to get first 4 char
您可以使用 to 函数来获取前 4 个字符
mystring = "9862 ....... -pack size 1 - SST Unspun Label (Roll) CAT#: 9862"
mystring = mystring.substr(0,4);
or
或者
mystring = mystring.substring(0,4);
both will work fine...try it
两者都可以正常工作......试试吧