我需要编写一个函数来查找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
是的。我为我的答案添加了一个可选设计。 –