cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

JMP Wish List

We want to hear your ideas for improving JMP. Share them here.
Choose Language Hide Translation Bar

Function to remove repeated characters in a string

Hi.  I'm interested in a function that could be used to remove repeated characters from a string.  

What inspired this wish list request?  If I have a string that contains the same repeated character and sometimes I want to remove all but one instance of that character.  For instance, I have a string like "input1;;;;input2;;;input3", and I want to convert it to "input1;input2;input3".  I assumed I could just use SubstituteInto(str, ";;", ";") and it would recursively remove all repeated ";", but did not.  Instead it just would replace only the first 2 semicolons with a single semicolon, then stop.  

 

str = "input1;;;input2;;;input3";
SubstituteInto(str, ";;", ";");
Show(str);   //returns "input1;;input2;;;input3"

 

Note that the first set of semicolons was reduced from 3 to only 2, and the second set was not affected at all.  My current workaround is to embed this in a while loop:

 

curlen = Length(str) + 1;
While(Length(str) < curlen,
  SubstituteInto(str, ";;", ";");
);   //returns "input1;input2;input3"

 

What is the improvement you would like to see? I'd like to see a modification to the Substitute(), SubstituteInto(), and Munger() functions to include a "<< RECURSE" message that just makes it perform the same operation as the code above:

Substitute(x, patternString1, replacementString1, ..., < <<IGNORECASE >, < <<RECURSE >);
SubstituteInto(x, patternString1, replacementString1, ..., < <<IGNORECASE >, < <<RECURSE >);
Munger(s, startpos, findstringOrNChars, <replacestring, < <<RECURSE > >);

Alternatively, rather that change established functions, perhaps a new function could be created called "Trim Multiple()"

Trim Multiple(s, patternString);

where s is a string and patternString is one or more characters.  The code will search through s for any instances of patternString that are repeated consecutively.  Where it finds one, it replaces it with just one instance of patternString and continues searching s for more instances of patternString.

Why is this idea important? This would reduce the complexity of users' codes.  It would also be a way to help to highlight to users that the Substitute()/SubstituteInto()/Munger() functions are -not- inherently recursive, and in doing so this would help prevent bugs.  

3 Comments
hogi
Level XIII

While waiting for Trim Multiple(), you can use a combination of words() and concat items():

str = "input1;;;input2;;;input3";
Show(concat items(Words(str, ";"), ";"))
nikles
Level VI

@hogi thanks for the idea.  This is much more compact than my while loop.

scott_allen
Staff
Status changed to: Acknowledged

We will keep an eye on this to see if there are additional use cases that would benefit from a new function. In the meantime, it appears that @hogi's solution is sufficient.