admin管理员组文章数量:1327803
I have a string from which I am trying to get a specif value. The value is buried in the middle of the string. For example, the string looks like this:
Content1Save
The value I want to extract is "1";
Currently, I use the built-in substring function to get to remove the left part of the string, like this:
MyString = "Content1Save";
Position = MyString;
Position = Position.substring(7);
alert(Position); // alerts "1Save"
I need to get rid of the "Save" part and be left with the 1;
How do I do that?
+++++++++++++++++++++++++++++++++++++++++ ANSWER
Position = Position.substr(7, 1);
QUESTION
What's the difference between these two?
Position = Position.substr(7, 1);
Position = Position.substring(7, 1);
I have a string from which I am trying to get a specif value. The value is buried in the middle of the string. For example, the string looks like this:
Content1Save
The value I want to extract is "1";
Currently, I use the built-in substring function to get to remove the left part of the string, like this:
MyString = "Content1Save";
Position = MyString;
Position = Position.substring(7);
alert(Position); // alerts "1Save"
I need to get rid of the "Save" part and be left with the 1;
How do I do that?
+++++++++++++++++++++++++++++++++++++++++ ANSWER
Position = Position.substr(7, 1);
QUESTION
What's the difference between these two?
Position = Position.substr(7, 1);
Position = Position.substring(7, 1);
Share
Improve this question
edited Aug 16, 2011 at 17:28
Evik James
asked Aug 16, 2011 at 17:06
Evik JamesEvik James
10.5k19 gold badges76 silver badges123 bronze badges
4
- 1 Do you KNOW that you will be saving a NUMBER from the middle of a string with nothing but LETTERS around it? – josh.trow Commented Aug 16, 2011 at 17:08
- 1 Do the strings follow any general pattern? – Felix Kling Commented Aug 16, 2011 at 17:08
- The number in the string will always be a 1, 2, 3, or 4. – Evik James Commented Aug 16, 2011 at 17:25
- Regarding your update, have a look at developer.mozilla/en/JavaScript/Reference/Global_Objects/… and developer.mozilla/en/JavaScript/Reference/Global_Objects/… – Felix Kling Commented Aug 16, 2011 at 17:35
4 Answers
Reset to default 4You can use the substr
[MDN] method. The following example gets the 1 character long substring starting at index 7.
Position = Position.substr(7, 1);
Or, you can use a regex.
Position = /\d+/.exec(Position)[0];
I would suggest looking into regex, and groups.
Regex is built essentially exactly for this purpose and is built in to javascript.
Regex for something like Content1Save
would look like this:
rg = /^[A-Za-z]*([0-9]+)[A-Za-z]*$/
Then you can extract the group using:
match = rg.exec('Content1Save');
alert(match[1]);
More on regex can be found here: http://en.wikipedia/wiki/Regular_expression
It highly depends on the rules you have for that middle part. If it's just a character, you can use Position = Position.substring(0, 1)
. If you're trying to get the number, as long as you have removed the letters before it, you can use parseInt
.
alert(parseInt("123abc")); //123
alert(parseInt("foo123bar")); //NaN
If you're actually trying to search, you'll more often than not need to use something called Regular Expressions. They're the best search syntax JavaScript avails.
var matches = Position.match(/\d+/)
alert(matches[0])
Otherwise you can use a series of substr's, but that implies you know what is in the string to begin with:
MyString.substr(MyString.indexOf(1), 1);
But that is a tad annoying.
本文标签: JavaScriptHow to get at specific value in a stringStack Overflow
版权声明:本文标题:JavaScript - How to get at specific value in a string? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742234477a2437820.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论