我需要編寫一個函數來查找VB.NET中字符串中的第一個非重複字符。下面的代碼看起來好嗎?在VB.NET中查找字符串中的第一個非重複字符
Module Module2
Sub Main()
' Unit test
' Pass string as argument.
Console.WriteLine(nonRepeat("BBEEXEE")
End Sub
Function nonRepeat(ByVal aString As String) As String
Dim repeated As Integer = 0
For i = 0 To aString.Length-1
repeated = 0
For j = 0 To aString.Length-1
' If inner and outer For loops are on the same index then
' inner For loop moves to next index and compares character
' with outer For loop character.
' If characters are equal then set repeated = 1 and Exit inner For loop.
' Otherwise, continue to find repeating character
' If reached end of string without finding repeating character
' then non-repeating character has been found and is returned.
If ((i <> j) AndAlso (aString(i) = aString(j))) Then
' Found repeating character
repeated = 1
Exit For
End If
Next
If (repeated = 0) Then
' Found first non-repeating character
Return aString(i)
End If
Next
Return ("No Non-Reapeating character!")
End Function
End Module
好的,我更新了代碼。其他一切看起來不錯? – Bruno
是的。我爲我的答案添加了一個可選設計。 –