nobbe Posted February 8, 2008 Posted February 8, 2008 a little helper for strings which i use often expandcollapse popup#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
GtaSpider Posted February 8, 2008 Posted February 8, 2008 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 Forum
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now