1. 字符串函数
String Functions, Helm 包含了一下字符串函数: abbrev, abbrevboth, camelcase, cat, contains, hasPrefix, hasSuffix, indent, initials, kebabcase, lower, nindent, nospace, plural, print, printf, println, quote, randAlpha, randAlphaNum, randAscii, randNumeric, repeat, replace, shuffle, snakecase, squote, substr, swapcase, title, trim, trimAll, trimPrefix, trimSuffix, trunc, untitle, upper, wrap, 和 wrapWith
1.1. print
返回各部分组合的字符串。
print "Matt has " .Dogs " dogs"
如果可能,非字符串类型会被转换成字符串。
注意,当相邻两个参数不是字符串时会在它们之间添加一个空格。
1.2. println
和 print效果一样,但会在末尾新添加一行。
1.3. printf
返回参数按顺序传递的格式化字符串。
printf "%s has %d dogs." .Name .NumberDogs
占位符取决于传入的参数类型。这包括:
一般用途:
%v
默认格式的值 当打印字典时,加号参数(%+v)可以添加字段名称%%
字符百分号,不使用值- 布尔值:%t true或false
整型:
%b
二进制%c
the character represented by the corresponding Unicode code point%d
十进制%o
8进制%O
带0o前缀的8进制%q
安全转义的单引号字符%x
16进制,使用小写字符a-f%X
16进制,使用小写字符A-F%U
Unicode格式: U+1234; 和"U+%04X"相同
浮点数和复杂成分
%b
指数二次幂的无小数科学计数法,比如 -123456p-78%e
科学计数法,比如:-1.234456e+78
%E
科学计数法,比如:-1.234456E+78
%f
无指数的小数,比如:123.456
%F
与%f
同义%g
%e
的大指数,否则是%f
%G
%E
的大指数,否则是%F
%x
十六进制计数法(和两个指数的十进制幂),比如:-0x1.23abcp+20
%X
大写的十六进制计数法,比如:-0X1.23ABCP+20
字符串和字节切片:
%s
未解析的二进制字符串或切片%q
安全转义的双引号字符串%x
十六进制,小写,每个字节两个字符%X
十六进制,大写,每个字节两个字符
切片:
- %p 16进制的第0个元素的地址,以0x开头
1.4. trim
trim 行数移除字符串两边的空格:
trim " hello "
上述结果为: hello
1.5. trimAll
从字符串中移除给定的字符:
trimAll "$" "$5.00"
上述结果为:5.00 (作为一个字符串)。
1.6. trimPrefix
从字符串中移除前缀:
trimPrefix "-" "-hello"
上述结果为:hello
1.7. trimSuffix
从字符串中移除后缀:
trimSuffix "-" "hello-"
上述结果为: hello
1.8. lower
将整个字符串转换成小写:
lower "HELLO"
上述结果为: hello
1.9. upper
将整个字符串转换成大写:
upper "hello"
上述结果为: HELLO
1.10. title
首字母转换成大写:
title "hello world"
上述结果为: Hello World
1.11. untitle
移除首字母大写:
untitle "Hello World"
会得到 hello world.
1.12. repeat
重复字符串多次:
repeat 3 "hello"
上述结果为: hellohellohello
1.13. substr
获取字符串的子串,有三个参数:
start (int)
end (int)
string (string)
substr 0 5 "hello world"
上述结果为: hello
1.14. nospace
去掉字符串中的所有空格:
nospace "hello w o r l d"
上述结果为: helloworld
1.15. trunc
截断字符串。
trunc 5 "hello world"
上述结果为: hello.
trunc -5 "hello world"
上述结果为: world.
1.16. abbrev
用省略号截断字符串 (...)
参数: 最大长度 字符串
abbrev 5 "hello world"
上述结果为: he...
, 因为将省略号算进了长度中。
1.17. bbrevboth
两边都省略
abbrevboth 5 10 "1234 5678 9123"
上述结果为: ...5678...
It takes:
左侧偏移值 最大长度 字符串
1.18. initials
截取给定字符串每个单词的首字母,并组合在一起。
initials "First Try"
上述结果为: FT
1.19. randAlphaNum, randAlpha, randNumeric, and randAscii
这四个字符串生成加密安全的(使用 crypto/rand)的随机字符串,但是字符集合不同:
randAlphaNum 使用 0-9a-zA-Z
randAlpha 使用 a-zA-Z
randNumeric 使用 0-9
randAscii 使用所有的可打印ASCII字符
每个函数都需要一个参数:字符串的整型长度
randNumeric 3
上述会生成三个数字的字符串。
1.20. wrap
以给定列数给文字换行。
wrap 80 $someText
上述结果会以 $someText 在80列处换行。
1.21. wrapWith
wrapWith 和 wrap 类似,但可以以指定字符串换行。(wrap 使用的是 \n)
wrapWith 5 "\t" "Hello World"
上述结果为: hello world (其中空格是ASCII tab字符)
1.22. contains
测试字符串是否包含在另一个字符串中:
contains "cat" "catch"
上述结果为: true 因为 catch 包含了 cat.
1.23. hasPrefix and hasSuffix
hasPrefix 和 hasSuffix 函数测试字符串是否有给定的前缀或后缀:
hasPrefix "cat" "catch"
上述结果为: true 因为 catch 有 cat.
该函数将字符串用双引号(quote) 或者单引号(squote)括起来。
1.24. cat
cat 函数将多个字符串合并成一个,用空格分隔:
cat "hello" "beautiful" "world"
上述结果为: hello beautiful world
1.25. indent
indent 以指定长度缩进给定字符串所在行,在对齐多行字符串时很有用:
indent 4 $lots_of_text
上述结果会将每行缩进4个空格。
1.26. nindent
nindent 函数和indent函数一样,但可以在字符串开头添加新行。
nindent 4 $lots_of_text
上述结果会在字符串所在行缩进4个字符,并且在开头新添加一行。
1.27. replace
执行简单的字符串替换。
需要三个参数: 待替换字符串 要替换字符串 源字符串
"I Am Henry VIII" | replace " " "-"
上述结果为: I-Am-Henry-VIII
1.28. plural
字符串复数化。
len $fish | plural "one anchovy" "many anchovies"
如上,如果字符串长度为1,则第一个参数会被打印(one anchovy)。否则,会打印第二个参数(many anchovies)。
参数包括:单数字符串 复数字符串 整型长度
注意: Helm 现在不支持多语言复杂的复数规则。0被认为是复数的因为英文中作为(zero anchovies) 对待。
1.29. snakecase
将驼峰写法转换成蛇形写法。
snakecase "FirstName"
上述结果为: first_name.
1.30. camelcase
将字符串从蛇形写法转换成驼峰写法。
camelcase "http_server"
上述结果为: HttpServer。
1.31. kebabcase
将驼峰写法转换成烤串写法。
kebabcase "FirstName"
上述结果为: first-name.
1.32. swapcase
基于单词的算法切换字符串的大小写。
转换算法:
- 大写字符变成小写字母
- 首字母变成小写字母
- 空格后或开头的小写字母转换成大写字母
- 其他小写字母转换成大写字母
- 通过unicode.IsSpace(char)定义空格
swapcase "This Is A.Test"
上述结果为: tHIS iS a.tEST.
1.33. shuffle
对字符串进行洗牌。
shuffle "hello"
上述hello的随机字符串可能会是oelhl。
1.34. quote
该函数将字符串用双引号(quote) 括起来。
如 .Values.foo 为 bar
quote .Values.foo
或
.Values.foo | quote
输出: "bar"
1.35. squote
该函数将字符串用单引号(squote)括起来。
如 .Values.foo 为 bar
squote .Values.foo
或
.Values.foo | squote
输出: 'bar'