警报已弃用:Stdlib.String.set
Alert deprecated: Stdlib.String.set
以下代码 returns 一个错误,表示该语法已弃用。更改字符串中字符的正确方法是什么?
let hello = "Hello!" ;;
hello.[1] <- 'a' ;;
Alert deprecated: Stdlib.String.set
Use Bytes.set instead.
Error: This expression has type string but an expression was expected of type
bytes
字符串是不可变的(或者至少很快就会是),因此您不能更改它们的内容。当然,您可以创建一个字符串的副本,其中一个字符不同,例如
let with_nth_char m c =
String.mapi (fun i b -> if i = m then c else b)
和
# with_nth_char 1 'E' "hello";;
- : string = "hEllo"
但是,如果您需要更改数组中的字符,则不应使用 string
数据类型,而应依赖 bytes
可变字符串类型。您可以使用 Bytes.of_strings
和 Bytes.to_string
将字符串转换为字节,反之亦然。
以下代码 returns 一个错误,表示该语法已弃用。更改字符串中字符的正确方法是什么?
let hello = "Hello!" ;;
hello.[1] <- 'a' ;;
Alert deprecated: Stdlib.String.set
Use Bytes.set instead.
Error: This expression has type string but an expression was expected of type
bytes
字符串是不可变的(或者至少很快就会是),因此您不能更改它们的内容。当然,您可以创建一个字符串的副本,其中一个字符不同,例如
let with_nth_char m c =
String.mapi (fun i b -> if i = m then c else b)
和
# with_nth_char 1 'E' "hello";;
- : string = "hEllo"
但是,如果您需要更改数组中的字符,则不应使用 string
数据类型,而应依赖 bytes
可变字符串类型。您可以使用 Bytes.of_strings
和 Bytes.to_string
将字符串转换为字节,反之亦然。