心中已經到處搜尋,但沒有找到答案,如果我錯了,請告訴我去的帖子,否則我的問題是:全部替換字符串中的字符用「*」字符
我想將一個字符串(例如「password」)替換爲所有'*',但它仍然有字符串的字符數,例如「password」有8個字符,所以它會像這樣代替它「*** *****「而不是密碼或任何其他字符串(例如」word「>」****「,」string「>」******「)。
如果任何人都可以幫助我在這裏,這將是偉大的,謝謝!
心中已經到處搜尋,但沒有找到答案,如果我錯了,請告訴我去的帖子,否則我的問題是:全部替換字符串中的字符用「*」字符
我想將一個字符串(例如「password」)替換爲所有'*',但它仍然有字符串的字符數,例如「password」有8個字符,所以它會像這樣代替它「*** *****「而不是密碼或任何其他字符串(例如」word「>」****「,」string「>」******「)。
如果任何人都可以幫助我在這裏,這將是偉大的,謝謝!
你可以做這樣的事情:
Function HideString(str As String) As String
Return New String("*"c, str.Length)
End Function
這個函數創建使用「*」字符一個新的字符串,但與相同長度的輸入字符串。
您可以使用它像這樣:
Dim test As String = HideString("password")
謝謝你工作得很漂亮,正是我在找的東西! – Melternet
Nit Picking:'返回新的字符串(「*」c,Trim(str).Length)',我建議你使用'TRIM()'來避免尾隨空格。 –
使用regex.replace()方法:
RegularExpressions.Regex.Replace("password", "[a-zA-Z]", "*")
您可以創建一個功能類似下面
Private Function strReplace(ByVal str As String, ByVal char2replace As String)
Dim lstr As String = Trim(str)
str = RegularExpressions.Regex.Replace(lstr, "[a-zA-Z]", char2replace)
'or you can use
'str = Regex.Replace(lstr, "[a-zA-Z]", char2replace)
'but in this case you need to import System.Text.RegularExpressions
Return lstr
End Function
用法:
Dim str As String = " PassWORD "
str = strReplace(str, "*")
'Output : ********
Dim str As String = "password"
str = strReplace(str, "#")
'Output : ########
你的意思是'在輸入時顯示爲星星'?或者將一些現有文本轉換爲「隱藏」已知密碼?或者用給定數量的星星創建一個新的字符串,給定任意長度的字符串? – user2864740
已經在下面回答,謝謝:) – Melternet