cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Submit your abstract to the call for content for Discovery Summit Americas by April 23. Selected abstracts will be presented at Discovery Summit, Oct. 21- 24.
Discovery is online this week, April 16 and 18. Join us for these exciting interactive sessions.
Choose Language Hide Translation Bar
DanAlexander
Level III

Removing the end of a string of variable length

Hello all,

 

I have a column :Protocol which contains strings of variable length in the format Character, Nominal. I've added some examples below. I am trying to remove the last 4 characters of the string from the column, and keep everything else. The last 4 characters are always one of: -001, -002, -003, or -004. I have tried to accomplish this using Word(), and Substr() but am unable to get it to work:

 

Protocol

AAA-BB-1234-19-001

AAAA-B-18-002

AA-BBB-123-18-003

AA-B-CC-1001-19-002

 

This is what I want the column to look like for those 4 rows:

 

Protocol

AAA-BB-1234-19

AAAA-B-18

AA-BBB-123-18

AA-B-CC-1001-19

 

I have tried to accomplish this using the Word() functions as follows:

For Each Row(
	:Protocol = Word(1, :Protocol, "00")
);
1 ACCEPTED SOLUTION

Accepted Solutions
David_Burnham
Super User (Alumni)

Re: Removing the end of a string of variable length

Something like Left(:Protocol, Length(:Protocol) - 4)
-Dave

View solution in original post

4 REPLIES 4
David_Burnham
Super User (Alumni)

Re: Removing the end of a string of variable length

Something like Left(:Protocol, Length(:Protocol) - 4)
-Dave
DanAlexander
Level III

Re: Removing the end of a string of variable length

Thank you, I tried your solution and it worked perfectly.
JLX
JLX
Level II

Re: Removing the end of a string of variable length

I agree with @David_Burnham , you can get results by directly using left, if you really wanna use word, maybe you can refer to this one below (I also attached data table in attachment):

Names Default To Here( 1 );
Clear Log();
dt = Current Data Table();
new_col = dt << New Column( "Trimmed Protocol",
	character,
	formula( Word( [1 -2], :Protocol, "-" ) )
);

Re: Removing the end of a string of variable length

I thought of using the Left() function, too. Here is my take:

 

Names Default to Here( 1 );

string = "AAA-BB-1234-19-001";

Left( string, Contains( string, "00") - 2 );

I recommend that you select Help > Scripting Index and select Functions > Character. There is a lot of built-in support for manipulating character strings.