Ticket #2174: StringInsertNEW.au3

File StringInsertNEW.au3, 1.1 KB (added by amarcruz, on Apr 4, 2012 at 8:43:56 AM)

_StringInsert, corrected & minor improved version

Line 
1Func _StringInsert($s_String, $s_InsertString, $i_Position)
2 Local $i_Length
3
4 If ($s_String = "") Or (Not IsString($s_String)) Then
5 Return SetError(1, 0, $s_String) ; Source string empty / not a string
6 ElseIf ($s_InsertString = "") Or (Not IsString($s_InsertString)) Then
7 Return SetError(2, 0, $s_String) ; Insert string empty / not a string
8 Else
9 $i_Length = StringLen($s_String) ; Take a note of the length of the source string
10 If (Abs($i_Position) > $i_Length) Or (Not IsInt($i_Position)) Then
11 Return SetError(3, 0, $s_String) ; Invalid position
12 EndIf
13 EndIf
14
15 ; @CHANGED START : 2012-04-04 aMarCruz
16 ; Test <0 here to catch pos 0 with negative offset (ex. if $i_Length=5 and $i_Position=-5)
17 If $i_Position < 0 Then
18 $i_Position = $i_Length + $i_Position
19 EndIf
20
21 ; If $i_Position at start of string
22 If $i_Position = 0 Then
23 Return $s_InsertString & $s_String ; Just add them up :) Easy :)
24 EndIf
25 Return StringLeft($s_String, $i_Position) & $s_InsertString & StringRight($s_String, $i_Length - $i_Position)
26 ; @CHANGED END
27
28
29EndFunc ;==>_StringInsert