我的目的是接受一段文本并找到我要 REDACT 或替换的指定短语。
我创建了一个接受参数作为文本字符串的方法。我将该字符串分解为单个字符。比较这些字符,如果它们匹配,我将这些字符替换为*
.
def search_redact(text)
str = ""
print "What is the word you would like to redact?"
redacted_name = gets.chomp
puts "Desired word to be REDACTED #{redacted_name}! "
#splits name to be redacted, and the text argument into char arrays
redact = redacted_name.split("")
words = text.split("")
#takes char arrays, two loops, compares each character, if they match it
#subs that character out for an asterisks
redact.each do |x|
if words.each do |y|
x == y
y.gsub!(x, '*') # sub redact char with astericks if matches words text
end # end loop for words y
end # end if statment
end # end loop for redact x
# this adds char array to a string so more readable
words.each do |z|
str += z
end
# prints it out so we can see, and returns it to method
print str
return str
end
# calling method with test case
search_redact("thisisapassword")
#current issues stands, needs to erase only if those STRING of characters are
# together and not just anywehre in the document
如果我输入一个与文本的其他部分共享字符的短语,例如,如果我调用:
search_redact("thisisapassword")
那么它也将替换该文本。当它接受用户的输入时,我只想摆脱文本密码。但它看起来像这样:
thi*i**********
请帮忙。