SynNaz Posted February 21, 2010 Posted February 21, 2010 How do I check if a digit doesn't repeat in a string. For example: if I have 0123456789 it returns true if I have 1123456789 it returns false
Moderators Melba23 Posted February 21, 2010 Moderators Posted February 21, 2010 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. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
MHz Posted February 21, 2010 Posted February 21, 2010 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
Yoriz Posted February 21, 2010 Posted February 21, 2010 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.
ResNullius Posted February 22, 2010 Posted February 22, 2010 RegExp-fu $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
notsure Posted February 22, 2010 Posted February 22, 2010 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now