我有一个 SASS/SCSS 字符串,其中包含两个列表(以逗号分隔),每个列表都包含数字(以空格分隔)。如何将字符串拆分为两个数字列表?
SCSS:
$values: "10px 20px 30px, 20px 30px 40px";
$begin: /* should be */ "10px", "20px", "30px";
$end: /* should be */ "20px", "30px", "40px";
// optionally it can be a map:
$begin: (10px, 20px, 30px);
$end: (20px, 30px, 40px);
Sass Meister 上的代码:http://sassmeister.com/gist/4d9c1bd741177636ae1b
好吧,你可以用这个函数分割字符串:
字符串分割
@function str-split($string, $separator) {
// empty array/list
$split-arr: ();
// first index of separator in string
$index : str-index($string, $separator);
// loop through string
@while $index != null {
// get the substring from the first character to the separator
$item: str-slice($string, 1, $index - 1);
// push item to array
$split-arr: append($split-arr, $item);
// remove item and separator from string
$string: str-slice($string, $index + 1);
// find new index of separator
$index : str-index($string, $separator);
}
// add the remaining string to list (the last item)
$split-arr: append($split-arr, $string);
@return $split-arr;
}
Usage
在你的情况下,你可以这样使用它:
$values: "10px 20px 30px, 20px 30px 40px";
$list: ();
$split-values: str-split($values, ", ");
@each $value in $split-values {
$list: append($list, str-split($value, " "));
}
转换为数字
至于将字符串值转换为数字,请查看 Hugo Giraudel 的函数萨斯梅斯特(或阅读他的博客文章)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)