Sign in to follow this  
Followers 0
RiCK_420_333

String Replacing ! ?

8 posts in this topic

Does anyone know why this code below is not working? Maybe I am not writing some correctly? Thanks!

Func ReplaceStrings($str)
    ; StringReplace ( "string", "searchstring" or start, "replacestring" [, count [, casesense]] )
    ; StringInStr ( "string", "substring" [, casesense [, occurrence]] )
    If StringInStr($str, " ",0 ,0) <> 0 Then $str = StringReplace($str, " ", "*",0 ,0)
    If StringInStr($str, "'",0 ,0) <> 0 Then $str = StringReplace($str, "'", "?",0 ,0)
    If StringInStr($str, "im",0 ,0) <> 0 Then $str = StringReplace($str, "im", "i?m",0 ,0)
    If StringInStr($str, "ill",0 ,0) <> 0 Then $str = StringReplace($str, "ill", "i?ll",0 ,0)
    If StringInStr($str, "ive",0 ,0) <> 0 Then $str = StringReplace($str, "ive", "i?ve",0 ,0)
    If StringInStr($str, "youre",0 ,0) <> 0 Then $str = StringReplace($str, "youre", "you?re",0 ,0)
    If StringInStr($str, "youll",0 ,0) <> 0 Then $str = StringReplace($str, "youll", "you?ll",0 ,0)
    If StringInStr($str, "youd",0 ,0) <> 0 Then $str = StringReplace($str, "youd", "you?d",0 ,0)
    If StringInStr($str, "youve",0 ,0) <> 0 Then $str = StringReplace($str, "youve", "you?ve",0 ,0)
    If StringInStr($str, "hes",0 ,0) <> 0 Then $str = StringReplace($str, "hes", "he?s",0 ,0)
    If StringInStr($str, "hell",0 ,0) <> 0 Then $str = StringReplace($str, "hell", "he?ll",0 ,0)
    If StringInStr($str, "hed",0 ,0) <> 0 Then $str = StringReplace($str, "hed", "he?d",0 ,0)
    If StringInStr($str, "shes",0 ,0) <> 0 Then $str = StringReplace($str, "shes", "she?s",0 ,0)
    If StringInStr($str, "shell",0 ,0) <> 0 Then $str = StringReplace($str, "shell", "she?ll",0 ,0)
    If StringInStr($str, "shed",0 ,0) <> 0 Then $str = StringReplace($str, "shed", "she?d",0 ,0)
    If StringInStr($str, "its",0 ,0) <> 0 Then $str = StringReplace($str, "its", "it?s",0 ,0)
    If StringInStr($str, "itll",0 ,0) <> 0 Then $str = StringReplace($str, "itll", "it?ll",0 ,0)
    If StringInStr($str, "itd",0 ,0) <> 0 Then $str = StringReplace($str, "itd", "it?d",0 ,0)
    If StringInStr($str, "were",0 ,0) <> 0 Then $str = StringReplace($str, "were", "we?er",0 ,0)
    If StringInStr($str, "well",0 ,0) <> 0 Then $str = StringReplace($str, "well", "we?ll",0 ,0)
    If StringInStr($str, "wed",0 ,0) <> 0 Then $str = StringReplace($str, "wed", "we?d",0 ,0)
    If StringInStr($str, "weve",0 ,0) <> 0 Then $str = StringReplace($str, "weve", "we?ve",0 ,0)
    If StringInStr($str, "theyre",0 ,0) <> 0 Then $str = StringReplace($str, "theyre", "they?re",0 ,0)
    If StringInStr($str, "theyll",0 ,0) <> 0 Then $str = StringReplace($str, "theyll", "they?ll",0 ,0)
    If StringInStr($str, "theyd",0 ,0) <> 0 Then $str = StringReplace($str, "theyd", "they?d",0 ,0)
    If StringInStr($str, "theyre",0 ,0) <> 0 Then $str = StringReplace($str, "theyve", "they?ve",0 ,0)
    If StringInStr($str, "theres",0 ,0) <> 0 Then $str = StringReplace($str, "theres", "there?s",0 ,0)
    If StringInStr($str, "therell",0 ,0) <> 0 Then $str = StringReplace($str, "therell", "there?ll",0 ,0)
    If StringInStr($str, "thered",0 ,0) <> 0 Then $str = StringReplace($str, "thered", "there?d",0 ,0)
    If StringInStr($str, "thats",0 ,0) <> 0 Then $str = StringReplace($str, "thats", "that?s",0 ,0)
    If StringInStr($str, "thatll",0 ,0) <> 0 Then $str = StringReplace($str, "thatll", "that?ll",0 ,0)
    If StringInStr($str, "thatd",0 ,0) <> 0 Then $str = StringReplace($str, "thatd", "that?d",0 ,0)
    If StringInStr($str, "arent",0 ,0) <> 0 Then $str = StringReplace($str, "arent", "are?nt",0 ,0)
    If StringInStr($str, "cant",0 ,0) <> 0 Then $str = StringReplace($str, "cant", "can?t",0 ,0)
    If StringInStr($str, "couldnt",0 ,0) <> 0 Then $str = StringReplace($str, "couldnt", "couldn?t",0 ,0)
    If StringInStr($str, "didnt",0 ,0) <> 0 Then $str = StringReplace($str, "didnt", "didn?t",0 ,0)
    If StringInStr($str, "doesnt",0 ,0) <> 0 Then $str = StringReplace($str, "doesnt", "doesn?t",0 ,0)
    If StringInStr($str, "dont",0 ,0) <> 0 Then $str = StringReplace($str, "dont", "don?t",0 ,0)
    If StringInStr($str, "hadnt",0 ,0) <> 0 Then $str = StringReplace($str, "hadnt", "hadn?t",0 ,0)
    If StringInStr($str, "hasnt",0 ,0) <> 0 Then $str = StringReplace($str, "hasnt", "hasn?t",0 ,0)
    If StringInStr($str, "isnt",0 ,0) <> 0 Then $str = StringReplace($str, "isnt", "isn?t",0 ,0)
    If StringInStr($str, "mustnt",0 ,0) <> 0 Then $str = StringReplace($str, "mustnt", "mustn?t",0 ,0)
    If StringInStr($str, "neednt",0 ,0) <> 0 Then $str = StringReplace($str, "neednt", "needn?t",0 ,0)
    If StringInStr($str, "shouldnt",0 ,0) <> 0 Then $str = StringReplace($str, "shouldnt", "shouldn?t",0 ,0)
    If StringInStr($str, "wasnt",0 ,0) <> 0 Then $str = StringReplace($str, "wasnt", "wasn?t",0 ,0)
    If StringInStr($str, "werent",0 ,0) <> 0 Then $str = StringReplace($str, "werent", "weren?t",0 ,0)
    If StringInStr($str, "wont",0 ,0) <> 0 Then $str = StringReplace($str, "wont", "won?t",0 ,0)
    If StringInStr($str, "wouldnt",0 ,0) <> 0 Then $str = StringReplace($str, "wouldnt", "wouldn?t",0 ,0)
    MsgBox(0, "str:", $str)
    Return $str
EndFunc

Share this post


Link to post
Share on other sites



When you specify 0 as the last parameter in StringInStr you tell it to check the string zero times, just set this to 1 or just leave it out :)


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

Hi,

may consider to put the stuff into an array to shorten your code.

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

When you specify 0 as the last parameter in StringInStr you tell it to check the string zero times, just set this to 1 or just leave it out :)

I have now tried it like this per your suggestion, however it's still not working.

Func ReplaceStrings($str)
    ; StringReplace ( "string", "searchstring" or start, "replacestring" [, count [, casesense]] )
    ; count     [optional] The number of times to replace the searchstring. 0 = all searchstrings will be replaced (default)
    ; casesense     [optional] Flag to indicate if the operations should be case sensitive. 0 = not case sensitive, using the user's locale (default)
    ; StringInStr ( "string", "substring" [, casesense [, occurrence]] )
    If StringInStr($str, " ",0 ,0) Then $str = StringReplace($str, " ", "*",0 ,0)
    If StringInStr($str, "'",0 ,0) Then $str = StringReplace($str, "'", "?",0 ,0)
    If StringInStr($str, "im",0 ,0) Then $str = StringReplace($str, "im", "i?m",0 ,0)
    If StringInStr($str, "ill",0 ,0) Then $str = StringReplace($str, "ill", "i?ll",0 ,0)
    If StringInStr($str, "ive",0 ,0) Then $str = StringReplace($str, "ive", "i?ve",0 ,0)
    If StringInStr($str, "youre",0 ,0) Then $str = StringReplace($str, "youre", "you?re",0 ,0)
    If StringInStr($str, "youll",0 ,0) Then $str = StringReplace($str, "youll", "you?ll",0 ,0)
    If StringInStr($str, "youd",0 ,0) Then $str = StringReplace($str, "youd", "you?d",0 ,0)
    If StringInStr($str, "youve",0 ,0) Then $str = StringReplace($str, "youve", "you?ve",0 ,0)
    If StringInStr($str, "hes",0 ,0) Then $str = StringReplace($str, "hes", "he?s",0 ,0)
    If StringInStr($str, "hell",0 ,0) Then $str = StringReplace($str, "hell", "he?ll",0 ,0)
    If StringInStr($str, "hed",0 ,0) Then $str = StringReplace($str, "hed", "he?d",0 ,0)
    If StringInStr($str, "shes",0 ,0) Then $str = StringReplace($str, "shes", "she?s",0 ,0)
    If StringInStr($str, "shell",0 ,0) Then $str = StringReplace($str, "shell", "she?ll",0 ,0)
    If StringInStr($str, "shed",0 ,0) Then $str = StringReplace($str, "shed", "she?d",0 ,0)
    If StringInStr($str, "its",0 ,0) Then $str = StringReplace($str, "its", "it?s",0 ,0)
    If StringInStr($str, "itll",0 ,0) Then $str = StringReplace($str, "itll", "it?ll",0 ,0)
    If StringInStr($str, "itd",0 ,0) Then $str = StringReplace($str, "itd", "it?d",0 ,0)
    If StringInStr($str, "were",0 ,0) Then $str = StringReplace($str, "were", "we?er",0 ,0)
    If StringInStr($str, "well",0 ,0) Then $str = StringReplace($str, "well", "we?ll",0 ,0)
    If StringInStr($str, "wed",0 ,0) Then $str = StringReplace($str, "wed", "we?d",0 ,0)
    If StringInStr($str, "weve",0 ,0) Then $str = StringReplace($str, "weve", "we?ve",0 ,0)
    If StringInStr($str, "theyre",0 ,0) Then $str = StringReplace($str, "theyre", "they?re",0 ,0)
    If StringInStr($str, "theyll",0 ,0) Then $str = StringReplace($str, "theyll", "they?ll",0 ,0)
    If StringInStr($str, "theyd",0 ,0) Then $str = StringReplace($str, "theyd", "they?d",0 ,0)
    If StringInStr($str, "theyre",0 ,0) Then $str = StringReplace($str, "theyve", "they?ve",0 ,0)
    If StringInStr($str, "theres",0 ,0) Then $str = StringReplace($str, "theres", "there?s",0 ,0)
    If StringInStr($str, "therell",0 ,0) Then $str = StringReplace($str, "therell", "there?ll",0 ,0)
    If StringInStr($str, "thered",0 ,0) Then $str = StringReplace($str, "thered", "there?d",0 ,0)
    If StringInStr($str, "thats",0 ,0) Then $str = StringReplace($str, "thats", "that?s",0 ,0)
    If StringInStr($str, "thatll",0 ,0) Then $str = StringReplace($str, "thatll", "that?ll",0 ,0)
    If StringInStr($str, "thatd",0 ,0) Then $str = StringReplace($str, "thatd", "that?d",0 ,0)
    If StringInStr($str, "arent",0 ,0) Then $str = StringReplace($str, "arent", "are?nt",0 ,0)
    If StringInStr($str, "cant",0 ,0) Then $str = StringReplace($str, "cant", "can?t",0 ,0)
    If StringInStr($str, "couldnt",0 ,0) Then $str = StringReplace($str, "couldnt", "couldn?t",0 ,0)
    If StringInStr($str, "didnt",0 ,0) Then $str = StringReplace($str, "didnt", "didn?t",0 ,0)
    If StringInStr($str, "doesnt",0 ,0) Then $str = StringReplace($str, "doesnt", "doesn?t",0 ,0)
    If StringInStr($str, "dont",0 ,0) Then $str = StringReplace($str, "dont", "don?t",0 ,0)
    If StringInStr($str, "hadnt",0 ,0) Then $str = StringReplace($str, "hadnt", "hadn?t",0 ,0)
    If StringInStr($str, "hasnt",0 ,0) Then $str = StringReplace($str, "hasnt", "hasn?t",0 ,0)
    If StringInStr($str, "isnt",0 ,0) Then $str = StringReplace($str, "isnt", "isn?t",0 ,0)
    If StringInStr($str, "mustnt",0 ,0) Then $str = StringReplace($str, "mustnt", "mustn?t",0 ,0)
    If StringInStr($str, "neednt",0 ,0) Then $str = StringReplace($str, "neednt", "needn?t",0 ,0)
    If StringInStr($str, "shouldnt",0 ,0) Then $str = StringReplace($str, "shouldnt", "shouldn?t",0 ,0)
    If StringInStr($str, "wasnt",0 ,0) Then $str = StringReplace($str, "wasnt", "wasn?t",0 ,0)
    If StringInStr($str, "werent",0 ,0) Then $str = StringReplace($str, "werent", "weren?t",0 ,0)
    If StringInStr($str, "wont",0 ,0) Then $str = StringReplace($str, "wont", "won?t",0 ,0)
    If StringInStr($str, "wouldnt",0 ,0) Then $str = StringReplace($str, "wouldnt", "wouldn?t",0 ,0)
    MsgBox(0, "str:", $str)
    Return $str
EndFunc

Share this post


Link to post
Share on other sites

Hi,

may consider to put the stuff into an array to shorten your code.

Mega

Can you elaborate on that or show me a short example? Thanks.


Share this post


Link to post
Share on other sites

I have now tried it like this per your suggestion, however it's still not working.

Are you saying this ain't working?

MsgBox(0,"",ReplaceStrings("Hello there is no whitespaces here!"))

Func ReplaceStrings($str)
If StringInStr($str, " ",0 ,1) Then $str = StringReplace($str, " ", "*",0 ,0)
;....
Return $str
EndFunc

Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

But as Xenobiologist said, an array is much better, I actually made the function for you :)

Dim $array[4][2]=[[" ","*"],["'","?"],["im","i?m"],["ill","i?ll"]] ; Extend as you want

MsgBox(0,"",_ReplaceStrings("Hello i'm here",$array))

Func _ReplaceStrings($string,$farray)
    For $i=0 To Ubound($farray,1)-1
        $string=StringReplace($string,$farray[$i][0],$farray[$i][1])
    Next
    Return $string
EndFunc

:(

Edited by monoceres

Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

monoceres is on the right track. There is really no point in even using the StringInStr function. From a performance standpoint, it is only increasing the execution time of your function because the original string must be fully searched once if the substring is not found, and more than once if the substring is found.

Edited by zorphnog

Share this post


Link to post
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
Sign in to follow this  
Followers 0