Start a topic
With the exception of Windchill, The PTC Community is on read-only status until April 6 in preparation for moving our community to a new platform. Learn more here
cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

The PTC Community is on temporary read only status in preparation for moving our community to a new platform. Learn more here

Translate the entire conversation x

Replace a string with a string

Snowshoeman
13-Aquamarine

Replace a string with a string

I have a character replace function.  I would like to use it to replace a string with a string.  If the user inputted duplicate "OR's" then I want the function to thin out "OR OR" to just "OR".

 

Thanks in advance

ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:Snowshoeman)

You may try this string replace function.

grafik.png

View solution in original post

6 REPLIES 6
Werner_E
25-Diamond I
(To:Snowshoeman)

You may try this string replace function.

grafik.png

Snowshoeman
13-Aquamarine
(To:Werner_E)

Thank You!  I learn so much from this forum!  Your suggestion works!

SPaulis
14-Alexandrite
(To:Snowshoeman)

@Snowshoeman just do an initial search for "s1" inside "s2".  If it is, then return the original text so that you do not have an infinite recursion.  Or, like typical replace functions, keep track of the position of the last search and so that the replacement text does not itself get searched.  Here are both examples.

 

Screenshot 2026-03-09 082605.png
 
If you want the flexibility an exhaustive search while having the protection from recursion, then combine both function so that you keep track of the position only if there can be a recursion:
 
Screenshot 2026-03-09 083310.png

"check" remembers if "s1" is inside "s2", then in the 'if' function, "pos" is advanced if "check" is true, or it is reset to zero if not.  


 

Shawn P.
“It’s OK to fall in love with your heart. But, when it comes to making engineering decisions, don’t design with your heart.” – Blodgett, Omer W.
Snowshoeman
13-Aquamarine
(To:SPaulis)

Thanks!

Werner_E
25-Diamond I
(To:SPaulis)


@SPaulis wrote:

@Snowshoeman just do an initial search for "s1" inside "s2".  If it is, then return the original text so that you do not have an infinite recursion. 

Actually my suggestion did not use recursion but rather an iteration loop.

But I like the idea of intercepting the infinite loop that could occur in some cases.

 

And since we were just talking about recursion, here is a recursive variant of the string replacement that solves the problem in a similar way.

grafik.png

Snowshoeman
13-Aquamarine
(To:Werner_E)

Thanks!  I will chew on this too.

Announcements


Top Tags