如何从Golang中的字符串中修剪“[”char

时间:2016-11-23 09:28:42

标签: string go trim

可能是一件愚蠢的事,但有点卡住了......

无法修剪字符串中的"["字符,我尝试输出的内容:

package main

import (
    "fmt"
    "strings"
)

func main() {
    s := "this[things]I would like to remove"
    t := strings.Trim(s, "[")

    fmt.Printf("%s\n", t)   
}

// output: this[things]I would like to remove

go playground

还尝试了所有这些,但没有成功:

s := "this [ things]I would like to remove"
t := strings.Trim(s, " [ ")
// output: this [ things]I would like to remove


s := "this [ things]I would like to remove"
t := strings.Trim(s, "[")
// output: this [ things]I would like to remove

没有用。我在这里缺少什么?

1 个答案:

答案 0 :(得分:33)

你错过了阅读文档。 strings.Trim()

<input type="tel" minlength="10" maxlength="10" id="mobile" name="mobile" title="10 digit mobile number" required>


$('#mob_frm').submit(function(e) {
            e.preventDefault();
            if(!$('#mobile').val().match('[0-9]{10}'))  {
                alert("Please put 10 digit mobile number");
                return;
            }  

        });
     

Trim返回字符串s的一部分,其中包含cutset中包含的所有前导<尾部 Unicode代码点。

输入中的func Trim(s string, cutset string) string 字符不在前导中,也不在尾随位置,它位于中间中,所以[ - 行为良好 - 不会将其删除。

请尝试strings.Replace()

strings.Trim()

输出(在Go Playground上尝试):

s := "this[things]I would like to remove"
t := strings.Replace(s, "[", "", -1)
fmt.Printf("%s\n", t)