Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

jquery - Finding numbers in a string and adding them together

I was wondering if it were possible with jQuery to add together numbers in a string...

The strings can only be added to themselves as shown in the examples below. If one or more of the string appears, it should be combined.

All the possible strings: click here

Example with possible strings:

+0.53 attack damage
+0.53 attack damage
= 1.06 attack damage

+8.75 mana
+8.75 mana
= 17.5 mana

Each of these can be combined up to 9 times.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Iterate through the characters of the string (by making substrings with length 1); once you detect a number or a decimal point, save the start position in a variable. Then, once you detect something that is not a number or decimal point, or a second decimal point (count the number of decimal points), the number has ended; save the end position and voilà, you have the start and end position of the number and can get it by making a substring.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...