如果你在编程语言中使用字符串,你会对Sass字符串函数感到很舒服,但有一个例外:Sass字符串不是基于零的数组。 Sass字符串中的第一个字符是索引1,而不是0。
例如。 Sass函数str-index($string,$substring)返回$string中第一次出现$substring的位置。 等效的JavaScript函数是indexOf(<substring>)方法。 但是给定相同的参数,它们不会返回相同的值:
/** * 时间:2019年8月24日 * 前端教程: https://www.pipipi.net/ */ $str-index("hello","h"); //Sass returns 1 "hello".indexOf("h"); //JavaScript returns 0
下表列出了Sass中的所有字符串函数。
函数 | 描述 | 例子 |
---|---|---|
unquote($string) | 删除字符串周围的引号 | unquote("hello") Result: hello |
quote($string) | 在不加引号的字符串中添加引号; 返回未修改的带引号的字符串 | quote(hello) Result: "hello" quote("hello") Result: "hello" |
str-length($string) | 返回字符串中的字符数 | str-length("hello") Result: 5 |
str-insert($string, $insert, $index) | 在字符串内的指定索引位置插入指定为$ insert的字符串 | str-insert(" world", "hello", 0) Result: "hello world" |
str-index($string, $substring) | 返回$string内第一次出现$substring的索引,如果找不到该字符串,则返回null | str-index("Hello", "H") Result: 1 str-index("hello", "H") Result: null |
str-slice($string, $start-at, [$end-at]) | 返回从位置$start-at开始并可选地在位置$end-at结束的子字符串 | str-slice("hello, world", 8) "world" str-slice("hello, world", 8, 9) Result: "wo" |
to-upper-case($string) | 返回包含$ string的字符串转换为大写 | to-upper-case("hello") Result:"HELLO" |
to-lower-case($string) | 返回包含$ string转换为小写的字符串 | to-lower-case("Hello") "hello" |