我想在两个字符串上执行按位,以便:
假设:
Dim word As String = "abcd"
Dim temp As String = "a-d-"
我想只返回'a'
假设:
Dim word As String = "abcd"
Dim temp As String = "a--d"
我想只返回'a - d'
我尝试过交叉,但它只在一个字符串中找到与另一个字符匹配的字符而不管位置如何。
我在这里使用' - '来表示空格。
任何建议都将不胜感激。
答案 0 :(得分:2)
这将处理长度不匹配的字符串:
Public Function CheckMask(ByVal word As String, ByVal mask As String) As String
Dim wordChars() As Char = word.ToCharArray()
Dim maskChars() As Char = mask.ToCharArray()
Dim i As Integer = 0
While i < wordChars.Length AndAlso i < maskChars.Length
If wordChars(i) <> maskChars(i) Then wordChars(i) = " "c
i = i + 1
End While
'If string lengths are equal or the mask is longer, we're done
'If the word is longer, need to set remaining characters to " "
While i < wordChars.Length
wordChars(i) = " "c
End While
Return New String(wordChars)
End Function
答案 1 :(得分:1)
Dim Res As String = ""
For i = 0 To Math.Min(StrA.Length, StrB.Length) - 1
If StrA(i) = StrB(i) Then Res &= StrA(i) Else Res &= " "
Next
Return Res
这基本上循环到两个字符串中较短的一个字符串的末尾。如果给定位置的字母匹配,则将字母添加到结果中,否则添加空格。
答案 2 :(得分:0)
Dim sFirstWord As String = "qwerty"
Dim sSecndWord As String = "qseftg"
Dim sResult As String = ""
For i As Integer = 0 To Math.Min(sFirstWord.Length, sSecndWord.Length) - 1
If sFirstWord(i) = sSecndWord(i) Then
sResult &= sFirstWord(i)
Else
sResult &= " "
End If
Next