Jump to content

New String Function : StringMultiInsert ()


clearguy
 Share

Recommended Posts

Hi all,

I hope this coul be useful.

I started this script as I noticed that StringInsert was just made up for one insert,and I found it annoying to doing this thousand times with loops...so here is the new MultiInsertString () function.

I took the SetError() from StringInsert() source.

Examples are included.

(ps:my english isn't that good lol :P )

#cs~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;~                               ;~~           ;~                     ;~
;~ AutoIt Version: 3.2.0.1        ;~~         ;~                     ;~
;~ Author:clearguy               ;~~           ;~~~~~~~~~~~~~~~~~~~~;
;~~                                ;~~~~~~~~~~~~~~~~~~~~~~~~~~;    ;~
;~~ Script Function:                                        ;~~     ;~
;~~ Insert string in a string between all characters         ;~~   ;~
;~~          or in a range of characters.                   ;~~     ;~
#ce~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;~~     ///// _StringMultiInsert \\\\\\
;~Script Start 
;~~ Example
$String = 'Allseparedwith a slash "/"'
MsgBox(0,"_StringMultiInsert", _StringMultiInsert($String, "/", 1, 14))
;~~
$String = ':::::::::'
MsgBox(0,"_StringMultiInsert", "Bad smileys => "& _StringMultiInsert($String, "/" ))
;~~
$String = 0
_StringMultiInsert($String,"!" )
MsgBox(0,"_StringMultiInsert", "Error 1 : "& @error)
 ;~~Function
Func _StringMultiInsert($s_String, $s_InsertString, $s_StartRange = 0, $s_RangeStop = 0)
    Local $s_NewString, $i_Length, $s_SplitString
    
        If $s_String = "" Or (Not IsString($s_String)) Then
        SetError(1) ;~~ Source string empty / not a string
        Return $s_String
    ElseIf $s_InsertString = "" Or (Not IsString($s_String)) Then
        SetError(2) ;~~ Insert string empty / not a string
        Return $s_String
    Else
        $i_Length = StringLen($s_String) ; Take a note of the length of the source string
        If (Abs($s_StartRange) > $i_Length) Or (Not IsInt($s_StartRange)) Or (Not IsInt($s_RangeStop)) Or ($s_RangeStop > $i_Length) Or ($s_RangeStop < $s_StartRange) Then
            SetError(3) ;~~ Invalid position
            Return $s_String
        EndIf
    EndIf
    
    $i_Length = StringLen($s_String)  ;~~ take string length
    $s_SplitString = StringSplit($s_String, "") ;~~split all chars in string
    
    If $s_StartRange = 0 And $s_RangeStop = 0 Then
        For $i = 1 To $i_Length
            $s_NewString = $s_NewString & $s_SplitString[$i] & $s_InsertString ;~~ simply add InsertString everywhere
        Next
        Return $s_NewString
    ElseIf  $s_StartRange <> 0  Then
        If $s_StartRange > 1 And $s_RangeStop > $s_StartRange Then
            $s_NewString = StringLeft($s_String,$s_StartRange - 1)  ;~~ add clear string before starting the multi-insert
            For $i = $s_StartRange To $s_RangeStop
                $s_NewString = $s_NewString & $s_SplitString[$i] & $s_InsertString ;~~ add InsertString in a range of string
            Next
            $s_NewString = $s_NewString & StringRight($s_String, ($i_Length - $s_RangeStop) ) ;~~ add clear string after multi-insert range
            Return $s_NewString
        ElseIf $s_StartRange = 1 And $s_RangeStop > $s_StartRange Then
            For $i = 1 To $s_RangeStop
                $s_NewString = $s_NewString & $s_SplitString[$i] & $s_InsertString   ;~~ multi-insert since the start
            Next
            $s_NewString = $s_NewString & StringRight($s_String, ($i_Length - $s_RangeStop) ) ;~~ until the range stop
            Return $s_NewString
        EndIf
    EndIf
EndFunc

StringMultiInsert.au3

Link to comment
Share on other sites

nice code, i made a simmiliar one to yours, with lesser code !

$String = 'Allseparedwith a slash "/"'
MsgBox(0,"_StringMultiInsert", _StringMultiInsert2($String, "/",1,14))

Func _StringMultiInsert2($string, $insertstring, $Startrange=0, $Stoprange=0)
    Local $ret="",$count=0
    If $Startrange > $Stoprange Then Return -1
    $lSplit = StringSplit( $string , " " )
    For $i = 1 To $lSplit[0]
        If $Stoprange = 0 Then $Stoprange = StringLen($string)
        $rsplit = StringSplit( $lSplit[$i] , "" )
        For $i_o = 1 To $rsplit[0]
            $count += 1
            If $Startrange <= $i_o And $count <= $Stoprange Then 
                $ret &= $rsplit[$i_o] & $insertstring
                Else
                $ret &= $rsplit[$i_o]
            EndIf
        Next
        $ret &= " "
    Next
    Return $ret
EndFunc
oÝ÷ Ùçbµ«­¢+Ø)Õ¹}MÑÉ¥¹5ձѥ%¹ÍÉÐÈ ÀÌØíÍÑÉ¥¹°ÀÌØí¥¹ÍÉÑÍÑÉ¥¹°ÀÌØíMÑÉÑɹôÀ°ÀÌØíMѽÁɹôÀ¤(%1½°ÀÌØíÉÐôÅÕ½ÐìÅÕ½Ðì°ÀÌØí½Õ¹ÐôÀìø±É¥¹YÉ¥±Ì(%%ÀÌØíMÑÉÑɹÐìÀÌØíMѽÁɹQ¡¸IÑÕɸ´Äìø
¡­¥¹ÍÑÉйÍѽÀɹ(%%ÀÌØíMѽÁɹôÀQ¡¸ÀÌØíMѽÁɹôMÑÉ¥¹1¸ ÀÌØíÍÑÉ¥¹¤ìøMÐÍѽÁɹѼµá¥µÕ´¡ÍÑÉ¥¹±¹¡Ð¤($ÀÌØí±MÁ±¥ÐôMÑÉ¥¹MÁ±¥Ð ÀÌØíÍÑÉ¥¹°ÅÕ½ÐìÅÕ½Ðì¤ìøÍÁ±¥ÑÑ¥¹±°Ý¡¥ÑÍÁÌ(%½ÈÀÌØí¤ôÄQ¼ÀÌØí±MÁ±¥ÑlÁtìøÍÑÉÑ¥¹±½½ÀѼѡݡ¥ÑÍÁÍÁ±¥Ð($$ÀÌØíÉÍÁ±¥ÐôMÑÉ¥¹MÁ±¥Ð ÀÌØí±MÁ±¥ÑlÀÌØí¥t°ÅÕ½ÐìÅÕ½Ðì¤ìøÍÁ±¥ÐÑ¡ÕÉɹÐݽÉ($%½ÈÀÌØí¥}¼ôÄQ¼ÀÌØíÉÍÁ±¥ÑlÁtìøÍÑÉÐͽ¹±½½ÀÁ±¥¸Ñ¡ÅÕ½Ðí¥¹ÍÉÑÍÑÉ¥¹ÅÕ½Ðì($$$ÀÌØí½Õ¹Ð¬ôÄìø͵±°½Õ¹Ð½ÈÑ¡ÍѽÁɹ($$%%ÀÌØíMÑÉÑɹ±ÐìôÀÌØí¥}¼¹ÀÌØí½Õ¹Ð±ÐìôÀÌØíMѽÁɹQ¡¸($$$$ÀÌØíÉеÀìôÀÌØíÉÍÁ±¥ÑlÀÌØí¥}½tµÀìÀÌØí¥¹ÍÉÑÍÑÉ¥¹ìø¥ÍÑÉÐɹ¥ÌÉ¡°ÍÐÑ¡¥¹ÍÉÐÍÑÉ¥¹($$$%±Íìø¥¹½Ð¸¸¸($$$$ÀÌØíÉеÀìôÀÌØíÉÍÁ±¥ÑlÀÌØí¥}½tìøÁ±Ñ¡¹áÐÍÑÉ¥¹($$%¹%($%9áÐ($$ÀÌØíÉеÀìôÅÕ½ÐìÅÕ½ÐììøÍÐÝ¡¥ÑÍÁÌ¥¸(%9áÐ(%IÑÕɸÀÌØíÉÐìøÉÑÕɸձ°ÍÑÉ¥¹)¹Õ¹((

commented code.

Edited by Busti
My UDF's : Startet on : 06.06.2006_CaseSearchOrReplaceStr();~> Searches OR Replaces a String,;~> With or Without Casesensivity
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...