首页 > 文章列表 > 在 GOLANG 中对来自 HTML 文本区域的字符串进行拆分可能导致意外结果

在 GOLANG 中对来自 HTML 文本区域的字符串进行拆分可能导致意外结果

382 2024-02-17
问题内容

我有一个字符串变量str,它保存的值是

来自 HTML texarea

我正在尝试用新行分割字符串

fmt.Println("befor split:")
    fmt.Println(str)
    x := strings.Split(str, "n")
fmt.Println("after split:")
    fmt.Println(x)

这就是我得到的


正确答案


不同类型的行结束符:根据平台的不同,换行符可以表示为 rn (Windows)、n (Unix/Linux) 或 r (Old Mac)。

const textareaValue = document.getElementById("yourTextareaId").value;
const lines = textareaValue.split(/r?n/); // This will handle both rn and n