Results 1 to 4 of 4

Thread: [2008] How to check if all the chars in a string are different from each other?

  1. #1

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Question [2008] How to check if all the chars in a string are different from each other?

    Hi!!

    How can I check if all the chars in a string are different from each other? In other words how can I check if there are repeated chars in a string?

    Thks a lot!
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] How to check if all the chars in a string are different from each other?

    try this:

    vb Code:
    1. MsgBox("string contains duplicate characters = " & checkString("astringa"))

    vb Code:
    1. Private Function checkString(ByVal str As String) As Boolean
    2.        
    3.      For x As Integer = 0 To str.Length - 1
    4.           If x > 0 Then
    5.              If CStr(str.Substring(0, x) & str.Substring(x + 1, str.Length - (x + 1))).Contains(str.Substring(x, 1)) Then
    6.                 Return True
    7.              End If
    8.           Else
    9.              If str.Substring(x + 1, str.Length - (x + 1)).Contains(str.Substring(x, 1)) Then
    10.                  Return True
    11.              End If
    12.           End If
    13.      Next
    14.      Return False
    15.  
    16. End Function

  3. #3
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2008] How to check if all the chars in a string are different from each other?

    Well, the question is a little bit vague. You don’t specify the casing. If the method should be case sensitive, than here is the method:
    Code:
    'Case sensitive
    Private Function ContainsDuplicate(ByRef str As String) As Boolean
        For i As Integer = 0 To str.Length - 1
            If str.Substring(0, i).Contains(str.Substring(i, 1)) Then
                Return True
            End If
        Next
        Return False
    End Function
    If it is not spouse to be case sensitive, than add ToLower method after str and leave the rest the same, here is an example:
    Code:
    'None case sensitive
    Private Function ContainsDuplicate(ByRef str As String) As Boolean
        For i As Integer = 0 To str.Length - 1
            If str.ToLower.Substring(0, i).Contains(str.ToLower.Substring(i, 1)) Then
                Return True
            End If
        Next
        Return False
    End Function

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] How to check if all the chars in a string are different from each other?

    vb.net Code:
    1. For index As Integer = 0 To myString.Length - 1
    2.     If myString.LastIndexOf(myString(index)) <> index Then
    3.         'There is a least one more occurrence in the string of the character at the current index.
    4.     End If
    5. Next
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width