Jump to content

_StringRemIns()


erifash
 Share

Recommended Posts

This function is named _StringRemIns() because it stands for String Remove and Insert. What it does is takes all the characters in the position range you specify and replaces them with a different string (or removes the characters if you specify a blank string).

Syntax:

$sString = String to work with
$iPos1 = Position of the first character (beginning of string is 0)
$iPos2 = Position of the second character
$sInsert = String to replace all the characters between the first and second character positions (default is blank)

Here is the code (with example):

$s = "aaaabbbcc"
$t = "a=1,b=2,c=3"

MsgBox(0, "string #1", _StringRemIns($s, 0, 0))
MsgBox(0, "replace a", _StringRemIns($s, 0, 4, "{replaced a's}"))
MsgBox(0, "remove a", _StringRemIns($s, 0, 4))
MsgBox(0, "replace b", _StringRemIns($s, 4, 7, "{replaced b's}"))
MsgBox(0, "remove b", _StringRemIns($s, 4, 7))
MsgBox(0, "replace c", _StringRemIns($s, 7, 9, "{replaced c's}"))
MsgBox(0, "remove c", _StringRemIns($s, 7, 9))
MsgBox(0, "append string", _StringRemIns($s, 9, 9, "{appended string}"))
MsgBox(0, "insert string", _StringRemIns($s, 0, 0, "{inserted string}"))
MsgBox(0, "replace string", _StringRemIns($s, 0, 9, "{replaced string}"))
MsgBox(0, "remove string", _StringRemIns($s, 0, 9))

MsgBox(0, "string #2", _StringGet($t, 0, 11))
MsgBox(0, "a equals", _StringGet($t, 2, 3))
MsgBox(0, "a plus three", _StringRemIns($t, 2, 3, _StringGet($t, 2, 3) + 3))
MsgBox(0, "b equals", _StringGet($t, 6, 7))
MsgBox(0, "b plus four", _StringRemIns($t, 6, 7, _StringGet($t, 6, 7) + 4))
MsgBox(0, "c equals", _StringGet($t, 10, 11))
MsgBox(0, "c plus five", _StringRemIns($t, 10, 11, _StringGet($t, 10, 11) + 5))

Func _StringRemIns($sString, $iPos1, $iPos2, $sInsert = "")
    If $iPos1 < 0 or $iPos1 > StringLen($sString) or $iPos2 < 0 or $iPos2 > StringLen($sString) or not IsInt($iPos1) or not IsInt($iPos2) or $iPos1 > $iPos2 Then Return 0
    Return StringTrimRight($sString, StringLen($sString) - $iPos1) & $sInsert & StringTrimLeft($sString, $iPos2)
EndFunc

Func _StringGet($sStri, $iPosi1, $iPosi2)
    If $iPosi1 < 0 or $iPosi1 > StringLen($sStri) or $iPosi2 < 0 or $iPosi2 > StringLen($sStri) or not IsInt($iPosi1) or not IsInt($iPosi2) or $iPosi1 > $iPosi2 Then Return 0
    Return StringTrimLeft(StringTrimRight($sStri, StringLen($sStri) - $iPosi2), $iPosi1)
EndFunc

Questions and comments are greatly appreciated!

EDIT: Fixed minor bugs and added a new function, _StringGet() (see posts below)

Edited by erifash
Link to comment
Share on other sites

Added extra error checking.

Anyone that bothered to test this out, can you please tell me what you think?

This is a great function and I have never seen anything like it on the forums at all.

Link to comment
Share on other sites

Added extra error checking.

Anyone that bothered to test this out, can you please tell me what you think?

This is a great function and I have never seen anything like it on the forums at all.

<{POST_SNAPBACK}>

Tested and all works great.. It is a nice use of multiple string functions in one script.

Cheers.. :)

Link to comment
Share on other sites

Thanks busysignal! I feel that I need to list all the things this function can do:

1. Insert text between characters

_StringRemIns("heo", 2, 2, "ll") = "hello"

2. Replace text in character positions

_StringRemIns("text", 0, 1, "n") = "next"

3. Remove text in character positions

_StringRemIns("te764653st", 2, 8) = "test"

4. Append text

_StringRemIns("hi", 2, 2, " people") = "hi people"

That is pretty useful! :)

Edited by erifash
Link to comment
Share on other sites

I just created a new function to be used with _StringRemIns(). It's called _StringGet() and it will return the characters, in a string that you give it, between two character positions. I have updated the example and code above! :)

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...