Jump to content

Search in a string / position


Go to solution Solved by mrflibblehat,

Recommended Posts

Posted

Hello,

I have string like this:

Auto|it|is|funny|sometime

I want to know if ex. "funny" is in the string and return the position ( in the example the number 4 )

How to do that? Thanks

Posted

Function StringInStr?

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted (edited)

I have some problem, If i have 2 string named:

Auto

Autoit

And i want to search for "Autoit", give me the wrong result ( 1 instead of 2 )

But in general give me wrong result, i have search for the 6 element and give me 55 :D

Edited by MyEarth
Posted

Ok, I think you need something like this:

$sString = "Auto|it|is|funny|sometime"
$sSearch = "funny"
$aWords = StringSplit($sString, "|")
For $i = 1 To $aWords[0]
    If $aWords[$i] = $sSearch Then
        MsgBox(0, "Result", "Searchword '" & $sSearch & "' found at position " & $i)
        ExitLoop
    EndIf
Next

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

Why don't you just search for |funny| ?

Not totally related, but might get you thinking.

#include <Constants.au3>

Example()

Func Example()
    ; String to search for the word "auto".
    Local $sString = 'This is an automatic AutoIt string with auto text.'

    ; Case sensitive (use (?i:\bauto\b) for non-case sensitive. Replace the word auto with a unqiue search
    ; identifier.
    Local $sStringRename = StringRegExpReplace($sString, '(?:\bauto\b)', '_SEARCHSTRING1234_')

    ; Now use StringInStr to find the unique identifier.
    MsgBox($MB_SYSTEMMODAL, '', StringInStr($sStringRename, '_SEARCHSTRING1234_'))

    ; Traditional approach will find auto in automatic.
    MsgBox($MB_SYSTEMMODAL, '', StringInStr($sString, 'auto'))
EndFunc   ;==>Example

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

The OP doesn't want to get the Character but the Word position.

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

Still not what the OP wants.

$sString = "Auto|it|is|funny|sometime"
$sSearch = "|funny|"
ConsoleWrite(StringInStr($sString, $sSearch)+1 & @LF)

returns 12.

But the OP wants 4.

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

And to make it find the first or last word you need to add the delimiter to both ends.

$sString = "Auto|it|is|funny|sometime"
$sSearch = "sometime"
ConsoleWrite(StringInStr("|" & $sString & "|", "|" & $sSearch & "|") & @LF)

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted
  On 5/31/2013 at 10:24 AM, water said:

The OP doesn't want to get the Character but the Word position.

No I see that when I re-read the problem again. Sorry.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

First reading, then posting o:);)

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

Sometimes the problem isn't what was originally described.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

I just re-read the original post. The user seems to be quite clear what he wants.

I've seen a lot of posts where the user lets us guess what he wants. This one even gives us example data and an expected result.

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted (edited)

Besides the obvious StringSplit solution, here's an alternative method.

Local $sString = "Auto|it|is|funny|sometime"
Local $pattern = "funny"
Local $iposition = StringInStr("|" & $sString & "|", "|" & $pattern & "|")

Local $sStringLeft, $iOccurence = 0
If $iposition Then
    $iOccurence += 1
    $sStringLeft = StringLeft("|" & $sString, $iposition -1)
    While StringInStr($sStringLeft, "|", 0, $iOccurence)
        $iOccurence += 1
    WEnd
EndIf

MsgBox(0, "", $iOccurence)

It's similar to what FireFox posted.

Edited by czardas
Posted

As always there are many ways to skin a cat.

I'm curious which of the solutions is preferred by the OP.

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted
  On 5/31/2013 at 11:36 AM, Malkey said:

 

Another method.

Local $vString = "Auto|it|is|funny|sometime" ; or "Auto it is funny sometime" ;
Local $sFind = "funny"
Local $iNum = UBound(StringRegExp($sFind, "(" & StringRegExpReplace($vString, "(\W+)", ")|(") & ")", 3))

MsgBox(0,"Results",'"' & $sFind & '" is word number #' & $iNum & ' in "' & $vString & '"' & @LF)

; Returns:-
; "funny" is word number #4 in "Auto|it|is|funny|sometime"

Nice example Malkey.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...