替换字符串中除最后4个字符以外的所有字符

时间:2021-02-22 01:59:30

Using Go, how do I replace all characters in a string with "X" except the last 4 characters?

使用Go,除了最后4个字符外,如何用“X”替换字符串中的所有字符?

This works fine for php/javascript but not for golang as "?=" is not supported.

这适用于php / javascript但不适用于golang,因为不支持“?=”。

\w(?=\w{4,}$)

Tried this, but does not work. I couldn't find anything similar for golang

试过这个,但不起作用。我找不到任何类似golang的东西

(\w)(?:\w{4,}$)

JavaScript working link

JavaScript工作链接

Go non-working link

去非工作链接

3 个解决方案

#1


6  

A simple yet efficient solution that handles multi UTF-8-byte characters is to convert the string to []rune, overwrite runes with 'X' (except the last 4), then convert back to string.

处理多个UTF-8字节字符的简单而有效的解决方案是将字符串转换为[]符文,用'X'覆盖符文(除了最后4个),然后转换回字符串。

func maskLeft(s string) string {
    rs := []rune(s)
    for i := 0; i < len(rs)-4; i++ {
        rs[i] = 'X'
    }
    return string(rs)
}

Testing it:

测试它:

fmt.Println(maskLeft("123"))
fmt.Println(maskLeft("123456"))
fmt.Println(maskLeft("1234世界"))
fmt.Println(maskLeft("世界3456"))

Output (try it on the Go Playground):

输出(在Go Playground上试试):

123
XX3456
XX34世界
XX3456

Also see related question: How to replace all characters in a string in golang

另请参阅相关问题:如何在golang中替换字符串中的所有字符

#2


0  

Create a Regexp with

用。创建一个正则表达式

re := regexp.MustCompile("\w{4}$")

Let's say inputString is the string you want to remove the last four characters from. Use this code to return a copy of inputString without the last 4 characters:

假设inputString是要从中删除最后四个字符的字符串。使用此代码返回inputString的副本,不包含最后4个字符:

re.ReplaceAllString(inputString, "")

Note: if it's possible that your input string could start out with less than four characters, and you still want those characters removed since they are at the end of the string, you should instead use:

注意:如果您的输入字符串可能以少于四个字符开头,并且您仍然希望删除这些字符,因为它们位于字符串的末尾,您应该使用:

re := regexp.MustCompile("\w{0,4}$")

#3


0  

Let's say inputString is the string you want to mask all the characters of (except the last four).

假设inputString是要屏蔽所有字符的字符串(除了最后四个字符)。

First get the last four characters of the string:

首先获取字符串的最后四个字符:

last4 := string(inputString[len(inputString)-4:])

Then get a string of X's which is the same length as inputString, minus 4:

然后得到一个X的字符串,其长度与inputString相同,减去4:

re := regexp.MustCompile("\w")
maskedPart := re.ReplaceAllString(inputString[0:len(inputString)-5], "X")

Then combine maskedPart and last4 to get your result:

然后结合使用maskedPart和last4来获得结果:

maskedString := strings.Join([]string{maskedPart,last4},"")

#1


6  

A simple yet efficient solution that handles multi UTF-8-byte characters is to convert the string to []rune, overwrite runes with 'X' (except the last 4), then convert back to string.

处理多个UTF-8字节字符的简单而有效的解决方案是将字符串转换为[]符文,用'X'覆盖符文(除了最后4个),然后转换回字符串。

func maskLeft(s string) string {
    rs := []rune(s)
    for i := 0; i < len(rs)-4; i++ {
        rs[i] = 'X'
    }
    return string(rs)
}

Testing it:

测试它:

fmt.Println(maskLeft("123"))
fmt.Println(maskLeft("123456"))
fmt.Println(maskLeft("1234世界"))
fmt.Println(maskLeft("世界3456"))

Output (try it on the Go Playground):

输出(在Go Playground上试试):

123
XX3456
XX34世界
XX3456

Also see related question: How to replace all characters in a string in golang

另请参阅相关问题:如何在golang中替换字符串中的所有字符

#2


0  

Create a Regexp with

用。创建一个正则表达式

re := regexp.MustCompile("\w{4}$")

Let's say inputString is the string you want to remove the last four characters from. Use this code to return a copy of inputString without the last 4 characters:

假设inputString是要从中删除最后四个字符的字符串。使用此代码返回inputString的副本,不包含最后4个字符:

re.ReplaceAllString(inputString, "")

Note: if it's possible that your input string could start out with less than four characters, and you still want those characters removed since they are at the end of the string, you should instead use:

注意:如果您的输入字符串可能以少于四个字符开头,并且您仍然希望删除这些字符,因为它们位于字符串的末尾,您应该使用:

re := regexp.MustCompile("\w{0,4}$")

#3


0  

Let's say inputString is the string you want to mask all the characters of (except the last four).

假设inputString是要屏蔽所有字符的字符串(除了最后四个字符)。

First get the last four characters of the string:

首先获取字符串的最后四个字符:

last4 := string(inputString[len(inputString)-4:])

Then get a string of X's which is the same length as inputString, minus 4:

然后得到一个X的字符串,其长度与inputString相同,减去4:

re := regexp.MustCompile("\w")
maskedPart := re.ReplaceAllString(inputString[0:len(inputString)-5], "X")

Then combine maskedPart and last4 to get your result:

然后结合使用maskedPart和last4来获得结果:

maskedString := strings.Join([]string{maskedPart,last4},"")