Snippets ( AutoIt String ): Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
No edit summary
 
(Added snippets _StringGetDigitLeft and _StringGetDigitRight.)
 
(6 intermediate revisions by 4 users not shown)
Line 14: Line 14:
<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
#include <Array.au3>
#include <Array.au3>
Global $aArray = _StringEqualSplit("abcdefghijklmnopqrstuvwxyz", 5)
Local $aArray = _StringEqualSplit("12345678910", 3)
_ArrayDisplay($aArray)
_ArrayDisplay($aArray)


Func _StringEqualSplit($sString, $iNumChars)
Func _StringEqualSplit($sString, $iNumChars)
     If (Not IsString($sString)) Or $sString = "" Then Return SetError(1, 0, 0)
     If Not IsString($sString) Or $sString = "" Then Return SetError(1, 0, 0)
     If (Not IsInt($iNumChars)) Or $iNumChars < 1 Then Return SetError(2, 0, 0)
     If Not IsInt($iNumChars) Or $iNumChars < 1 Then Return SetError(2, 0, 0)
     Return StringRegExp($sString, "(?s).{1," & $iNumChars & "}", 3)
     Return StringRegExp($sString, "(?s).{1," & $iNumChars & "}", 3)
EndFunc
EndFunc
Line 59: Line 59:
}}
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">#include <Array.au3>
#include <Array.au3>
#include <String.au3>


Global $aArray = _StringEqualSplit('abcdefghijklmnopqrstuvwxyz', 5)
Local $aSplitEqual = _StringEqualSplit('abcdefghijklmnopqrstuvwxyz', 5)
 
_ArrayDisplay($aSplitEqual)
_ArrayDisplay($aArray)


; By czardas & modified by guinness >> http://www.autoitscript.com/forum/topic/139260-autoit-snippets/page__st__20#entry992149
; By czardas & modified by guinness >> http://www.autoitscript.com/forum/topic/139260-autoit-snippets/page__st__20#entry992149
; Version: 1.00. AutoIt: V3.3.8.1
; Version: 1.10. AutoIt: V3.3.8.1
; Splits a string into an equal number of characters. The 0th index returns the number of items.
; Splits a string into an equal number of characters. The 0th index returns the number of items.
Func _StringEqualSplit($sString, $iNumChars)
Func _StringEqualSplit($sString, $iNumChars)
    If (Not IsString($sString)) Or $sString = '' Then
If IsString($sString) = 0 Or $sString == '' Then
        Return SetError(1, 0, 0)
Return SetError(1, 0, 0)
    EndIf
EndIf


    If (Not IsInt($iNumChars)) Or $iNumChars < 1 Then
If IsInt($iNumChars) = 0 Or $iNumChars < 1 Then
        Return SetError(2, 0, 0)
Return SetError(2, 0, 0)
    EndIf
EndIf


    Local $tBuffer = DllStructCreate('char[' & $iNumChars & ']')
Local $aReturn = StringRegExp(_StringRepeat('0', 5) & $sString, '(?s).{1,' & $iNumChars & '}', 3)
 
$aReturn[0] = UBound($aReturn, 1) - 1
    DllCall('msvcrt.dll', 'ptr:cdecl', 'memset', 'ptr', DllStructGetPtr($tBuffer), 'int', 65, 'int', $iNumChars) ; By Zedna (StringRepeat)
Return $aReturn
 
EndFunc  ;==>_StringEqualSplit</syntaxhighlight>
    Local $aArray = StringRegExp(DllStructGetData($tBuffer, 1) & $sString, '(?s).{1,' & $iNumChars & '}', 3)
 
    $aArray[0] = Ubound($aArray, 1) - 1
 
    Return $aArray
EndFunc  ;==>_StringEqualSplit
</syntaxhighlight>


[[#top | Return To Contents]]
[[#top | Return To Contents]]
Line 223: Line 215:
[[#top | Return To Contents]]
[[#top | Return To Contents]]


== _StringRegExpSplit ==
== StringTrimLeftUntil ==
 
{{Snippet Header
| AuthorURL = 89462-TheDcoder
| AuthorName = TheDcoder
| Desc = Trim a string left until it reaches the delimiter character.
}}
 
<syntaxhighlight lang="autoit">
; #FUNCTION# ====================================================================================================================
; Name ..........: StringTrimLeftUntil
; Description ...: Trim a string left until it reaches the delimiter charecter
; Syntax ........: StringTrimLeftUntil($sDelimiter, $sString)
; Parameters ....: $sDelimiter          - Charecter(s) to trim until
;                  $sString            - A string value.
;                  $iOccurrence        - Trim until which occurrence of the delimiter [Default = 1st occurrence]
; Return values .: Trimmed String
; Author ........: TheDcoder
; Modified ......: Thanks to water for the Idea
; Remarks .......: This function is not case sensitive meaning "DeLiMiTeR" is same as "Delimiter"
; Related .......: StringTrimLeft
; Link ..........: https://www.autoitscript.com/forum/topic/139260-autoit-snippets/?do=findComment&comment=1250764
; Example .......: No
; ===============================================================================================================================
Func StringTrimLeftUntil($sDelimiter, $sString, $iOccurrence = 1)
$iDelimiterLen = StringLen($sDelimiter)
    $sString = StringTrimLeft($sString, StringInStr($sString, $sDelimiter, 2, $iOccurrence) + ($iDelimiterLen - 1)) ; This is a little hard to explain:
#cs =========================================================================================================================================|
| What it does is:                                                                                                                          |
| 1. Find the delimiter's position in the string (StringInStr)                                                                              |
| 2. Add delimiter's Length to delimiters position (I remove 1 for delimiter's length because StringInStr already contains that 1 charecter) |
| 3. Trim the string :)                                                                                                                      |
#ce =========================================================================================================================================|
    Return $sString ; Return the String :D
EndFunc  ;==>StringTrimLeftUntil
</syntaxhighlight>
 
[[#top | Return To Contents]]
 
== StringTrimRightUntil ==
 
{{Snippet Header
| AuthorURL = 89462-TheDcoder
| AuthorName = TheDcoder
| Desc = Trim a string right until it reaches the delimiter character.
}}
 
<syntaxhighlight lang="autoit">
; #FUNCTION# ====================================================================================================================
; Name ..........: StringTrimRightUntil
; Description ...: Trim a string right until it reaches the delimiter charecter
; Syntax ........: StringTrimRightUntil($sDelimiter, $sString)
; Parameters ....: $sDelimiter          - Charecter(s) to trim until
;                  $sString            - A string value.
;                  $iOccurrence        - Trim until which occurrence of the delimiter [Default = 1st occurrence]
; Return values .: Trimmed String
; Author ........: TheDcoder
; Modified ......: Me
; Remarks .......: This function is not case sensitive meaning "DeLiMiTeR" is same as "Delimiter"
; Related .......: StringTrimRight
; Link ..........: https://www.autoitscript.com/forum/topic/139260-autoit-snippets/?do=findComment&comment=1250764
; Example .......: No
; ===============================================================================================================================
Func StringTrimRightUntil($sDelimiter, $sString, $iOccurrence = 1)
$iStringLen = StringLen($sString) ; We need string's len
$iDelimiterLen = StringLen($sDelimiter) ; We need delimiter's len too
    $sString = StringTrimRight($sString, ($iStringLen - StringInStr($sString, $sDelimiter, 2, $iOccurrence)) + $iDelimiterLen) ; Explanation:
#cs ============================================================================================|
| 1. Find delimiters postition                                                                  |
| 2. Remove String's len from delimiters position to invert the trimming process (to the right) |
| 3. Add the delimiter's len to trim the delimiter                                              |
| 4. Trim the string!!!                                                                        |
#ce ============================================================================================|
    Return $sString ; Return the String
EndFunc  ;==>StringTrimRightUntil
</syntaxhighlight>
 
 
 
== StringBetween ==
 
{{Snippet Header
| AuthorURL = 77301-trong
| AuthorName = Trong
| Desc = Find strings between two string delimiters.
}}
 
<syntaxhighlight lang="autoit">
 
; EG---------------------------------------------- -
Local $sString = StringBetween("<test>C</test><test>B</test><test>A</test>", "<test>", "</test>")
MsgBox(0, "StringBetween", "Strings between <test> and </test>" & @CRLF & "Is: " & $sString & @CRLF & "In text: <test>C</test><test>B</test><test>A</test>")
; ------------------------------------------------ -
 
; #FUNCTION# ====================================================================================================================
; Name ..........: StringBetween
; Description ...: Find strings between two string delimiters
; Syntax ........: StringBetween($sString, $sStart, $sEnd)
; Parameters ....: $sString        - The string to search.
; Parameters ....: $sStart          - The beginning of the string to find.
;                  $sEnd            - The end of the string to find.
; Return values .: Success: a found string
;                  Failure: sets the @error flag to non-zero.
;                  @error: 1 - No strings found.
; Author ........: Trong
; Related .......: _StringBetween
; Link ..........: https://www.autoitscript.com/forum/topic/139260-autoit-snippets/?do=findComment&comment=1290019
; Example .......: Yes
; ===============================================================================================================================
 
Func StringBetween($sString, $sStart, $sEnd)
$sString = StringReverse(StringTrimLeft($sString, StringInStr($sString, $sStart) + StringLen($sStart) - 1))
$sString = StringReverse(StringTrimLeft($sString, StringInStr($sString, StringReverse($sEnd)) + StringLen($sEnd) - 1))
Return SetError(StringLen($sString) < 1, 0, $sString)
EndFunc  ;==>StringBetween
 
</syntaxhighlight>
 
[[#top | Return To Contents]]
 
== _StringGetDigitLeft ==


{{Snippet Header
{{Snippet Header
| AuthorURL = 69238-dany
| AuthorURL = 102275-donchunior
| AuthorName = dany
| AuthorName = DonChunior
| Desc = Split a string based on a regular expression.
| Desc = Returns the string on the left side of a search string consisting of digits only.
}}
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
; Split a string based on a regular expression.
#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7
; http://www.autoitscript.com/forum/topic/139260-autoit-snippets/page__st__120#entry1036533
 
#include <Array.au3> ; Only needed for example.
; Returns the string on the left side of a search string consisting of digits only
Global $sString = 'List:' & _
 
         '1. Bananas' & _
ConsoleWrite(_StringGetDigitLeft("1234abcd5678") & @CRLF) ; Results in "1234"
        '2. Coconuts' & _
 
        '3. Apples'
Func _StringGetDigitLeft(Const ByRef $sString)
_ArrayDisplay(_StringRegExpSplit($sString, '[0-9]+\. ?')) ; Use the numbering (#.) to split on.
    If Not IsString($sString) Then
Global $bBinary = Binary($sString)
         Return SetError(1, 0, "")
_ArrayDisplay(_StringRegExpSplit($bBinary, '3[0-9]2E(20)?'))
    EndIf
 
    Local $iLength = StringLen($sString)
    Local $sCheckString = ""
    Local $sLeft = ""
 
    For $i = 1 To $iLength
        $sCheckString = StringLeft($sString, $i)
        If StringIsDigit($sCheckString) Then
            $sLeft = $sCheckString
        Else
            ExitLoop
        EndIf
    Next
 
    Return $sLeft
EndFunc  ;==>_StringGetDigitLeft
</syntaxhighlight>
 
[[#top | Return To Contents]]
 
== _StringGetDigitRight ==
 
{{Snippet Header
| AuthorURL = 102275-donchunior
| AuthorName = DonChunior
| Desc = Returns the string on the right side of a search string consisting of digits only.
}}
 
<syntaxhighlight lang="autoit">
#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7
 
; Returns the string on the right side of a search string consisting of digits only
 
ConsoleWrite(_StringGetDigitRight("1234abcd5678") & @CRLF) ; Results in "5678"
 
Func _StringGetDigitRight(Const ByRef $sString)
    If Not IsString($sString) Then
        Return SetError(1, 0, "")
    EndIf
 
    Local $iLength = StringLen($sString)
    Local $sCheckString = ""
    Local $sRight = ""
 
    For $i = $iLength To 1 Step -1
        $sCheckString = StringRight($sString, $iLength - $i + 1)
        If StringIsDigit($sCheckString) Then
            $sRight = $sCheckString
        Else
            ExitLoop
        EndIf
    Next


; #FUNCTION# ===================================================================
    Return $sRight
; Name...........: _StringRegExpSplit
EndFunc  ;==>_StringGetDigitRight
; Description ...: Split a string according to a regular expression.
; Syntax.........: _StringRegExpSplit($sString, $sPattern)
; Parameters ....: $sString - String: String to split.
;                  $sPattern - String: Regular expression to split on.
; Return values .: Success - Array: Array of substrings, the total is in $array[0].
;                  Failure - Array: The count is 1 ($array[0]) and the full string is returned ($array[1]) and sets @error:
;                  |1 Delimiter not found.
;                  |2 Bad RegExp pattern, @extended contains the offset of the error in the pattern.
;                  |3 No suitable placeholder delimiter could be constructed.
; Author ........: dany
; Modified ......: czardas, AZJIO
; Remarks .......:
; Related .......:
; ==============================================================================
Func _StringRegExpSplit($sString, $sPattern)
Local $sSplit, $sDelim, $aError[2] = [1, $sString]
For $i = 1 To 31
$sDelim &= Chr($i)
If Not StringInStr($sString, $sDelim) Then ExitLoop
If 32 = StringLen($sDelim) Then Return SetError(3, 0, $aError)
Next
$sSplit = StringRegExpReplace($sString, $sPattern, $sDelim)
If @error Then Return SetError(2, @extended, $aError)
If @extended = 0 Then Return SetError(1, 0, $aError)
If Not IsBinary($sString) Then Return StringSplit($sSplit, $sDelim, 1)
$sSplit = StringSplit($sSplit, $sDelim, 1)
For $i = 2 To $sSplit[0]
$sSplit[$i] = '0x' & $sSplit[$i]
Next
Return $sSplit
EndFunc  ;==>_StringRegExpSplit
</syntaxhighlight>
</syntaxhighlight>


[[#top | Return To Contents]]
[[#top | Return To Contents]]

Latest revision as of 11:59, 19 October 2020


Please always credit an author in your script if you use their code. It is only polite.


_StringEqualSplit

Author: czardas








#include <Array.au3>
Local $aArray = _StringEqualSplit("12345678910", 3)
_ArrayDisplay($aArray)

Func _StringEqualSplit($sString, $iNumChars)
    If Not IsString($sString) Or $sString = "" Then Return SetError(1, 0, 0)
    If Not IsInt($iNumChars) Or $iNumChars < 1 Then Return SetError(2, 0, 0)
    Return StringRegExp($sString, "(?s).{1," & $iNumChars & "}", 3)
EndFunc

Return To Contents

_StringGetChrCount

Author: GEOSoft







Check how many times the word 'test' appears in the string. 1 = Case-sensitive or 0 = Non Case-sensitive.


; Check how many times the word 'test' appears in the string. 1 = Case-sensitive or 0 = Non Case-sensitive.
ConsoleWrite(_StringGetChrCount("test teste test", "TEST", 0) & @CRLF)

Func _StringGetChrCount($sStr, $sChr, $iCase = 0)
	If $iCase <> 0 Then
		$iCase = 1
	EndIf
	StringReplace($sStr, $sChr, $sChr, 0, $iCase)
	Return @extended
EndFunc   ;==>_StringGetChrCount

Return To Contents

_StringEqualSplit

Author: czardas




Modified: guinness




Splits a string into an equal number of characters. The 0th index returns the number of items.


#include <Array.au3>
#include <String.au3>

Local $aSplitEqual = _StringEqualSplit('abcdefghijklmnopqrstuvwxyz', 5)
_ArrayDisplay($aSplitEqual)

; By czardas & modified by guinness >> http://www.autoitscript.com/forum/topic/139260-autoit-snippets/page__st__20#entry992149
; Version: 1.10. AutoIt: V3.3.8.1
; Splits a string into an equal number of characters. The 0th index returns the number of items.
Func _StringEqualSplit($sString, $iNumChars)
	If IsString($sString) = 0 Or $sString == '' Then
		Return SetError(1, 0, 0)
	EndIf

	If IsInt($iNumChars) = 0 Or $iNumChars < 1 Then
		Return SetError(2, 0, 0)
	EndIf

	Local $aReturn = StringRegExp(_StringRepeat('0', 5) & $sString, '(?s).{1,' & $iNumChars & '}', 3)
	$aReturn[0] = UBound($aReturn, 1) - 1
	Return $aReturn
EndFunc   ;==>_StringEqualSplit

Return To Contents

_StringIsNum

Author: smartee








ConsoleWrite(StringIsNum('Example') & @CRLF)
ConsoleWrite(StringIsNum('123456') & @CRLF)
ConsoleWrite(StringIsNum('Example & 123456') & @CRLF)

Func StringIsNum($sString)
    Return StringRegExp($sString, "^([0-9]*(\.[0-9]+){1}|[0-9]+(\.[0-9]*){0,1})$") = 1
EndFunc   ;==>StringIsNum

Return To Contents

_StringRemoveLine

Author: SmOke_N








Global $string = 'This is an example' & @CRLF & 'Of deleting a line' & @CRLF & 'If you know at least the beginning text of the line.'

MsgBox(0, 'Original', $string)

Global $deleteline = _StringRemoveLine($string, 'Of deleting')

MsgBox(0, 'Deleted Line', $deleteline)

Func _StringRemoveLine($hFile, $sDelete)
    If FileExists($hFile) Then $hFile = FileRead($hFile);Remove If FileExists($hFile) Then << only
    Local $nSNS = StringInStr($hFile, @CRLF & $sDelete) - 1
    Local $sSFH = StringLeft($hFile, $nSNS)
    Local $sRL = StringTrimLeft($hFile, StringLen($sSFH) + 2)
    Local $sLLEN = StringLen(StringLeft($sRL, StringInStr($sRL, @CRLF)))
    If Not $sLLEN Then $sLLEN = StringLen($sRL)
    Return $sSFH & StringTrimLeft($hFile, $sLLEN + $nSNS + 2)
EndFunc ;==>_StringRemoveLine()

Return To Contents

_StringReplaceBlank

Author: SmOke_N







Remove blank lines from a File


; Remove blank lines from a File
Local $sString = "I am a string" & @CRLF & @CRLF & _
		"With Empty Lines" & @CRLF & @CRLF & _
		"Please Remove those empty lines"

MsgBox(4096, "Before", $sString)

$sString = _StringReplaceBlank($sString, 1)

MsgBox(4096, "Replaced: " & @extended & " lines.", $sString)

Func _StringReplaceBlank($sString, $sSpaces = "")
	If $sSpaces Then
		$sSpaces = "\s*"
	EndIf

	$sString = StringRegExpReplace($sString, "(?s)\r\n" & $sSpaces & "\r\n", @CRLF)

	If @extended Then
		Return SetError(0, @extended, $sString)
	EndIf

	Return SetError(1, 0, $sString)
EndFunc   ;==>_StringReplaceBlank

Return To Contents

_StringTrimLeftIsEqual

Author: guinness







Strip a character/word from the leftmost part of a string.


ConsoleWrite(_StringTrimLeftIsEqual('C:\Test', 'C:\') & @CRLF) ; Returns Test as the string 'C:\' is stripped from the left.
ConsoleWrite(_StringTrimLeftIsEqual('C:\Test\', 'C') & @CRLF) ; Returns :\Test as the character 'C' is stripped from the left.
ConsoleWrite(_StringTrimLeftIsEqual('C:\Test\', 'Test') & @CRLF) ; Returns the initial string as the string 'Test' was not found to the leftmost part of the string.

; Version: 1.00. AutoIt: V3.3.8.1
; Strip a character/word from the leftmost part of a string.
Func _StringTrimLeftIsEqual($sString, $sStringTrim)
    Local $aStringTrim[2] = [0, StringLen($sStringTrim)]
    Return StringTrimLeft($sString, $aStringTrim[Number(StringLeft($sString, $aStringTrim[1]) == $sStringTrim)])
EndFunc   ;==>_StringTrimLeftIsEqual

Return To Contents

_StringTrimRightIsEqual

Author: guinness







Strip a character/word from the rightmost part of a string.


ConsoleWrite(_StringTrimRightIsEqual('C:\Test', 'Test') & @CRLF) ; Returns C:\ as the string 'Test' is stripped from the right.
ConsoleWrite(_StringTrimRightIsEqual('C:\Test\', '\') & @CRLF) ; Returns C:\Test as the character '\' is stripped from the right.
ConsoleWrite(_StringTrimRightIsEqual('C:\Test\', 'Test') & @CRLF) ; Returns the initial string as the string 'Test' was not found to the rightmost part of the string.

; Version: 1.00. AutoIt: V3.3.8.1
; Strip a character/word from the rightmost part of a string.
Func _StringTrimRightIsEqual($sString, $sStringTrim)
    Local $aStringTrim[2] = [0, StringLen($sStringTrim)]
    Return StringTrimRight($sString, $aStringTrim[Number(StringRight($sString, $aStringTrim[1]) == $sStringTrim)])
EndFunc   ;==>_StringTrimRightIsEqual

Return To Contents

StringTrimLeftUntil

Author: TheDcoder







Trim a string left until it reaches the delimiter character.


; #FUNCTION# ====================================================================================================================
; Name ..........: StringTrimLeftUntil
; Description ...: Trim a string left until it reaches the delimiter charecter
; Syntax ........: StringTrimLeftUntil($sDelimiter, $sString)
; Parameters ....: $sDelimiter          - Charecter(s) to trim until
;                  $sString             - A string value.
;                  $iOccurrence         - Trim until which occurrence of the delimiter [Default = 1st occurrence]
; Return values .: Trimmed String
; Author ........: TheDcoder
; Modified ......: Thanks to water for the Idea 
; Remarks .......: This function is not case sensitive meaning "DeLiMiTeR" is same as "Delimiter"
; Related .......: StringTrimLeft
; Link ..........: https://www.autoitscript.com/forum/topic/139260-autoit-snippets/?do=findComment&comment=1250764
; Example .......: No
; ===============================================================================================================================
Func StringTrimLeftUntil($sDelimiter, $sString, $iOccurrence = 1)
	$iDelimiterLen = StringLen($sDelimiter)
    $sString = StringTrimLeft($sString, StringInStr($sString, $sDelimiter, 2, $iOccurrence) + ($iDelimiterLen - 1)) ; This is a little hard to explain:
	#cs =========================================================================================================================================|
	| What it does is:                                                                                                                           |
	| 1. Find the delimiter's position in the string (StringInStr)                                                                               |
	| 2. Add delimiter's Length to delimiters position (I remove 1 for delimiter's length because StringInStr already contains that 1 charecter) |
	| 3. Trim the string :)                                                                                                                      |
	#ce =========================================================================================================================================|
    Return $sString ; Return the String :D
EndFunc   ;==>StringTrimLeftUntil

Return To Contents

StringTrimRightUntil

Author: TheDcoder







Trim a string right until it reaches the delimiter character.


; #FUNCTION# ====================================================================================================================
; Name ..........: StringTrimRightUntil
; Description ...: Trim a string right until it reaches the delimiter charecter
; Syntax ........: StringTrimRightUntil($sDelimiter, $sString)
; Parameters ....: $sDelimiter          - Charecter(s) to trim until
;                  $sString             - A string value.
;                  $iOccurrence         - Trim until which occurrence of the delimiter [Default = 1st occurrence]
; Return values .: Trimmed String
; Author ........: TheDcoder
; Modified ......: Me
; Remarks .......: This function is not case sensitive meaning "DeLiMiTeR" is same as "Delimiter"
; Related .......: StringTrimRight
; Link ..........: https://www.autoitscript.com/forum/topic/139260-autoit-snippets/?do=findComment&comment=1250764
; Example .......: No
; ===============================================================================================================================
Func StringTrimRightUntil($sDelimiter, $sString, $iOccurrence = 1)
	$iStringLen = StringLen($sString) ; We need string's len
	$iDelimiterLen = StringLen($sDelimiter) ; We need delimiter's len too
    $sString = StringTrimRight($sString, ($iStringLen - StringInStr($sString, $sDelimiter, 2, $iOccurrence)) + $iDelimiterLen) ; Explanation:
	#cs ============================================================================================|
	| 1. Find delimiters postition                                                                  |
	| 2. Remove String's len from delimiters position to invert the trimming process (to the right) |
	| 3. Add the delimiter's len to trim the delimiter                                              |
	| 4. Trim the string!!!                                                                         |
	#ce ============================================================================================|
    Return $sString ; Return the String 
EndFunc   ;==>StringTrimRightUntil


StringBetween

Author: Trong







Find strings between two string delimiters.


; EG---------------------------------------------- -
Local $sString = StringBetween("<test>C</test><test>B</test><test>A</test>", "<test>", "</test>")
MsgBox(0, "StringBetween", "Strings between <test> and </test>" & @CRLF & "Is: " & $sString & @CRLF & "In text: <test>C</test><test>B</test><test>A</test>")
; ------------------------------------------------ -

; #FUNCTION# ====================================================================================================================
; Name ..........: StringBetween
; Description ...: Find strings between two string delimiters
; Syntax ........: StringBetween($sString, $sStart, $sEnd)
; Parameters ....: $sString         - The string to search.
; Parameters ....: $sStart          - The beginning of the string to find.
;                  $sEnd            - The end of the string to find.
; Return values .: Success: a found string
;                  Failure: sets the @error flag to non-zero.
;                  @error: 1 - No strings found.
; Author ........: Trong
; Related .......: _StringBetween
; Link ..........: https://www.autoitscript.com/forum/topic/139260-autoit-snippets/?do=findComment&comment=1290019
; Example .......: Yes
; ===============================================================================================================================

Func StringBetween($sString, $sStart, $sEnd)
	$sString = StringReverse(StringTrimLeft($sString, StringInStr($sString, $sStart) + StringLen($sStart) - 1))
	$sString = StringReverse(StringTrimLeft($sString, StringInStr($sString, StringReverse($sEnd)) + StringLen($sEnd) - 1))
	Return SetError(StringLen($sString) < 1, 0, $sString)
EndFunc   ;==>StringBetween

Return To Contents

_StringGetDigitLeft

Author: DonChunior







Returns the string on the left side of a search string consisting of digits only.


#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7

; Returns the string on the left side of a search string consisting of digits only

ConsoleWrite(_StringGetDigitLeft("1234abcd5678") & @CRLF) ; Results in "1234"

Func _StringGetDigitLeft(Const ByRef $sString)
    If Not IsString($sString) Then
        Return SetError(1, 0, "")
    EndIf

    Local $iLength = StringLen($sString)
    Local $sCheckString = ""
    Local $sLeft = ""

    For $i = 1 To $iLength
        $sCheckString = StringLeft($sString, $i)
        If StringIsDigit($sCheckString) Then
            $sLeft = $sCheckString
        Else
            ExitLoop
        EndIf
    Next

    Return $sLeft
EndFunc   ;==>_StringGetDigitLeft

Return To Contents

_StringGetDigitRight

Author: DonChunior







Returns the string on the right side of a search string consisting of digits only.


#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7

; Returns the string on the right side of a search string consisting of digits only

ConsoleWrite(_StringGetDigitRight("1234abcd5678") & @CRLF) ; Results in "5678"

Func _StringGetDigitRight(Const ByRef $sString)
    If Not IsString($sString) Then
        Return SetError(1, 0, "")
    EndIf

    Local $iLength = StringLen($sString)
    Local $sCheckString = ""
    Local $sRight = ""

    For $i = $iLength To 1 Step -1
        $sCheckString = StringRight($sString, $iLength - $i + 1)
        If StringIsDigit($sCheckString) Then
            $sRight = $sCheckString
        Else
            ExitLoop
        EndIf
    Next

    Return $sRight
EndFunc   ;==>_StringGetDigitRight

Return To Contents