Sign in to follow this  
Followers 0
nobbe

_string_before_after

2 posts in this topic

a little helper for strings which i use often

#include <String.au3>

;
; test for string

$string = "1;2;3;4;5;6";

$ret = _string_before_after($string, ";", 0)

MsgBox(64, "first occurrance", $ret)

$ret = _string_before_after($string, ";", 1)
MsgBox(64, "last  occurrance", $ret)


$ret = _string_before_after($string, "-", 0)

MsgBox(64, "no occurrance", $ret)

Exit;


;
; mode 0 == want first part (1);2;3
; mode 1 == want last part  1;2;(3)
;
Func _string_before_after($input, $separator, $mode)

    Local $tmp = StringSplit($input, $separator)
    
    ;; found
    If @error <> 1 Then
        
        If $mode == 0 Then
            Return $tmp[1]; first part
        Else
            Local $nbr = $tmp[0];
            Return $tmp[$nbr]; right part
        EndIf
        
        ;; not found
    Else
        Return -1 ; no occurance
    EndIf
    

EndFunc   ;==>_string_before_after

Share this post


Link to post
Share on other sites



Hi,

Nice function, but it works also a little bit smaller:

#include <String.au3>

;
; test for string

$string = "1;2;3;4;5;6";
$ret = _StringAftBef($string,";",0)
MsgBox(64, "first occurrance", $ret)

$ret = _StringAftBef($string,";",1)
MsgBox(64, "first occurrance", $ret)


$ret = _StringAftBef($string,"-",1)
MsgBox(64, "first occurrance", $ret)

Exit;
Func _StringAftBef($sString,$sSeparator,$iMode)
    If Not StringInStr($sString,$sSeparator) Then Return -1
    If $iMode Then
        Return StringtrimLeft($sString,StringInStr($sString,$sSeparator,0,-1))
    Else
        Return StringLeft($sString,StringInStr($sString,$sSeparator)-1)
    EndIf
EndFunc

Spider


www.AutoIt.de - Moderator of the German AutoIt Froum

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