cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
The Discovery Summit 2025 Call for Content is open! Submit an abstract today to present at our premier analytics conference.
Choose Language Hide Translation Bar
View Original Published Thread

Removing the end of a string of variable length

DanAlexander
Level III

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

5 REPLIES 5
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.

 

Crew
Level I

Re: Removing the end of a string of variable length

// I had a similar problem with removing the file name from the file path 
 
Names Default To Here( 1 );
dt = Current Data Table();
 
current_file = dt << Get Path();
current_directory = Word([1 -2], current_file, "\");
// return the full string ([1 -1]), except for the last word ([1 -2])
 
data = Pick File(
"Select Data Files",
current_directory,
{"Excel|csv","JMP Files|jmp;jsl;jrn","All Files|*"},
1,
0,
"", multiple
);