Digital Workshop

Welcome to the Digital Workshop Message Boards
It is currently December 22nd, 2024, 7:40 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Replace part of string
PostPosted: October 31st, 2005, 1:42 pm 
Offline

Joined: January 19th, 2005, 5:15 pm
Posts: 83
Location: Netherlands
I need to replace a part of a string.

Because of database functionality, i need to replace all ' in the variables to \'. I tried the next script:

variable.replace("'", "\'");

but that is a javascript function, apparently not supported by Opus. Is there a easy way of doing this?


Top
 Profile  
 
 Post subject: use split and join
PostPosted: October 31st, 2005, 7:21 pm 
Offline

Joined: November 11th, 2004, 4:05 am
Posts: 636
Location: Christchurch, NZ
Opusscript is mainly limited to those functions found in Javascript 1.1, but there are usually workarounds for functions in later versions of Javascript.
You can achieve the desired result with the split and join string functions.

str = "A'B'C'D";

str2 = str.split("'").join("\\'");

this will give you an output of str2 = A\'B\'C\'D

cheers

Paul


Top
 Profile  
 
 Post subject: "find and replace" function
PostPosted: November 1st, 2005, 2:13 am 
Offline

Joined: November 11th, 2004, 4:05 am
Posts: 636
Location: Christchurch, NZ
if you prefer it packaged as a general "find and replace" function, use:

function editString(str,findThis,replaceWith){
return newString = str.split(findThis).join(replaceWith);
}

//usage::

string1="I am the black sheep of the family"

string2 = editString(string1,"black sheep","dark horse")

string2 will now contain the phrase "I am the dark horse of the family"

Paul


Top
 Profile  
 
 Post subject:
PostPosted: November 1st, 2005, 10:12 am 
Offline

Joined: January 19th, 2005, 5:15 pm
Posts: 83
Location: Netherlands
Hi Paul,


Thanks for your reply. That will do the trick. For the moment i solved it with a different usage of " and 's, but that wasn't my preffered method.
This will come in handy. Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group