TI String Replace Function

Post Reply
macky29
Posts: 6
Joined: Fri Oct 24, 2008 9:20 am

TI String Replace Function

Post by macky29 »

Hi guys,

Is there a String Replace Function in TI?

I'm encountering a problem when I Write Back to an Oracle Table because some string value that I need to write has single qoute(') character. I plan to replace it with double qoute ('') during run time so that the Oracle Table will accept it.

Tnx,
Macky
Alan Kirk
Site Admin
Posts: 6643
Joined: Sun May 11, 2008 2:30 am
OLAP Product: TM1
Version: PA2.0.9.18 Classic NO PAW!
Excel Version: 2013 and Office 365
Location: Sydney, Australia
Contact:

Re: TI String Replace Function

Post by Alan Kirk »

macky29 wrote:Hi guys,
Is there a String Replace Function in TI?
I'm encountering a problem when I Write Back to an Oracle Table because some string value that I need to write has single qoute(') character. I plan to replace it with double qoute ('') during run time so that the Oracle Table will accept it.
Short answer no (though it's an odd oversight), longer answer I wouldn't use it with quotes anyway since those can be mind bending. Whenever I have to use quote characters as literal strings I always use the Char() function.

You can improvise a replace function by using the Scan() and Subst() functions; basically, every time you find the character you're looking for, take the part of the string before it (if any), whack in a double quote character, then add on the bit of the string after it. This tested out OK:

Code: Select all

s_StringWithQuotes  = char(39) | 'This' | char(39) | 'is'  | char(39) | 'such' | char(39) | 'a'  | char(39) | 'string' ;

AsciiOutput ( 'F:\Temp\SWSQ.txt', s_StringWithQuotes );

l_LocNext = Scan ( Char(39), s_StringWithQuotes  );

While ( l_LocNext > 0 );

    s_StringWithQuotes  = Subst ( s_StringWithQuotes , 1, l_LocNext - 1) | Char(34) | Subst ( s_StringWithQuotes , l_LocNext+1, Long (s_StringWithQuotes ) - l_LocNext );

     l_LocNext = Scan ( Char(39), s_StringWithQuotes  );

End;

AsciiOutput ( 'F:\Temp\SWDQ.txt', s_StringWithQuotes );
"To them, equipment failure is terrifying. To me, it’s 'Tuesday.' "
-----------
Before posting, please check the documentation, the FAQ, the Search function and FOR THE LOVE OF GLUB the Request Guidelines.
David Usherwood
Site Admin
Posts: 1458
Joined: Wed May 28, 2008 9:09 am

Re: TI String Replace Function

Post by David Usherwood »

Assuming you are communicating directly to Oracle via ODBCOUTPUT, you could use the Oracle PL/SQL Translate function in the SQL string you are sending.
macky29
Posts: 6
Joined: Fri Oct 24, 2008 9:20 am

Re: TI String Replace Function

Post by macky29 »

Tnx guys, I already considered the solutions you gave. just hoping to make my life easier if I can do it with a TI Function.
Alan Kirk
Site Admin
Posts: 6643
Joined: Sun May 11, 2008 2:30 am
OLAP Product: TM1
Version: PA2.0.9.18 Classic NO PAW!
Excel Version: 2013 and Office 365
Location: Sydney, Australia
Contact:

Re: TI String Replace Function

Post by Alan Kirk »

macky29 wrote:Tnx guys, I already considered the solutions you gave. just hoping to make my life easier if I can do it with a TI Function.
Ah. Clearly you haven't seen the sign over the doorway leading to the TI development team's office: "A little (sometimes a lot) of suffering is good for the soul".

Were it otherwise we'd have the type of function that you're after, find and replace in the editor, debugging tools, For loops, function names which are consistent across all aspects of the interface, ItemSkip and ItemReject would throw a compilation error if used in the wrong tabs, chore scheduling would be in local time... oh my is that the time? I could spend all night typing these... :evil:
"To them, equipment failure is terrifying. To me, it’s 'Tuesday.' "
-----------
Before posting, please check the documentation, the FAQ, the Search function and FOR THE LOVE OF GLUB the Request Guidelines.
Mems
Posts: 58
Joined: Thu Apr 14, 2011 12:27 pm
OLAP Product: TM1
Version: v10.2.2
Excel Version: 2010
Location: South Africa

Re: TI String Replace Function

Post by Mems »

Thanks gents... Not sure were I would be without this
MEMS
User avatar
macsir
MVP
Posts: 785
Joined: Wed May 30, 2012 6:50 am
OLAP Product: TM1
Version: PAL 2.0.9
Excel Version: Office 365
Contact:

Re: TI String Replace Function

Post by macsir »

Just want to quickly share my code, sample code for replacing single quote with space

Code: Select all

vTemp = '';
vChar = '';
k=1;
WHILE( k <= LONG(vOriginalString) );
vChar = SUBST(vOriginalString, k, 1);
  IF(vChar @= '''');
    vChar = ' ';
  ENDIF;
vTemp = vTemp | vChar;
k = k + 1;

END;
vOriginalString = vTemp;
In TM1,the answer is always yes though sometimes with a but....
http://tm1sir.blogspot.com.au/
pandinus
Posts: 78
Joined: Tue Mar 18, 2014 8:02 am
OLAP Product: TM1, Cognos Express
Version: 10.2.2
Excel Version: 2013

Re: TI String Replace Function

Post by pandinus »

For some more elaborate and dynamic find/replace functions in TI, I was forced to do some cool stuff a while ago: regular expressions.

While not being exactly regular expressions as per definition, they offer a lot of flexibility and have become my golden hammer in many situations.

It works like this:
  • *Define your original string
    *Define your filter expression
    *Define any replacement rules.

Example 1:
Here we do a simple replace. Replace any space characters with underscores.

Code: Select all

vString = 'TM1 is great!';
vExpression = ' ';
vReplacement = '_';
Becomes

Code: Select all

TM1_is_great!

Example 2:
Here we do not only want to replace, we want to add to a matched string. For this we use the $& to refer to the matched string.

Code: Select all

vString = 'CellPutN(vValue, vCubename, cm_dim1, cm_dim2, cm_dim3)';
vExpression = 'CellPut?(*,*,';
vReplacement = '$& cm_extra, ';
Becomes

Code: Select all

CellPutN(vValue, vCubename, cm_extra, cm_dim1, cm_dim2, cm_dim3)

Example 3:
When not replacing stuff, sometimes we just want just look for stuff in strings. This will give you the FIRST occurrence of the match based on the expression in the string, and the length of the matched string.

Code: Select all

vString = 'TM1 is relatively ok!';
vExpression = 'el';
Becomes

Code: Select all

"TM1 is relatively ok!","el","9","2"
When using wildcards:

Code: Select all

vString = 'TM1 is relatively ok!';
vExpression = '1*!';
Becomes

Code: Select all

"TM1 is relatively ok!","1*!","3","19"

I have attached the required .pro files to this post for your entertainment.
You run the Regex_test process for testing.
The logic is in the Sys_regex_replace and Sys_regex_find processes.
Attachments
Regex_test.pro
(3.07 KiB) Downloaded 894 times
Sys_Regex_find.pro
(5.97 KiB) Downloaded 854 times
Sys_Regex_replace.pro
(8.32 KiB) Downloaded 1234 times
Wim Gielis
MVP
Posts: 3223
Joined: Mon Dec 29, 2008 6:26 pm
OLAP Product: TM1, Jedox
Version: PAL 2.1.5
Excel Version: Microsoft 365
Location: Brussels, Belgium
Contact:

Re: TI String Replace Function

Post by Wim Gielis »

Seems to me that the post above is a very good candidate for the "TM1 Useful code, tips and tricks" subforum.
Best regards,

Wim Gielis

IBM Champion 2024-2025
Excel Most Valuable Professional, 2011-2014
https://www.wimgielis.com ==> 121 TM1 articles and a lot of custom code
Newest blog article: Deleting elements quickly
lotsaram
MVP
Posts: 3698
Joined: Fri Mar 13, 2009 11:14 am
OLAP Product: TableManager1
Version: PA 2.0.x
Excel Version: Office 365
Location: Switzerland

Re: TI String Replace Function

Post by lotsaram »

Wim Gielis wrote:Seems to me that the post above is a very good candidate for the "TM1 Useful code, tips and tricks" subforum.
I agree. Bookmarked :D
Please place all requests for help in a public thread. I will not answer PMs requesting assistance.
Post Reply