Sign in to follow this  
Followers 0
Klexen

If string contains a letter.

5 posts in this topic

Is there a way to take a string contained in a variable, check to see if it starts with a number. If it doesn't start with a number set the string to something else?

Here is some of my code...

$readtext = _IEBodyReadText($oIE)
    
;Black
    $BlackPercent = StringMid($readtext,470,4)
    $BlackPages = StringMid($readtext,665,4)

;Cyan
    $CyanPercent = StringMid($readtext, 770,4)
    $CyanPages = StringMid($readtext, 963,4)
    
;Magenta
    $MagentaPercent = StringMid($readtext,1070,4)
    $MagentaPages = StringMid($readtext,1263,4)
    
;Yellow 
    $YellowPercent = StringMid($readtext,1370,4)
    $YellowPages = StringMid($readtext,1563,4)

Basically what this does is opens the page that displays printer ink statistics, and reads the stats, then I can do with the values what I want. However, if the ink is out, it does not display the stats, so of course the string it grabs is all messed up. So if the string does not start with a number, I want to replace string contents with the word "out"

If anyone can help me with this I would greatly appreciate it.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

$String = StringRegExp(StringLeft($YOUR_STRING_HERE, 1), '[0-9]', 1);replace $YOUR_STRING_HERE with the variable that contains your string contents
If @error == 1 Then
    $YOUR_STRING_HERE = 'out';replace $YOUR_STRING_HERE with the variable that contains your string contents
EndIf

Edited by oMBra

Share this post


Link to post
Share on other sites

$String = StringRegExp(StringLeft($YOUR_STRING_HERE, 1), '[0-9]', 1);replace $YOUR_STRING_HERE with the variable that contains your string contents
If @error == 1 Then
    $YOUR_STRING_HERE = 'out';replace $YOUR_STRING_HERE with the variable that contains your string contents
EndIf
Worked GREAT. Thanks a lot man!

Share this post


Link to post
Share on other sites

No problem man

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

You had what you needed already, anyways this also works.

Edit: javascript error

$sString = 'Some String'
If Not StringRegExp($sString, '\A\d') Then ; string does not start with an integer
    $sString = '1. Another String'
EndIf

ConsoleWrite($sString & @CRLF)
Edited by Robjong

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