2011-11-13 78 views
0

我需要編寫一個函數來查找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 

回答

1

不,您的代碼會拋出異常,因爲您的循環會耗盡要處理的數據。您的循環需要以.Length -1結尾。否則,它應該工作。

但是,你可以使它更有效率和處理邊緣情形:

' Don't bother checking if the string is empty 
    If Not String.IsNullOrEmpty(asString) Then 
     ' If the string is only a single character, just return it 
     If asString.Length = 1 Then 
      Return asString 
     End If 

     ' Create a collection that records the number of occurences for each character 
     Dim cCharacterCounts As New System.Collections.Generic.Dictionary(Of Char, Integer) 

     For Each cCharacter As Char In asString 
      If cCharacterCounts.ContainsKey(cCharacter) Then 
       ' If the character exists, increment its count 
       cCharacterCounts(cCharacter) += 1 
      Else 
       ' Otherwise record the character as a new entry, initializing the count to 1 
       cCharacterCounts.Add(cCharacter, 1) 
      End If 
     Next 

     ' Now find the first character which only has a single count. This will be the first non-repeating value. 
     For Each cCharacter As Char In cCharacterCounts.Keys 
      If cCharacterCounts(cCharacter) > 1 Then 
       Return cCharacter.ToString() 
      End If 
     Next 
    End If 

    ' Handle the case in which there is no non-repeating character 
    Return String.Empty 
+0

好的,我更新了代碼。其他一切看起來不錯? – Bruno

+0

是的。我爲我的答案添加了一個可選設計。 –

1

LINQ的位可以使這個短了很多。

Imports System.Linq 

Module Module1 

    Sub Main() 
    Console.WriteLine(FirstCharacterToNotRepeat(Nothing)) 
    Console.WriteLine(FirstCharacterToNotRepeat("")) 
    Console.WriteLine(FirstCharacterToNotRepeat("BBEEXEE")) 
    Console.WriteLine(FirstCharacterToNotRepeat("BBEEEE")) 
    Console.WriteLine(FirstCharacterToNotRepeat("XBBEEEE")) 
    Console.WriteLine(FirstCharacterToNotRepeat("BBEEEEX")) 
    Console.WriteLine(FirstCharacterToNotRepeat("BBEEXEEACEED")) 
    Console.ReadLine() 
    End Sub 

    Private Function FirstCharacterToNotRepeat(ByVal input As String) As String 
    If String.IsNullOrEmpty(input) Then Return String.Empty 
    Return (input.GroupBy(Function(x) x).Where(Function(x) x.Count = 1).Select(Function(x) x.First))(0) 
    End Function 
End Module 
+0

+1好用的lambda! – Wade73

0
Dim str As String = "BBEEXEE" 
    For Each ch As Char In str 
     If str.Contains(ch & ch) = False Then 
      'first non-repeating 
      MsgBox("First non-repeating character is: " & ch) 
      Exit For 
     End If 
    Next 
相關問題