Jump to content

StringStripBetween()


gamerman2360
 Share

Recommended Posts

MsgBox(0, "Test", StringStripBetween("I really like pizza!", " r", "y ")); returns "I like pizza!"

Func StringStripBetween($string, $startstr, $endstr, $casesense = 0, $startocc = 1, $endocc = 1)
    If Not $startocc Or Not $endocc Then Return SetError(1)
    $start = StringInStr($string, $startstr, $casesense, $startocc)
    $end = StringInStr($string, $endstr, $casesense, $endocc)
    If Not $start Or Not $end Then Return SetError(2)
    $right = StringRight($string, StringLen($string) - $end)
    Return StringLeft($string, $start - 1) & $right
EndFunc

Link to comment
Share on other sites

  • Moderators

MsgBox(0, "Test", StringStripBetween("I really like pizza!", " r", "y ")); returns "I like pizza!"

Func StringStripBetween($string, $startstr, $endstr, $casesense = 0, $startocc = 1, $endocc = 1)
    If Not $startocc Or Not $endocc Then Return SetError(1)
    $start = StringInStr($string, $startstr, $casesense, $startocc)
    $end = StringInStr($string, $endstr, $casesense, $endocc)
    If Not $start Or Not $end Then Return SetError(2)
    $right = StringRight($string, StringLen($string) - $end)
    Return StringLeft($string, $start - 1) & $right
EndFunc
I was looking at this and wondering what if there were more than 2 instances in one line what would happen, and as I thought, it only catches the first one. I did this one for you, I don't play with RegExp much at all, but it seemed to work... maybe you or someone else can make it better:
MsgBox(0, "Test", EliminateBetween("I really like pizza, I really like extra cheese too!", " R", "y ")); returns "I like pizza, I like extra cheese too!"

Func EliminateBetween($s_String, $s_Start, $e_End, $c_CaseSense = 0, $s_Space = '')
    If StringInStr($s_Start, ' ') Or StringInStr($e_End, ' ') Then $s_Space = ' '
    If StringInStr($s_String, $s_Start) And StringInStr($s_String, $e_End) Then 
        If $c_CaseSense = 0 Then
            $ReturnResult = StringRegExpReplace($s_String, '(?i:' & $s_Start & ')(?i:[^' & $e_End  & ']*'& $e_End & ')', $s_Space)
        Else
            $ReturnResult = StringRegExpReplace($s_String, '(' & $s_Start & ')([^' & $e_End  & ']*'& $e_End & ')', $s_Space)
        EndIf
        If @error = 0 And @extended Then Return StringStripWS($ReturnResult, 7)
    EndIf
EndFunc

Edit:

I noticed yours had case sensitive, mine was originally hard coded to be case sensitive, but I think the above fixes that, and gives the option... someone might want to proof and or fix it if it doesn't (if they feel like it I should say :o).

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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