Jump to content

StringReplace


Go to solution Solved by Musashi,

Recommended Posts

Hi

I have like 80 different strings that i need to map out and remove them as below.

$test2 = StringReplace($test2, 'word',"")
$test2 = StringReplace($test2, '/>',"")

But can i make it some how different, instead making 80 new lines with same method?
Is it possible somehing like this
 

$Strings = ("word", "/>","more words")
 $test2 = StringReplace($test2, $Strings, "")

Tested, didn't work lol

Link to comment
Share on other sites

15 minutes ago, Subz said:

Try StringRegExpReplace you can test on your patterns here: RegExr: Learn, Build, & Test RegEx

Cant use that. i have about 80 sentences with all have words  "nice sun", "Slow Motion", "Don't come today, in this place" in it.

Some samples
span class="a-size-nice sun-success"> br div cla
s="a-row">span class="a-color-state">nice sun>div>div>spa
. </span>  
nice sun                    <br     Don't come today, in this place  <br
So i use fallowing:
Global $sResponse = StringInStr($sData, "nice sun")
Global $test2 = StringMid($sData, $sResponse-50, "100")

Global $sResponse = StringInStr($sData, "nice sun")
Global $test2 = StringMid($sData, $sResponse-50, "50")
$test2 = StringReplace($test2, 's="a-row">span class="a-color-state">',"")

I need that 50 character $sResponse due different sentences and more results that i need from there.
But i do not need all that other stuff

Edited by dersiniar
Link to comment
Share on other sites

30 minutes ago, dersiniar said:

But can i make it some how different, instead making 80 new lines with same method?

Put the strings in an Array and loop through the array with a single StringReplace().

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

12 minutes ago, Subz said:

Can you post the example text correctly, the example above is missing closing/opening tags is badly formatted.

that's how they are, Those tags have to be missing cos its just a part randomly received from html. I just need to map out text what i need from it

Link to comment
Share on other sites

41 minutes ago, Musashi said:

Put the strings in an Array and loop through the array with a single StringReplace().

I like the idea, But i do not know lol how to make it. 

$Strings = "1", "2", "3"
For $i = 1 to Strings              ;........;that part is always complicated for me how to make
$String = $Strings, $i
Next
$test2 = StringReplace($test2, '$String',"")

 

Edited by dersiniar
Link to comment
Share on other sites

  • Solution
2 hours ago, dersiniar said:

I like the idea, But i do not know lol how to make it. 

Example :

Local $aWords[4] = [3, "word", "/>", "more words"]
Local $sString   = "In the beginning was the word." & @CRLF & _
                   "After the word, there were more words, e.g. />" & @CRLF & _
                   "Now they are gone !"
ConsoleWrite("Before : " & @CRLF & $sString & @CRLF & @CRLF)
For $i = 1 To $aWords[0]
    $sString = StringReplace($sString, $aWords[$i], "")
    ; or use :
    ; $sString = StringRegExpReplace($sString, "(?m)\Q" & $aWords[$i] & "\E", "")
Next
ConsoleWrite("After  : " & @CRLF & $sString & @CRLF & @CRLF)

The problem is :
If "word" was replaced by "" in one pass, StringReplace will not find the search string "more words" in a following pass, because "word" has already been replaced.

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

17 hours ago, Musashi said:

Example :

Local $aWords[4] = [3, "word", "/>", "more words"]
Local $sString   = "In the beginning was the word." & @CRLF & _
                   "After the word, there were more words, e.g. />" & @CRLF & _
                   "Now they are gone !"
ConsoleWrite("Before : " & @CRLF & $sString & @CRLF & @CRLF)
For $i = 1 To $aWords[0]
    $sString = StringReplace($sString, $aWords[$i], "")
    ; or use :
    ; $sString = StringRegExpReplace($sString, "(?m)\Q" & $aWords[$i] & "\E", "")
Next
ConsoleWrite("After  : " & @CRLF & $sString & @CRLF & @CRLF)

The problem is :
If "word" was replaced by "" in one pass, StringReplace will not find the search string "more words" in a following pass, because "word" has already been replaced.

Thank you man, ill tray  to add it into my code asap. Looks promising :D

Link to comment
Share on other sites

As mentioned above you can use StringRegExpReplace example:

Local $aWords[4] = [3, "word", "/>", "more words"]
Local $sString   = "In the beginning was the word." & @CRLF & _
                   "After the word, there were more words, e.g. />" & @CRLF & _
                   "Now they are gone !"
ConsoleWrite("Before : " & @CRLF & $sString & @CRLF & @CRLF)
$sString = StringRegExpReplace($sString, $aWords[1] & '|' & $aWords[2] & '|' & $aWords[3] , "")
ConsoleWrite("After  : " & @CRLF & $sString & @CRLF & @CRLF)

 

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