Subscribe Bookmark RSS Feed

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

jmpbeginner

Community Trekker

Joined:

Sep 11, 2013

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
Solution

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]

  );

);

3 REPLIES
Solution

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

Community Trekker

Joined:

Sep 11, 2013

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

Joined:

Jun 23, 2011

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[]

  );

);