Jump to content



Photo

_CaseSearchOrReplaceStr()


  • Please log in to reply
9 replies to this topic

#1 Busti

Busti

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 337 posts

Posted 06 June 2006 - 08:28 PM

hi , here my new udf, you can :

search for a string,
search for a string casesense,
replace a string,
replace a string casesense!

commentet nearly all of the code, hope ya like it :

udf :

AutoIt         
Func _CaseSearchOrReplaceStr( $String , $SearchString="" , $ReplaceString="", $Mode=1 ,$Count="x")     ;Mode's :     ;   ~ 1 = Search a String     ;   ~ 2 = Search a String Casesense     ;   ~ 3 = Replace a String     ;   ~ 4 = Replace a String Casesense     Local $u_String = $String     Local $s_String = $SearchString     Local $r_String = $ReplaceString     Local $u_Mode   = $Mode     Local $ReturnString = ""     Dim $LenOfFoundString = 0,$LenOfSearchString = StringLen($s_String),$FoundCount=0     If ( $u_String = "" ) Or ( StringLen($u_String) < 1 ) Or ( StringLen($s_String) < 1 )Then         SetError(1)         Return -1 ; -1 = error in the strings         ;Error in the Strings     EndIf     If $Count < 1 And $Count <> "x" Then         SetError(2)         Return -2     EndIf     If ($Mode = 1) Or ($Mode = 2) Then         ;~GetMode         If $Mode = 1 Then             ;~Split String             $u_String = StringSplit($u_String," ")             ;~Searchfor the Word             For $i_Search = 1 To $u_String[0]                 ;~if is in string then return the word count, eg.                 ;~hello im back                 ;~searching for im,he will return 2                 If StringInStr($u_String[$i_Search],$s_String,1) Then                     Return $i_Search                 EndIf             Next             ;~loop is done, nothing found               Return 0                    Else             ;~Split the String             $u_String = StringSplit($u_String," ")             For $i_Search = 1 To $u_String[0]                 ;~if is in string then return the word count, eg.                 ;~hello imback                 ;~searching for im,he will return 0, becouse                 ;~it was not found -> casesense !                 If StringInStr($u_String[$i_Search],$s_String,1) Then                     ;~Found String, Checking the Lengh of it.                     $LenOfFoundString = StringLen($u_String[$i_Search])                     If $LenOfFoundString <> $LenOfSearchString Then                         ;~Do Nothing..                         ;~Becouse its not our string we want!                     Else                         ;~Else Return number, as by Mode 1                         Return $i_Search                     EndIf                 EndIf             Next             ;~loop is done, nothing found               Return 0                            EndIf     ElseIf ($Mode = 3) Or ($Mode = 4) Then         If $Mode = 3 Then             ;~There is already a Function for this so...             ;~i will save some work :D             If StringInStr( $u_String, $s_String ) Then                 Return StringRegExpReplace( $u_String, $s_String, $r_String )             EndIf             Return 0         Else             ;~Split the String             $u_String = StringSplit($u_String," ")             If $Count = "x" Then $Count = $u_String[0]             For $i_Search = 1 To $u_String[0]                 ;~if the string is found he checks the lengh of the                 ;~a) searchstring                 ;~B) replacestring                 ;~if the lengh matches, he replaces the word,                 ;~and returns the complete string.                 If StringInStr($u_String[$i_Search],$s_String,1) Then                     ;~Found String, Checking the Lengh of it.                     $LenOfFoundString = StringLen($u_String[$i_Search])                     If $LenOfFoundString = $LenOfSearchString Then                         $FoundCount += 1                         $u_String[$i_Search] = $ReplaceString                         If ($FoundCount = $Count) Or ($i_Search == $u_String[0]) Then                             ;~Makes the full string again, and returns it!                             For $i_RemakeString = 1 To $u_String[0]                                 $ReturnString &= " " & $u_String[$i_RemakeString]                             Next                             Return $ReturnString                         EndIf                         Return 0                     Else                         ;~Do Nothing..                         ;~Becouse its not our string we want!                     EndIf                 EndIf             Next             ;~loop is done, nothing found               Return 0         EndIf     EndIf     Return 0 EndFunc ƒo݊÷ ÙìZš™^jëhŠ×6 MsgBox(0,"",_CaseSearchOrReplaceStr( "Hello im ThereBO" , "BO" , -1, 1));search for a string MsgBox(0,"",_CaseSearchOrReplaceStr( "Hello im ThereBO" , "BO" , -1, 2));search casesense for a string MsgBox(0,"",_CaseSearchOrReplaceStr( "Hello im ThereBO" , "BO" , "omg dont lie!", 3));replace a string MsgBox(0,"",_CaseSearchOrReplaceStr( "Hello im ThereBO" , "BO" , "omg dont lie!", 4));replace a string casesense



post some comments :D


hope the name is okay @valik^^

Edited by Busti, 06 June 2006 - 08:34 PM.

My UDF's : Startet on : 06.06.2006_CaseSearchOrReplaceStr();~> Searches OR Replaces a String,;~> With or Without Casesensivity





#2 w0uter

w0uter

    resreveR nA

  • Active Members
  • PipPipPipPipPipPip
  • 2,262 posts

Posted 06 June 2006 - 08:54 PM

why do you do

Func _CaseSearchOrReplaceStr( $String , $SearchString="" , $ReplaceString="", $Mode=1 ,$Count="x")     Local $u_String = $String     Local $s_String = $SearchString     Local $r_String = $ReplaceString     Local $u_Mode   = $Modeƒo݊÷ Ú)ìµæ¡ö®¶ˆ­sdgVæ266U6V&6„÷%&WÆ6U7G"‚b33c·Uõ7G&–ærÂb33c·5õ7G&–æsÒgV÷C²gV÷C²Âb33c·%õ7G&–æsÒgV÷C²gV÷C²Âb33c·UôÖöFSÓÂb33c´6÷VçCÒgV÷C·‚gV÷C²ƒo݊÷ Ù©l¡«­¢+؀€€€€€€€€€€€€€€€€€€±Í”(€€€€€€€€€€€€€€€€€€€€€€€€íù¼9½Ñ¡¥¹œ¸¸(€€€€€€€€€€€€€€€€€€€€€€€€íù  •½Õ͔¥Ñ́¹½Ð½ÕȁÍÑÉ¥¹œÝ”Ý…¹Ð˜ŒÌ̃o݊÷ Ú+n+n±é^²Ë™è(zÇè­«­¢+؀€€€€€€€€€€€€€€€€€€%˜€˜ŒÀÌØí1•¹=™½Õ¹‘MÑÉ¥¹œ€™±Ð왝Ð쀘ŒÀÌØí1•¹=™M•…ɍ¡MÑÉ¥¹œQ¡•¸(€€€€€€€€€€€€€€€€€€€€€€€€íù¼9½Ñ¡¥¹œ¸¸(€€€€€€€€€€€€€€€€€€€€€€€€íù  •½Õ͔¥Ñ́¹½Ð½ÕȁÍÑÉ¥¹œÝ”Ý…¹Ð˜ŒÌÌì(€€€€€€€€€€€€€€€€€€€±Í”(€€€€€€€€€€€€€€€€€€€€€€€€íù±Í”I•ÑÕɸ¹Õµ‰•Ȱ…́‰ä5½‘”€Ä(€€€€€€€€€€€€€€€€€€€€€€€I•ÑÕɸ€˜ŒÀÌØí¥}M•…ɍ (€€€€€€€€€€€€€€€€€€€¹‘%˜ƒo݊÷ Ûœ†ÈhºW[zj+zX¤y«­¢+Ù%˜€˜ŒÀÌØí1•¹=™½Õ¹‘MÑÉ¥¹œ€ô€˜ŒÀÌØí1•¹=™M•…ɍ¡MÑÉ¥¹œQ¡•¸I•ÑÕɸ€˜ŒÀÌØí¥}M•…ɍ


and for the last bash:

why do you first decide between 1-2 & 3-4 and then 1-2 -> 1 & 2 and 3-4 -> 3 & 4
why not just make it one Switch statement ? (kinda blurry explanation i know)

other then that it looks pretty decent.

Edited by w0uter, 06 June 2006 - 09:06 PM.

My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

#3 Busti

Busti

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 337 posts

Posted 06 June 2006 - 08:57 PM

becouse if i want to change this variables :

$String , $SearchString="" , $ReplaceString="", $Mode=1 ,$Count="x"

i dont need to change them in the whole script :D

rather useless but it saves some work,

if i found a better way or forgot something :D

example : i forgot the $replace array, i only needet to change my old name of $searchorreplace to $searchstring and 1 to $replace string,

and i only needet to change 1 var insteat of 10


€dit:

too your new edit,
i wantet to make the function so tidy, that everyone can read it, i had some problems, where i startet to read it how you made


€dit :

thx, did you test it ?

Edited by Busti, 06 June 2006 - 09:01 PM.

My UDF's : Startet on : 06.06.2006_CaseSearchOrReplaceStr();~> Searches OR Replaces a String,;~> With or Without Casesensivity

#4 w0uter

w0uter

    resreveR nA

  • Active Members
  • PipPipPipPipPipPip
  • 2,262 posts

Posted 06 June 2006 - 09:12 PM

IMO preformence > readability :D

sure edited that post alot eh :D
i always do that but noone reads it as fast as you do. ;)

and no i did not test it since im booted from a live cd. :P
My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

#5 Busti

Busti

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 337 posts

Posted 06 June 2006 - 10:15 PM

i always do that but noone reads it as fast as you do. :D



im always waiting, until ~10min or all leaved reading my topic :D

hmm switch, alright ! Forgot to make it but looks nice with if :P

dunno im always catching the first thing that is in my head ;)

pls test it if you can :evil: or want a exe ?


i like much posts in my topic, thats why im looking in it^^

Edited by Busti, 06 June 2006 - 10:16 PM.

My UDF's : Startet on : 06.06.2006_CaseSearchOrReplaceStr();~> Searches OR Replaces a String,;~> With or Without Casesensivity

#6 Busti

Busti

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 337 posts

Posted 07 June 2006 - 04:35 PM

*waiting4forposts:(*
My UDF's : Startet on : 06.06.2006_CaseSearchOrReplaceStr();~> Searches OR Replaces a String,;~> With or Without Casesensivity

#7 Busti

Busti

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 337 posts

Posted 07 June 2006 - 05:08 PM

but its not casesense !
My UDF's : Startet on : 06.06.2006_CaseSearchOrReplaceStr();~> Searches OR Replaces a String,;~> With or Without Casesensivity

#8 JSThePatriot

JSThePatriot

    carpe diem. vita brevis.

  • MVPs
  • 3,686 posts

Posted 07 June 2006 - 05:10 PM

but its not casesense !

If you look at the end of his string of code you will see where it says [casesense])

That to me indicates the case sensitivity.

JS
AutoIt LinksFile-String Hash Plugin Updated! 04-02-08ComputerGetInfo UDF's Updated! 11-23-06External LinksVortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

#9 SmOke_N

SmOke_N

    It's not what you know ... It's what you can prove!

  • Moderators
  • 15,729 posts

Posted 07 June 2006 - 06:44 PM

I do that all the time!! Make a damn UDF that was already made :D because I didn't read every function option.

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.


#10 Busti

Busti

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 337 posts

Posted 07 June 2006 - 09:37 PM

@js it wont work, dont know why, or it doesntwork for me, if i search for bo and my string is mybo then it replaces the bo at mybo, but he should not do that!
My UDF's : Startet on : 06.06.2006_CaseSearchOrReplaceStr();~> Searches OR Replaces a String,;~> With or Without Casesensivity




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users