简体   繁体   English

JavaScript:字符串提取

[英]JavaScript: String extraction

If my String is like this: 如果我的字符串是这样的:

var dollarValue = "$7.50;$15;50%";

What would be the best method of extracting each value? 提取每个值的最佳方法是什么? Each semicolon is used as a separator between the part of the string that matters. 每个分号都用作重要字符串部分之间的分隔符。 So like: 像这样:

$7.50 $ 7.50

$15 $ 15

50% 50%

I understand how to extract two values with only one semicolon, but I'm not sure how I would continue on down the string using each subsequent semicolon as the next starting point... 我知道如何仅用一个分号提取两个值,但是我不确定如何使用后续的每个分号作为下一个起点继续处理字符串...

怎么回事String.split()

var arrayOfValues = dollarValue.split(";"); // ["$7.50", "$15", "50%"]

You can use the split method: 您可以使用split方法:

var values = dollarValue.split(";");

This will return an array with the values between the semicolons. 这将返回一个包含分号之间的值的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM