Jump to content

Digit doesn't repeat in a string


SynNaz
 Share

Recommended Posts

  • Moderators

SynNaz,

One way to do what you want:

Global $ALists[2] = ["0123456789", "0123456799"]

For $k = 0 To 1

    ; Set a flag
    $fFlag = True

    ; For each character in the string (bar the last)
    For $i = 1 To StringLen($ALists[$k]) - 1

        ; Get the character
        $sCheck = StringMid($ALists[$k], $i, 1)

        ; And now check it against those remaining 
        For $j = $i + 1 To StringLen($ALists[$k])

            ; Announce if there is a match
            If $sCheck = StringMid($aLists[$k], $j, 1) Then

                MsgBox(0, "Duplicate", "The string " & $aLists[$k] & " contains a duplicate value of " & $sCheck)
                $fFlag = False ; And set the flag

            EndIf

        Next

    Next

    ; If no flag, announce no duplicates
    If $fFlag = True Then MsgBox(0, "Unique", "The string " & $aLists[$k] & " contains no duplicates")

Next

I hope that helps. :mellow:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Here is another

; call the function and return the result
$result = _StringFindDuplicate('1123456789')
; show the result
MsgBox(0, 'Result', $result)

Func _StringFindDuplicate($string)
    ; split into an array of single chars
    Local $array = StringSplit($string, '')
    ; do a replace of each char until @extended is larger then 1
    For $i = 1 To UBound($array) -1
        If StringReplace($string, $array[$i], '') And @extended > 1 Then
            MsgBox(0, 'Test', @extended & ' of ' & $array[$i] & ' found')
            Return False
        EndIf
    Next
    ; return true if no duplicates found
    Return True
EndFunc
Link to comment
Share on other sites

And another

$sTrue = 0123456789 ;if I have 0123456789 it returns true
$sFalse = 1123456789 ;if I have 1123456789 it returns false

MsgBox(0,$sTrue,_CharNotRepeat($sTrue))
MsgBox(0,$sFalse,_CharNotRepeat($sFalse))



Func _CharNotRepeat($sCheckString)
    Local $sDeleteChar, $bResult = True
    Do
    $deleteChar = StringLeft($sCheckString,1)
    $sCheckString = StringTrimLeft($sCheckString,1)
    If StringInStr($sCheckString,$deleteChar) Then
        $bResult = False
        ExitLoop
    EndIf
    Until  StringLen($sCheckString) = 0
    Return $bResult
EndFunc
GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF.
Link to comment
Share on other sites

RegExp-fu :mellow:

$sTrue = 0123456789 ;if I have 0123456789 it returns true
$sFalse = 1123456789 ;if I have 1123456789 it returns false
$sFalse2 = 1234516789 ;if I have 1123456789 it returns false


MsgBox(0,$sTrue,_NoDupDigit($sTrue))
MsgBox(0,$sFalse,_NoDupDigit($sFalse))
MsgBox(0,$sFalse2,_NoDupDigit($sFalse2))

Func _NoDupDigit($sDigits)
    ;notes: no error checking built in: this will also return true if you feed it a string of letters
    ;also only works when the digit string has no spaces or other non-digit characters
    ;
    ;if you want to limit the check for digits that only occur right beside each other,
    ;then use "(\d)(?=\1)" as the RegExp
    ;
    Return NOT StringRegExp($sDigits, "(\d)(?=\d*\1)")

EndFunc
Link to comment
Share on other sites

local $string[4]
local $checkstring
$string[1] = "1123456789"
$string[2] = "01234567899"
$string[3] = "0023456891"


for $i = 1 to 3
    for $x = 1 to stringlen($string[$i])
        $checkstring = stringleft($string[$i],1)
        if stringinstr($string[$i], $checkstring, 0, 1, 2, stringlen($string[$i])) > 0 Then
            msgbox(16,"Omfg!", $checkstring & " is duplicated ;(")
        EndIf
        $string[$i] = stringtrimleft($string[$i],1)
    Next
Next

ForNext is my middlename.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...