Jump to content

String Manipulation


anixon
 Share

Recommended Posts

How do you extract the position within a string of the first appearance of a numeric with any number between 0-9. I was thinking in terms of writing a loop which extracted the first character in the string and tested to see if it was a numeric then the second and so on until it encountered a numeric or is there a simple string function?

Some string examples and the position of the first numeric within the string:

'1acde ffga 12zmo r' the result would be 1

'abcdef 012 zuf9gas' the result would be 8

'ab7cdef 973 zufsgas' the result would be 3

'abcdefgh ijkl mnop 7' the result would be 20

Assistance is always appreciated Ant..

Link to comment
Share on other sites

How do you extract the position within a string of the first appearance of a numeric with any number between 0-9. I was thinking in terms of writing a loop which extracted the first character in the string and tested to see if it was a numeric then the second and so on until it encountered a numeric or is there a simple string function?

Some string examples and the position of the first numeric within the string:

'1acde ffga 12zmo r' the result would be 1

'abcdef 012 zuf9gas' the result would be 8

'ab7cdef 973 zufsgas' the result would be 3

'abcdefgh ijkl mnop 7' the result would be 20

Assistance is always appreciated Ant..

With the StringRegExp() function. or StringInStr()

An example with StringInStr() function :

$String = "fndjsk123"
For $i = 0 To 9
    $Position = StringInStr($String,$i)
    If $Find <> 0 Then
        MsgBox(0,"",$Position)
        Exit
    EndIf
Next
Edited by jerem488

Qui ose gagneWho Dares Win[left]CyberExploit[/left]

Link to comment
Share on other sites

With the StringRegExp() function. or StringInStr()

An example with StringInStr() function :

$String = "fndjsk123"
For $i = 0 To 9
    $Position = StringInStr($String,$i)
    If $Find <> 0 Then
        MsgBox(0,"",$Position)
        Exit
    EndIf
Next
Thanks for that given that the string can vari in length I have modified your example as follows

CODE
$String = "fndjskaaaa0123"

For $i = 0 To stringlen($string)

$Position = StringInStr($String,$i)

If $position <> 0 Then

MsgBox(0,"",$Position)

Exit

EndIf

Next

Link to comment
Share on other sites

No...

He goes from 0 to 9 because they are the only digits to check... StringInStr returns the position of the chosen string or 0. You DO NOT need to do it like you are now. Read his example again and try understand exactly whats happening.

Cheers,

Brett

EDIT:

But, the example might have flaws (I haven't tested it) as it might check for 0, yet 0 is the 2nd digit etc. Let me whip something different up :)

EDIT:

This works correctly for me :)

$string = "abcdefg1f0d93045"
MsgBox(0, "", _CheckStringFirstDigit($string))

Func _CheckStringFirstDigit($string)
    $split = StringSplit($string, "")
    For $i = 1 To $split[0]
        If StringRegExp($split[$i], "\d") Then ExitLoop
    Next
    Return $i
EndFunc  ;==>_CheckStringFirstDigit
Edited by BrettF
Link to comment
Share on other sites

If you do care about speed... you ought to drop the "<> 0" as demonstrated here:

$String = "fndjs4gffgdfb66"

$time = TimerInit()

For $z = 1 to 10000
    For $i = 0 To 9
        $Position = StringInStr($String,$i)
        If $Position <> 0 Then
;           MsgBox(0,"","Position: " & $Position)
            ExitLoop
        EndIf
    Next
Next

Msgbox(1,"", int(TimerDiff($time)))

$time = TimerInit()
For $z = 1 to 10000
    For $i = 0 To 9
        $Position = StringInStr($String,$i)
        If $Position Then
;           MsgBox(0,"","Position: " & $Position)
            ExitLoop
        EndIf
    Next
Next
Msgbox(1,"", int(TimerDiff($time)))
Link to comment
Share on other sites

Ouch.

$String = "0fndjs4gffgdfb66"

$time = TimerInit()

For $z = 1 to 10000
    For $i = 0 To 9
        $Position = StringInStr($String,$i)
        If $Position Then ExitLoop
    Next
Next

Msgbox(1,"", int(TimerDiff($time)))

$time = TimerInit()
For $z = 1 to 10000
    $split = StringSplit($string, "")
    For $i = 1 To $split[0]
        If StringRegExp($split[$i], "\d") Then ExitLoop
    Next
Next
Msgbox(1,"", int(TimerDiff($time)))
Link to comment
Share on other sites

:)

$String = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa7aaaaaaaa"
Dim $Dummy, $Position   

$time = TimerInit()

For $z = 1 to 1000
    For $i = 0 To 9
        $Position = StringInStr($String,$i)
        If $Position Then ExitLoop
    Next
Next

Msgbox(0x40,'StringInStr', int(TimerDiff($time)))

$time = TimerInit()
For $z = 1 to 1000
    $Dummy = StringRegExp($String, '\d', 1)
    $Position = @extended-1
Next

Don't add unneeded code lol, now ouch!... XP

Link to comment
Share on other sites

Ouch.

Yes it may be faster but it returns the wrong result:

See the following:

$String = "fndjs4g0ffgdfb66"

$time = TimerInit()

For $z = 1 to 10000
    For $i = 0 To 9
        $Position = StringInStr($String,$i)
        If $Position Then ExitLoop
    Next
Next

Msgbox(0,$Position, int(TimerDiff($time)))

$time = TimerInit()
For $z = 1 to 10000
    $split = StringSplit($string, "")
    For $i = 1 To $split[0]
        If StringRegExp($split[$i], "\d") Then ExitLoop
    Next
Next
Msgbox(0,$i, int(TimerDiff($time)))

Just go with what Auth posted. It confused me because there is no mention of @extended return the position...

Cheers,

Brett

Link to comment
Share on other sites

how about

$String = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa7aaaaaaaa"

Dim $Dummy, $Position   

$time = TimerInit()

$ss=StringSplit($String,"")
    For $i = 1 To $ss[0]
    if StringIsDigit($ss[$i])=1 then ExitLoop
Next
Msgbox(0,$i,"Done in "& int(TimerDiff($time))&" miliseconds and it found the number "&$ss[$i])
Edited by Aceguy
Link to comment
Share on other sites

$String = "1aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa7aaaaaaaa"

Dim $Dummy, $Position   

$time = TimerInit()

For $z = 0 To 500
    $ss=StringSplit($String,"")
        For $i = 1 To $ss[0]
        if StringIsDigit($ss[$i]) then ExitLoop
    Next
Next

Msgbox(0,$i,"Done in "& int(TimerDiff($time))&" miliseconds and it found the number "&$ss[$i])


$time = TimerInit()

For $z = 0 To 500
    $Dummy = StringRegExp($String, '\d')
    $Position = $Dummy
Next

Msgbox(0,$i,"Done in "& int(TimerDiff($time))&" miliseconds and it found the number "&$ss[$i])

I added 1 in front of the string, still, the results are crystal clear. (=P

...@extended-1 instead of $Dummy.

Edited by Authenticity
Link to comment
Share on other sites

Hi guys thanks for the spirited response I really appreciated the effort from those concerned. On reading the installments it just confirms what I have anyways thought and that is there is more than one way of coding a solution. Based on those presented I have coded my requirement as follows.

;//Extract the Phone Number

$sString = "Most Urgent 61390890965"

StringRegExp($sString, '\d', 1) ;\\finds position of first numeric occurance

$sPosition = @extended - 1 ;calculate the @mid start position

$sPhoneNum = StringMid($sString, $sPosition, StringLen($sString) - ($sPosition - 1))

Msgbox(0,"",$sPhoneNum,5)

Thanks again Ant..

Link to comment
Share on other sites

  • Moderators

Hi guys thanks for the spirited response I really appreciated the effort from those concerned. On reading the installments it just confirms what I have anyways thought and that is there is more than one way of coding a solution. Based on those presented I have coded my requirement as follows.

;//Extract the Phone Number

$sString = "Most Urgent 61390890965"

StringRegExp($sString, '\d', 1) ;\\finds position of first numeric occurance

$sPosition = @extended - 1 ;calculate the @mid start position

$sPhoneNum = StringMid($sString, $sPosition, StringLen($sString) - ($sPosition - 1))

Msgbox(0,"",$sPhoneNum,5)

Thanks again Ant..

Hmm...
Local $a_phone = StringRegExp($s_string, "\d+", 1)
If Not @error Then MsgBox(64, "Number", $a_phone[0])

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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...