Jump to content
Sign in to follow this  
bafh

Check string for specific characters!

Recommended Posts

bafh

Hello,

I have a string and want to check if it only contains lowercase characters from (ascII) 47-58 and numbers.

How would I do that, as short as possible because i dont want to do things like

If $var[1] = a then exit

If $var[1] = b then exit and so on

thanks for answering

Share this post


Link to post
Share on other sites
GaryFrost

You mean something like this?

$string = 'abcdefghijklmnopqrstuvwxyz0123456789'
$test = 'This is a Test'

    For $x = 1 To StringLen($string)
        If(StringInStr($test,stringmid($string,$x,1),1)) Then
            ConsoleWrite("Found: " & stringmid($string,$x,1) & @LF)
        EndIf
    Next

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites
bafh

Hmm,

no, what i want is to check a string for its characters and only allow characters from a-z and numbers from 0-9, and if the string contains any other characters to make an error.

Share this post


Link to post
Share on other sites
bafh

Yes,

but there could also be letters,so, if I ceck it for alphanumeric it checks for letters from ascII code 0-125 or something, but i only want 0-9 and a-z :(

Share this post


Link to post
Share on other sites
GaryFrost

also alows for space

$string = 'abcdefghijklmnopqrstuvwxyz0123456789 '
$test = 'This is a Test 9'
$string_ok = 1
    For $x = 1 To StringLen($test)
        If(StringInStr($string,stringmid($test,$x,1),1) == 0) Then
            $string_ok = 0
        EndIf
    Next
ConsoleWrite($string_ok & @LF)
$test = 'this is a test 9'
$string_ok = 1
    For $x = 1 To StringLen($test)
        If(StringInStr($string,stringmid($test,$x,1),1) == 0) Then
            $string_ok = 0
        EndIf
    Next
ConsoleWrite($string_ok & @LF)

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites
bafh

That works?

i cant test it right now. what if string is "234hsjdfh=)(/"·$" ?

Share this post


Link to post
Share on other sites
GaryFrost

That works?

i cant test it right now. what if string is "234hsjdfh=)(/"·$" ?

<{POST_SNAPBACK}>

Example:

$string = 'abcdefghijklmnopqrstuvwxyz0123456789 '
$test = '"234hsjdfh=)(/"·$" ?'
$string_ok = 1
    For $x = 1 To StringLen($test)
        If(StringInStr($string,stringmid($test,$x,1),1) == 0) Then
            $string_ok = 0
            ExitLoop
        EndIf
    Next
If (Not $string_ok) Then
    ConsoleWrite("String has characters not allowed" & @LF)
EndIf

output

>Running: (3.1.1.0):C:\Program Files\AutoIt3\autoit3.exe "C:\Documents and Settings\Gary\My Documents\test5.au3" 

String has characters not allowed

>AutoIT3.exe ended.

>Exit code: 0    Time: 1.072

Edit: added ExitLoop Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites
kjactive

As I see it there is only a few string manipulation functions build into Autoit and that's StringInStr, StringReplace and StringMid well one could count stringleft/right, Lower/upper and StringLen in but that's all and you has to do with that no matter...

kjactive :(

Edited by kjactive

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.