Snippets ( AutoIt String ): Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
No edit summary
(Edited snippets to conform to template Snippet Header.)
Line 1: Line 1:
__TOC__
__TOC__
[[category:Snippets]]
[[category:Snippets]]


{{Snippet Credit Header}}
{{Snippet Credit Header}}


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _StringEqualSplit() ~ Author - [http://www.autoitscript.com/forum/user/15769-czardas/ czardas] '''</blockquote> =====
== _StringEqualSplit ==
 
{{Snippet Header
| AuthorURL = 15769-czardas
| AuthorName = czardas
}}
 
<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
#include <Array.au3>
#include <Array.au3>
Line 16: Line 23:
EndFunc
EndFunc
</syntaxhighlight>
</syntaxhighlight>
[[#top|Return To Contents]]


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _StringGetChrCount() ~ Author - [http://www.autoitscript.com/forum/user/52-geosoft/ GEOSoft] '''</blockquote> =====
[[#top | Return To Contents]]
 
== _StringGetChrCount ==
 
{{Snippet Header
| AuthorURL = 52-geosoft
| AuthorName = GEOSoft
| Desc = Check how many times the word 'test' appears in the string. 1 = Case-sensitive or 0 = Non Case-sensitive.
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
Line 32: Line 46:
EndFunc  ;==>_StringGetChrCount
EndFunc  ;==>_StringGetChrCount
</syntaxhighlight>
</syntaxhighlight>
[[#top|Return To Contents]]


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _StringEqualSplit() ~ Author - [http://www.autoitscript.com/forum/user/35302-guinness/ guinness] '''</blockquote> =====
[[#top | Return To Contents]]
 
== _StringEqualSplit ==
 
{{Snippet Header
| AuthorURL = 15769-czardas
| AuthorName = czardas
| ModifierURL = 35302-guinness
| ModifierName = guinness
| Desc = Splits a string into an equal number of characters. The 0th index returns the number of items.
}}
 
<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
#include <Array.au3>
#include <Array.au3>


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


Line 48: Line 73:
         Return SetError(1, 0, 0)
         Return SetError(1, 0, 0)
     EndIf
     EndIf
     If (Not IsInt($iNumChars)) Or $iNumChars < 1 Then
     If (Not IsInt($iNumChars)) Or $iNumChars < 1 Then
         Return SetError(2, 0, 0)
         Return SetError(2, 0, 0)
     EndIf
     EndIf
     Local $tBuffer = DllStructCreate('char[' & $iNumChars & ']')
     Local $tBuffer = DllStructCreate('char[' & $iNumChars & ']')
     DllCall('msvcrt.dll', 'ptr:cdecl', 'memset', 'ptr', DllStructGetPtr($tBuffer), 'int', 65, 'int', $iNumChars) ; By Zedna (StringRepeat)
     DllCall('msvcrt.dll', 'ptr:cdecl', 'memset', 'ptr', DllStructGetPtr($tBuffer), 'int', 65, 'int', $iNumChars) ; By Zedna (StringRepeat)
     Local $aArray = StringRegExp(DllStructGetData($tBuffer, 1) & $sString, '(?s).{1,' & $iNumChars & '}', 3)
     Local $aArray = StringRegExp(DllStructGetData($tBuffer, 1) & $sString, '(?s).{1,' & $iNumChars & '}', 3)
     $aArray[0] = Ubound($aArray, 1) - 1
     $aArray[0] = Ubound($aArray, 1) - 1
     Return $aArray
     Return $aArray
EndFunc  ;==>_StringEqualSplit
EndFunc  ;==>_StringEqualSplit
</syntaxhighlight>
</syntaxhighlight>
[[#top|Return To Contents]]


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _StringIsNum() ~ Author - [http://www.autoitscript.com/forum/user/38301-smartee/ smartee] '''</blockquote> =====
[[#top | Return To Contents]]
 
== _StringIsNum ==
 
{{Snippet Header
| AuthorURL = 38301-smartee
| AuthorName = smartee
}}
 
<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
ConsoleWrite(StringIsNum('Example') & @CRLF)
ConsoleWrite(StringIsNum('Example') & @CRLF)
Line 70: Line 108:
EndFunc  ;==>StringIsNum
EndFunc  ;==>StringIsNum
</syntaxhighlight>
</syntaxhighlight>
[[#top|Return To Contents]]


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _StringRemoveLine() ~ Author - [http://www.autoitscript.com/forum/user/4813-smoke-n/ SmOke_N] '''</blockquote> =====
[[#top | Return To Contents]]
 
== _StringRemoveLine ==
 
{{Snippet Header
| AuthorURL = 4813-smoke-n
| AuthorName = SmOke_N
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
Global $string = 'This is an example' & @CRLF & 'Of deleting a line' & @CRLF & 'If you know at least the beginning text of the line.'
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)
MsgBox(0, 'Original', $string)
Global $deleteline = _StringRemoveLine($string, 'Of deleting')
Global $deleteline = _StringRemoveLine($string, 'Of deleting')
MsgBox(0, 'Deleted Line', $deleteline)
MsgBox(0, 'Deleted Line', $deleteline)


Line 90: Line 137:
EndFunc ;==>_StringRemoveLine()
EndFunc ;==>_StringRemoveLine()
</syntaxhighlight>
</syntaxhighlight>
[[#top|Return To Contents]]


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _StringReplaceBlank() ~ Author - [http://www.autoitscript.com/forum/user/4813-smoke-n/ SmOke_N] '''</blockquote> =====
[[#top | Return To Contents]]
 
== _StringReplaceBlank ==
 
{{Snippet Header
| AuthorURL = 4813-smoke-n
| AuthorName = SmOke_N
| Desc = Remove blank lines from a File
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
Line 99: Line 153:
"With Empty Lines" & @CRLF & @CRLF & _
"With Empty Lines" & @CRLF & @CRLF & _
"Please Remove those empty lines"
"Please Remove those empty lines"
MsgBox(4096, "Before", $sString)
MsgBox(4096, "Before", $sString)
$sString = _StringReplaceBlank($sString, 1)
$sString = _StringReplaceBlank($sString, 1)
MsgBox(4096, "Replaced: " & @extended & " lines.", $sString)
MsgBox(4096, "Replaced: " & @extended & " lines.", $sString)


Line 107: Line 164:
$sSpaces = "\s*"
$sSpaces = "\s*"
EndIf
EndIf
$sString = StringRegExpReplace($sString, "(?s)\r\n" & $sSpaces & "\r\n", @CRLF)
$sString = StringRegExpReplace($sString, "(?s)\r\n" & $sSpaces & "\r\n", @CRLF)
If @extended Then
If @extended Then
Return SetError(0, @extended, $sString)
Return SetError(0, @extended, $sString)
EndIf
EndIf
Return SetError(1, 0, $sString)
Return SetError(1, 0, $sString)
EndFunc  ;==>_StringReplaceBlank
EndFunc  ;==>_StringReplaceBlank
</syntaxhighlight>
</syntaxhighlight>
[[#top|Return To Contents]]


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _StringTrimLeftIsEqual() ~ Author - [http://www.autoitscript.com/forum/user/35302-guinness/ guinness] '''</blockquote> =====
[[#top | Return To Contents]]
 
== _StringTrimLeftIsEqual ==
 
{{Snippet Header
| AuthorURL = 35302-guinness
| AuthorName = guinness
| Desc = Strip a character/word from the leftmost part of a string.
}}
 
<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
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 string 'C:\' is stripped from the left.
Line 129: Line 197:
EndFunc  ;==>_StringTrimLeftIsEqual
EndFunc  ;==>_StringTrimLeftIsEqual
</syntaxhighlight>
</syntaxhighlight>
[[#top|Return To Contents]]


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _StringTrimRightIsEqual() ~ Author - [http://www.autoitscript.com/forum/user/35302-guinness/ guinness] '''</blockquote> =====
[[#top | Return To Contents]]
 
== _StringTrimRightIsEqual ==
 
{{Snippet Header
| AuthorURL = 35302-guinness
| AuthorName = guinness
| Desc = Strip a character/word from the rightmost part of a string.
}}
 
<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
ConsoleWrite(_StringTrimRightIsEqual('C:\Test', 'Test') & @CRLF) ; Returns C:\ as the string 'Test' is stripped from the right.
ConsoleWrite(_StringTrimRightIsEqual('C:\Test', 'Test') & @CRLF) ; Returns C:\ as the string 'Test' is stripped from the right.
Line 144: Line 220:
EndFunc  ;==>_StringTrimRightIsEqual
EndFunc  ;==>_StringTrimRightIsEqual
</syntaxhighlight>
</syntaxhighlight>
[[#top|Return To Contents]]
 
[[#top | Return To Contents]]

Revision as of 20:31, 13 November 2012


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


_StringEqualSplit

Author: czardas








#include <Array.au3>
Global $aArray = _StringEqualSplit("abcdefghijklmnopqrstuvwxyz", 5)
_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>

Global $aArray = _StringEqualSplit('abcdefghijklmnopqrstuvwxyz', 5)

_ArrayDisplay($aArray)

; 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
; Splits a string into an equal number of characters. The 0th index returns the number of items.
Func _StringEqualSplit($sString, $iNumChars)
    If (Not IsString($sString)) Or $sString = '' Then
        Return SetError(1, 0, 0)
    EndIf

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

    Local $tBuffer = DllStructCreate('char[' & $iNumChars & ']')

    DllCall('msvcrt.dll', 'ptr:cdecl', 'memset', 'ptr', DllStructGetPtr($tBuffer), 'int', 65, 'int', $iNumChars) ; By Zedna (StringRepeat)

    Local $aArray = StringRegExp(DllStructGetData($tBuffer, 1) & $sString, '(?s).{1,' & $iNumChars & '}', 3)

    $aArray[0] = Ubound($aArray, 1) - 1

    Return $aArray
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