如何屏蔽字符串中除最后四个字符之外的所有字符 [英] How to mask all but last four characters in a string
问题描述
我一直在尝试进行编码练习,以屏蔽除任何输入的最后四位数字或字符之外的所有数字或字符.
I've been attempting a coding exercise to mask all but the last four digits or characters of any input.
我认为我的解决方案有效,但似乎有点笨拙.有没有人有关于如何重构它的想法?
I think my solution works but it seems a bit clumsy. Does anyone have ideas about how to refactor it?
这是我的代码:
def mask(string)
z = string.to_s.length
if z <= 4
return string
elsif z > 4
array = []
string1 = string.to_s.chars
string1[0..((z-1)-4)].each do |s|
array << "#"
end
array << string1[(z-4)..(z-1)]
puts array.join(", ").delete(", ").inspect
end
end
推荐答案
positive lookahead
正向前瞻 让这一切变得非常简单.如果任何字符后跟至少 4 个字符,它将被替换:
positive lookahead
A positive lookahead makes it pretty easy. If any character is followed by at least 4 characters, it gets replaced :
"654321".gsub(/.(?=.{4})/,'#')
# "##4321"
这是正则表达式的描述:
Here's a description of the regex :
r = /
. # Just one character
(?= # which must be followed by
.{4} # 4 characters
) #
/x # free-spacing mode, allows comments inside regex
请注意,正则表达式一次只匹配一个字符,即使每次匹配最多需要检查 5 个字符:
Note that the regex only matches one character at a time, even though it needs to check up to 5 characters for each match :
"654321".scan(r)
# => ["6", "5"]
/(.)..../
不起作用,因为每次迭代会消耗 5 个字符:
/(.)..../
wouldn't work, because it would consume 5 characters for each iteration :
"654321".scan(/(.)..../)
# => [["6"]]
"abcdefghij".scan(/(.)..../)
# => [["a"], ["f"]]
如果要参数化未屏蔽字符串的长度,可以使用 变量插值 :
If you want to parametrize the length of the unmasked string, you can use variable interpolation :
all_but = 4
/.(?=.{#{all_but}})/
# => /.(?=.{4})/
代码
把它打包成一个方法,就变成了:
Code
Packing it into a method, it becomes :
def mask(string, all_but = 4, char = '#')
string.gsub(/.(?=.{#{all_but}})/, char)
end
p mask('testabcdef')
# '######cdef'
p mask('1234')
# '1234'
p mask('123')
# '123'
p mask('x')
# 'x'
你也可以将它改编成句子:
You could also adapt it for sentences :
def mask(string, all_but = 4, char = '#')
string.gsub(/\w(?=\w{#{all_but}})/, char)
end
p mask('It even works for multiple words')
# "It even #orks for ####iple #ords"
关于代码的一些说明
string.to_s
命名在编程中非常重要,尤其是在动态语言中.
Some notes about your code
string.to_s
Naming things is very important in programming, especially in dynamic languages.
string.to_s
如果 string
确实是一个字符串,则没有任何理由调用 to_s
.
If string
is indeed a string, there shouldn't be any reason to call to_s
.
如果 string
不是字符串,你确实应该在 gsub
之前调用 to_s
但也应该重命名 string
更好的描述:
If string
isn't a string, you should indeed call to_s
before gsub
but should also rename string
to a better description :
object.to_s
array.to_s
whatever.to_s
加入
puts array.join(", ").delete(", ").inspect
你到底想做什么?你可能只使用 join
:
What do you want to do exactly? You could probably just use join
:
[1,2,[3,4]].join(", ").delete(", ")
# "1234"
[1,2,[3,4]].join
# "1234"
删除
请注意,.delete(", ")
以任何顺序删除每个逗号和每个空格.它不仅删除 ", "
子字符串:
delete
Note that .delete(", ")
deletes every comma and every whitespace, in any order. It doesn't only delete ", "
substrings :
",a b,,, cc".delete(', ')
# "abcc"
["1,2", "3,4"].join(', ').delete(', ')
# "1234"
这篇关于如何屏蔽字符串中除最后四个字符之外的所有字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!