cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
jmpbeginner
Level III

remove part of a string (contingent upon number of periods within that string)?

I have a column with strings of varying length.  All strings have 1 semicolon separating the first and last part of the string.

some strings with no 'periods':

_______________;______

some strings with 1 period

______.______;_______

some strings with 2 periods

___._______._____;_________

I want to create a new column and populate it with the string of interest with the following conditions:

  • IF a string contains exactly 2 periods, I want to remove everything from the second period up to the semicolon
    • example:  "before1st.after1st.after2nd;afterSemiColon"  would return  "before1st.after1st;afterSemiColon"
  • ELSE, i just want to leave the string alone (no changes)

I'm having trouble with this since the length of characters between each period can vary

any help on the best way to achieve this would be greatly appreciated!  thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
ms
Super User (Alumni) ms
Super User (Alumni)

Re: remove part of a string (contingent upon number of periods within that string)?

Assuming there are no periods after the semicolon, this script should work:

(Change "Column" to the name of the column with strings)

col = Column( "Column" );

For Each Row(

  w = Words( col[], ".;" );

  If( N Items( w ) == 4,

  col[] = w[1] || "." || w[2] || ";" || w[4]

  );

);

View solution in original post

3 REPLIES 3
ms
Super User (Alumni) ms
Super User (Alumni)

Re: remove part of a string (contingent upon number of periods within that string)?

Assuming there are no periods after the semicolon, this script should work:

(Change "Column" to the name of the column with strings)

col = Column( "Column" );

For Each Row(

  w = Words( col[], ".;" );

  If( N Items( w ) == 4,

  col[] = w[1] || "." || w[2] || ";" || w[4]

  );

);

jmpbeginner
Level III

Re: remove part of a string (contingent upon number of periods within that string)?

thank you MS!  this works great. 


How would I modify this so that instead of editing the contents of "Column", it instead populates a new column with the new edited string?

ms
Super User (Alumni) ms
Super User (Alumni)

Re: remove part of a string (contingent upon number of periods within that string)?

col = Column( "Column" );

new_col = New Column( "New Column", character );

For Each Row(

  w = Words( col[], ".;" );

  If( N Items( w ) == 4,

  new_col[] = w[1] || "." || w[2] || ";" || w[4],

  new_col[] = col[]

  );

);