Jump to content

_String_search


Zisly
 Share

Recommended Posts

Not sure if there already is a function like this..

Maybe this is something that could be added to the string UDF?

; #FUNCTION#;===============================================================================
;
; Name...........: _String_Search
; Description ...: Checks if a string contains a given substring
; Syntax.........: _String_Search($string,$substring)
; Parameters ....: $string = string to evaluate
;                 $substring = substring to search for
; Return values .: Success - Returns a array containing the position of the found substrings.
;                 Failure - Returns 0
;
; Author ........: Zisly
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........;
; Example .......; Yes
;
;;==========================================================================================
Func _String_Search($string, $substring)
    StringReplace($string, $substring, '')
    $ac = @extended
    If $ac = 0 Then Return 0
    Local $found[$ac]
    
    For $i = 1 To $ac
        $found[$i - 1] = StringInStr($string, $substring, 0, $i)
    Next
    
    Return $found
EndFunc ;==>_String_Search

Example:

#include <Array.au3>
#include <string.au3>

$array = _String_Search('a b a c a' , 'a')
_ArrayDisplay($array)
Edited by Zisly
Link to comment
Share on other sites

Link to comment
Share on other sites

@all

Did anyone ever run this example ?

Because it does not work correctly. Showing wrong output :D

@Zisly

You will have to do more efforts here. :D

regards

ptrex

Why not? Maybe you forgot to count the spaces

$array = _String_Search('abaca' , 'a')

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

@all

Did anyone ever run this example ?

Because it does not work correctly. Showing wrong output :D

@Zisly

You will have to do more efforts here. :D

regards

ptrex

It counts the spaces also so there isn't anything wrong with the example.

And what do you mean with "You will have to do more efforts here"?

Edited by Zisly
Link to comment
Share on other sites

Hi,

here ist somthing similar I did once.

;Umlaute
Global $test = 'this is a test ÜÖÄ ||| üöä Test ü, ö, ä, ÄÖÜ :-(!'
ConsoleWrite(_StringReplaceEx($test, 'Ü|Ö|Ä|ü|ö|ä|ß|(', 'Ue|Oe|Ae|ue|oe|ae|ss|)') & @CRLF)
ConsoleWrite(_StringReplaceEx($test, 'Ü|CH', 'Ue|CH!') & @CRLF)
ConsoleWrite(_StringReplaceEx($test, 'ch@|', '@', 2, 1, '@') & @CRLF)

;===============================================================================
; Function Name: _StringReplaceEx
; Description::    Replace multiple Strings like StringReplace
; Parameter(s):    Like StringReplace. $searchstring and $replacestring, $count, $case, $delim
; Requirement(s): ---
; Return Value(s): new string
;                    -1 = no | found
;                    -2 = string count do not match
;                    -3 = case should be 0,1,2
; Author(s): Thorsten Meger (Xenobiologist)
;===============================================================================
Func _StringReplaceEx($string, $searchstring, $replacestring, $count = 0, $case = 0, $delim = '|')
    If Not StringInStr($searchstring, $delim) Or Not StringInStr($replacestring, $delim) Then Return -1
    Local $searchstring_A = StringSplit($searchstring, $delim), $replacestring_A = StringSplit($replacestring, $delim)
    If UBound($searchstring_A) <> UBound($replacestring_A) Then Return -2
    If $case < 0 Or $case > 2 Then Return -3
    For $i = 1 To UBound($searchstring_A) - 1
        $string = StringReplace($string, String($searchstring_A[$i]), String($replacestring_A[$i]), $count, $case)
    Next
    Return $string
EndFunc   ;==>_StringReplaceEx

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

  • Moderators

Since everyone is throwing in their two cents. StringReplace() (especially insensitive match/replace) is much slower than StringInStr() itself. And sense you'll be using it anyway, might try something like this:

#include <array.au3>; Only for _ArrayDisplay()
Global $s_str = "Sally sells sea shells down by the sea shore"

; Case sensitive match
$a_array = _String_GetPosArray($s_str, "s", 1)
_ArrayDisplay($a_array, "Sensitive match")

; Case in-sensitive match
$a_array = _String_GetPosArray($s_str, "s")
_ArrayDisplay($a_array, "In-sensitive match")

Func _String_GetPosArray($s_str, $s_find, $v_case = 0)
    Local $i_c = 0, $i_pos = 0, $a_ret[100] = [100]
    
    While 1
        $i_pos = StringInStr($s_str, $s_find, $v_case, 1, $i_pos + 1)
        If $i_pos = 0 Then ExitLoop
        
        $i_c += 1
        If $i_c = $a_ret[0] Then
            ReDim $a_ret[$a_ret[0] + 100]
            $a_ret[0] = $a_ret[0] + 100
        EndIf
        
        $a_ret[$i_c] = $i_pos
    WEnd
    
    If $i_c = 0 Then Return SetError(1, 0, 0)
    
    ReDim $a_ret[$i_c + 1]
    $a_ret[0] = $i_c
    
    Return $a_ret
EndFunc

For the RegEx junky's

#include <array.au3>; Only for _ArrayDisplay()
Global $s_str = "Sally sells sea shells down by the sea shore"

; Case sensitive match
$a_array = _StringRegEx_GetPosArray($s_str, "s")
_ArrayDisplay($a_array, "Sensitive match")

; Case in-sensitive match
$a_array = _StringRegEx_GetPosArray($s_str, "(?i)s")
_ArrayDisplay($a_array, "In-sensitive match")

Func _StringRegEx_GetPosArray($s_str, $s_find)
    Local $i_pos = 0
    Local $a_sre = StringRegExp($s_str, $s_find, 3)
    If @error Then Return SetError(1, 0, 0)
    
    Local $i_ub = UBound($a_sre)
    Local $a_ret[$i_ub + 1] = [$i_ub]
    
    For $i = 0 To $i_ub - 1
        $i_pos = StringInStr($s_str, $a_sre[$i], 1, 1, $i_pos + 1)
        $a_ret[$i + 1] = $i_pos
    Next

    Return $a_ret
EndFunc
Edited by SmOke_N
Removed var that wasn't being used

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