cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

String replace not working

SB_9732186
11-Garnet

String replace not working

Hi all,

 

I am trying to replace '_' underscores from a given string, using the string.replace function,

SB_9732186_1-1657081844278.png

but I am not able to achieve this,

SB_9732186_2-1657081939199.png

can anyone tell me where I am going wrong.

 

 

Thanks,

Sujith

 

10 REPLIES 10

Sujith,

 

What version of ThingWorx are you on?  I'm in 9.0 and it seems to work for me:

 

nmilleson_0-1657082630754.png

 

Hi @nmilleson ,

 

I am using the latest build available on the cloud, I guess it is 9.2

In output even I am getting the desired result,

SB_9732186_0-1657083873176.png

but in log I am not getting it.

SB_9732186_2-1657083970152.png

 

 

Thanks,

Sujith

Sujith,

 

What happens if you try and run the replace right in the log statement?

 

logger.warn(Unique.replace(/_/g,''));

Hi @nmilleson ,

 

I did this: 

SB_9732186_0-1657167503650.png

and in the logs I got this:

SB_9732186_1-1657167560659.png

 

I am also sending the variable UniqueID to another service, in the other service also the value contains '_':

SB_9732186_2-1657167621555.png

 

Thanks,

Sujith

VP_10348089
5-Regular Member
(To:SB_9732186)

Sujith,

 

    I have tried this and working fine. Could you please try like :arraydata[2].replace('_' , '')

 

Hi @VP_10348089 ,

 

I have done this: 

SB_9732186_0-1657705188910.png

and in logs I got this: 

SB_9732186_1-1657705239284.png

 

Thanks,

Sujith

Hi all,

 

Anyone has a solution for this?

Or do I raise a Case on eSupport?

 

Thanks,

Sujith

M4RC
14-Alexandrite
(To:SB_9732186)

Hello,

you can replace another part of the string and see how it reacts?

like :arraydata[2].replace('IOTMH' , 'removed'));

 

Are you using the correct declaration of var/let? (even if this should have no affect on the logger)

 

If you can always remove the last character of the string, try "slice".

like :arraydata[2].slice(0, -1)

 

Best regards

Marc

What Marc wrote below is a very good solution.

There's a very slim chance that the code you shown us is not really the code that executed (by mistake?)

Are you 100% sure that the logger statement you see is generated by the code you've shown to us?

Otherwise, can you please send me the whole content of your service please in a PM?

This is clearly a topic for a Support Case, but I'm really curious to see the cause of this issue, as it should not happen.

Also write back the full version of ThingWorx you're using: 9.2.?

wcui
14-Alexandrite
(To:SB_9732186)

hi SB_9732186

 

below code worked fine on TW 9.3.2. Can you try on your TW instance?

---------------------------------------------------

var test = 'IOTMH22G022420_';
var result = test.replace('_','');
logger.warn("replace test : " + test.replace('_',''));

Top Tags