Sign in to follow this  
Followers 0
Bert

spaces, and trying to remove them

5 posts in this topic

#1 ·  Posted (edited)

Any idea why this doesn't work? I need to fix it so if the text typed in has spaces at the end of it, I can strip them off the string. The idea is to end up with the name as like the following example:

John Smith

If they put too many spaces in the first name, it may show us as

John . Smith (I put a . in there for the forum won't show multi spaces between words.)

I tried to do a search for the space at the end of the name, but the loop gets stuck. You will see where I commented where the problem is

:)

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

DIM  $string1,  $inifile = @ScriptDir&"/temp/usage.dat"
_getnamelist()
_Get_Name_At_Start()

Func _Get_Name_At_Start()
    $InputGUI          = GUICreate("Enter you name or select your name from the list", 410, 205, 193, 115)
    ;$InputGUI_Icon     = GUISetIcon("I:\autoit scrips\icons\97.ico")
    $InputGUI_Label_0  = GUICtrlCreateLabel("Select your name if listed", 16, 4, 252, 17)
    $InputGUI_list_0   = GUICtrlCreatecombo("", 16, 20, 353, 24)
                         GUICtrlSetData($InputGUI_list_0, $string1)
    $InputGUI_Label_1  = GUICtrlCreateLabel("Screen name if available", 16, 48, 352, 17)
    $InputGUI_Input_1  = GUICtrlCreateInput("", 16, 64, 353, 21)             
    $InputGUI_Label_2  = GUICtrlCreateLabel("First name", 16, 88, 52, 17)
    $InputGUI_Input_2  = GUICtrlCreateInput("", 16, 104, 353, 21)
    $InputGUI_Label_3  = GUICtrlCreateLabel("Last Name", 16, 132, 55, 17)
    $InputGUI_Input_3  = GUICtrlCreateInput("", 16, 148, 353, 21)
    $InputGUI_Button_1 = GUICtrlCreateButton("Ok", 16, 177, 89, 25, 0)
    $InputGUI_Button_2 = GUICtrlCreateButton("Cancel", 152, 177, 89, 25, 0)
    $InputGUI_Button_3 = GUICtrlCreateButton("Exit", 296, 177, 73, 25, 0)
    GUISetState(@SW_SHOW, $InputGUI)

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $InputGUI_Button_1
                $InputGUI_ControlREAD_1 = GUICtrlRead($InputGUI_Input_2)
                $InputGUI_ControlREAD_2 = GUICtrlRead($InputGUI_Input_3)
                $StringLenFirstName     = StringLen  ($InputGUI_ControlREAD_1)
                $StringLenLastName      = StringLen  ($InputGUI_ControlREAD_2)
                $picklist_select        = GUICtrlRead($InputGUI_list_0)
                $ScreenName_select      = GUICtrlRead($InputGUI_Input_1)
                
;problem is here--------------------------------------------------------------------    
;I need the extra spaces to be removed from the end of the name if the user does it by accident.
                do 
                    $spaces_firstNameCheck = StringRight($InputGUI_ControlREAD_1, 1)
                    if $spaces_firstNameCheck = " " then 
                       $space_trim = StringTrimRight($InputGUI_ControlREAD_1, 1)
                   endif  
                until $spaces_firstNameCheck <> " " 
                do 
                    $spaces_LastNameCheck = StringRight($InputGUI_ControlREAD_1, 1)
                    if $spaces_LastNameCheck = " " then 
                       $space_trim = StringTrimRight($InputGUI_ControlREAD_1, 1)
                    endif   
                until $spaces_LastNameCheck <> " "  
;-------------------------------------------------------------------------------------------------              
                $StringLenFirstName3     = StringLen  ($InputGUI_ControlREAD_1)  
                if $StringLenFirstName > 1 and $StringLenLastName > 1 and $picklist_select = "" then
;~                  MsgBox(0, "", $InputGUI_ControlREAD_1&" "&$InputGUI_ControlREAD_2)
;~                  exit
                    ;WinSetTitle($Form1, "", "Name of student: "& $InputGUI_ControlREAD_1&" "&$InputGUI_ControlREAD_2)
                    $var = IniReadSectionNames( $inifile)
                    _getnamelist()
                    $arraysearch = StringInStr ($string1, $InputGUI_ControlREAD_1&" "&$InputGUI_ControlREAD_2)
                    if $arraysearch > 0 then 
                        MsgBox(0, "Error", "Name already entered")
                    else    
                        IniWrite($inifile, $InputGUI_ControlREAD_1&" "&$InputGUI_ControlREAD_2, '1', $InputGUI_Input_1)
                        GUIDelete  ($InputGUI)
                        exitloop
                    endif
                endif
                if $StringLenFirstName = 0 and $StringLenLastName =0 and $picklist_select <> "" then
                    MsgBox(0, "",$picklist_select)
                    Exit
                    ExitLoop
                EndIf
                if $StringLenFirstName > 0 and $StringLenLastName > 0 and $picklist_select <> "" then
                    MsgBox(0, "", "Error. You need to either select a name or enter in your name")
                EndIf   
            Case $InputGUI_Button_2
                $InputGUI_Question      = MsgBox     (36, "Exit?", "Do you wish to exit?")
                if $InputGUI_Question   = 6 then 
                    GUIDelete  ($InputGUI)  
                    _exit()
                endif   
            Case $InputGUI_Button_3
                    GUIDelete  ($InputGUI)
                    _exit()
        EndSwitch
    WEnd
EndFunc

Func _getnamelist()
    $var = IniReadSectionNames( $inifile)
    If @error Then 
    ConsoleWrite( "Error occurred, probably no INI file.")
    Else
       $string0 = _ArrayToString($var)
       $pipecheck = stringinstr($string0, "|")
       $string1 = StringTrimLeft($string0, $pipecheck)
   EndIf
   Return $string1
EndFunc

Func _exit()
    Exit
EndFunc
Edited by Volly

Share this post


Link to post
Share on other sites



How about getting rid of these:

do 
    $spaces_firstNameCheck = StringRight($InputGUI_ControlREAD_1, 1)
    if $spaces_firstNameCheck = " " then 
       $space_trim = StringTrimRight($InputGUI_ControlREAD_1, 1)
   endif  
until $spaces_firstNameCheck <> " " 
do 
    $spaces_LastNameCheck = StringRight($InputGUI_ControlREAD_1, 1)
    if $spaces_LastNameCheck = " " then 
       $space_trim = StringTrimRight($InputGUI_ControlREAD_1, 1)
    endif   
until $spaces_LastNameCheck <> " "

and changing instead:

$InputGUI_ControlREAD_1 = StringStripWS(GUICtrlRead($InputGUI_Input_2), 2)
$InputGUI_ControlREAD_2 = StringStripWS(GUICtrlRead($InputGUI_Input_3), 2)
$ScreenName_select    = StringStripWS(GUICtrlRead($InputGUI_Input_1), 2)

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I took a look, I'm not that "great"

but wouldn't StringStripWS be equivalent to checking for & removing " " ?

/e

@enaiman, right before me :)

Edited by igotandrew

Share this post


Link to post
Share on other sites

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <String.au3> ; added by Val

Dim $string1, $inifile = @ScriptDir & "/temp/usage.dat"
_getnamelist()
_Get_Name_At_Start()

Func _Get_Name_At_Start()
    
    $InputGUI = GUICreate("Enter you name or select your name from the list", 410, 205, 193, 115)
    ;$InputGUI_Icon     = GUISetIcon("I:\autoit scrips\icons\97.ico")
    $InputGUI_Label_0 = GUICtrlCreateLabel("Select your name if listed", 16, 4, 252, 17)
    $InputGUI_list_0 = GUICtrlCreateCombo("", 16, 20, 353, 24)
    GUICtrlSetData($InputGUI_list_0, $string1)
    $InputGUI_Label_1 = GUICtrlCreateLabel("Screen name if available", 16, 48, 352, 17)
    $InputGUI_Input_1 = GUICtrlCreateInput("", 16, 64, 353, 21)
    $InputGUI_Label_2 = GUICtrlCreateLabel("First name", 16, 88, 52, 17)
    $InputGUI_Input_2 = GUICtrlCreateInput("", 16, 104, 353, 21)
    $InputGUI_Label_3 = GUICtrlCreateLabel("Last Name", 16, 132, 55, 17)
    $InputGUI_Input_3 = GUICtrlCreateInput("", 16, 148, 353, 21)
    $InputGUI_Button_1 = GUICtrlCreateButton("Ok", 16, 177, 89, 25, 0)
    $InputGUI_Button_2 = GUICtrlCreateButton("Cancel", 152, 177, 89, 25, 0)
    $InputGUI_Button_3 = GUICtrlCreateButton("Exit", 296, 177, 73, 25, 0)
    GUISetState(@SW_SHOW, $InputGUI)

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $InputGUI_Button_3, $GUI_EVENT_CLOSE
                Exit
            Case $InputGUI_Button_1
                $InputGUI_ControlREAD_1 = GUICtrlRead($InputGUI_Input_2)
                $InputGUI_ControlREAD_2 = GUICtrlRead($InputGUI_Input_3)
                $StringLenFirstName = StringLen($InputGUI_ControlREAD_1)
                $StringLenLastName = StringLen($InputGUI_ControlREAD_2)
                $picklist_select = GUICtrlRead($InputGUI_list_0)
                $ScreenName_select = GUICtrlRead($InputGUI_Input_1)

                ;problem is here--------------------------------------------------------------------
                ;I need the extra spaces to be removed from the end of the name if the user does it by accident.
;~                 do
;~                     $spaces_firstNameCheck = StringRight($InputGUI_ControlREAD_1, 1)
;~                     if $spaces_firstNameCheck = " " then
;~                        $space_trim = StringTrimRight($InputGUI_ControlREAD_1, 1)
;~                    endif
;~                 until $spaces_firstNameCheck <> " "
;~                 do
;~                     $spaces_LastNameCheck = StringRight($InputGUI_ControlREAD_1, 1)
;~                     if $spaces_LastNameCheck = " " then
;~                        $space_trim = StringTrimRight($InputGUI_ControlREAD_1, 1)
;~                     endif
;~                 until $spaces_LastNameCheck <> " "
                ;-------------------------------------------------------------------------------------------------
                $StringLenFirstName3 = StringLen($InputGUI_ControlREAD_1)
                If $StringLenFirstName > 1 And $StringLenLastName > 1 And $picklist_select = "" Then
                    
                    
                    ;************* ADDED BY VALUATER FROM HERE ********************
                    $InputGUI_ControlREAD_1 = _Cleanup($InputGUI_ControlREAD_1, True)
                    $InputGUI_ControlREAD_2 = _Cleanup($InputGUI_ControlREAD_2)
                    MsgBox(0x0, "name", "*" & $InputGUI_ControlREAD_1 & $InputGUI_ControlREAD_2 & "*") ; * used to show spaces
                    Exit
                    ;****************** END ADDED BY VALUATER ****************
                    

                    ;WinSetTitle($Form1, "", "Name of student: "& $InputGUI_ControlREAD_1&" "&$InputGUI_ControlREAD_2)
                    $var = IniReadSectionNames($inifile)
                    _getnamelist()
                    $arraysearch = StringInStr($string1, $InputGUI_ControlREAD_1 & " " & $InputGUI_ControlREAD_2)
                    If $arraysearch > 0 Then
                        MsgBox(0, "Error", "Name already entered")
                    Else
                        IniWrite($inifile, $InputGUI_ControlREAD_1 & " " & $InputGUI_ControlREAD_2, '1', $InputGUI_Input_1)
                        GUIDelete($InputGUI)
                        ExitLoop
                    EndIf
                EndIf
                If $StringLenFirstName = 0 And $StringLenLastName = 0 And $picklist_select <> "" Then
                    MsgBox(0, "", $picklist_select)
                    Exit
                    ExitLoop
                EndIf
                If $StringLenFirstName > 0 And $StringLenLastName > 0 And $picklist_select <> "" Then
                    MsgBox(0, "", "Error. You need to either select a name or enter in your name")
                EndIf
            Case $InputGUI_Button_2
                $InputGUI_Question = MsgBox(36, "Exit?", "Do you wish to exit?")
                If $InputGUI_Question = 6 Then
                    GUIDelete($InputGUI)
                    _exit()
                EndIf
            Case $InputGUI_Button_3
                GUIDelete($InputGUI)
                _exit()
        EndSwitch
    WEnd
EndFunc   ;==>_Get_Name_At_Start

; ADDED BY VALUATER
Func _Cleanup($iName, $Space = False)
    $iName = StringReplace($iName, " ", "")
    If $Space Then $iName &= " "
    Return _StringProper($iName)
EndFunc   ;==>_Cleanup



Func _getnamelist()
    $var = IniReadSectionNames($inifile)
    If @error Then
        ConsoleWrite("Error occurred, probably no INI file.")
    Else
        $string0 = _ArrayToString($var)
        $pipecheck = StringInStr($string0, "|")
        $string1 = StringTrimLeft($string0, $pipecheck)
    EndIf
    Return $string1
EndFunc   ;==>_getnamelist

Func _exit()
    Exit
EndFunc   ;==>_exit

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

Didn't know if you saw this because of a working idea from enaiman

8)


NEWHeader1.png

Share this post


Link to post
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
Sign in to follow this  
Followers 0