| Blockland Forums > Modification Help |
| Replacing A String Up To A Point [Solved] |
| (1/1) |
| jes00:
Is there any way I can replace a string up until it reaches a ' ? |
| Port:
--- Code: ---function strReplaceUntil( %string, %search, %replace, %until ) { %pos = strPos( %string, %until ); if ( %pos < 0 ) { %pos = strLen( %string ); } return strReplace( getSubStr( %string, 0, %pos ), %search, %replace ) @ getSubStr( %string, %pos + strLen( %until ), strLen( %string ) ); } --- End code --- |
| Ipquarx:
Port's is good, assuming that you want to search for something up until a point. If you want to replace it no matter what's there, here is the code you should use: --- Code: ---function replaceUpTo(%String, %UpToChar, %Replacement, %AlwaysReplace) { //If %AlwaysReplace is false and %UpToChar is not found, then return. //Otherwise, return the replacement. %Pos = strPos(%String, %UpToChar); if(%Pos <= 0 && !%AlwaysReplace) return %String; else if(%Pos <= 0 && !!%AlwaysReplace) return %Replacement; return %Replacement @ getSubStr(%String, %Pos + 1, strLen(%String); } --- End code --- |
| jes00:
--- Quote from: Port on March 05, 2012, 06:42:57 AM --- --- Code: ---function strReplaceUntil( %string, %search, %replace, %until ) { %pos = strPos( %string, %until ); if ( %pos < 0 ) { %pos = strLen( %string ); } return strReplace( getSubStr( %string, 0, %pos ), %search, %replace ) @ getSubStr( %string, %pos + strLen( %until ), strLen( %string ) ); } --- End code --- --- End quote --- Is %until a number? EDIT: Talked to Port on his server, he solved it. |
| Navigation |
| Message Index |