web stats
string comparisons help - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 10-12-2017, 05:57 AM
stormcel stormcel is offline
OBX.2 Kenobi
 
Join Date: Mar 2010
Location: Phoenix
Posts: 78
stormcel is on a distinguished road
Default string comparisons help

I've been trying to compare some long strings for mapping to values, and I find that .startsWith and .includes and .contains are not recognized.
I just want to compare the start of a string with a bunch of different values to determine message type.
So, if anyone can please help.
How do I compare a variable string to see if it starts with a specific string?
Code:
var str="This string needs to be tested..."
if (str.startsWith("This string needs to be tested...") {
return validated;
}
The above doesn't work as "startsWith" is not recognized as a method in Mirth.

Any help at all is greatly appreciated!
Reply With Quote
  #2  
Old 10-12-2017, 06:54 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 6,893
narupley is on a distinguished road
Default

Try indexOf:

Code:
if (str.indexOf("This string needs to be tested...") == 0) {
You could also use regular expressions:

Code:
if (/^This string needs to be tested\.\.\..*/.test(str)) {
__________________
Step 1: JAVA CACHE...DID YOU CLEAR

Nicholas Rupley
Work: 949-237-6069
Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


- How do I foo?
- You just bar.
Reply With Quote
  #3  
Old 10-12-2017, 11:49 AM
stormcel stormcel is offline
OBX.2 Kenobi
 
Join Date: Mar 2010
Location: Phoenix
Posts: 78
stormcel is on a distinguished road
Default

Thank you so much N!!!
Now, don't take this as ingratitude, but....

I like the regex test example as it matches only at the beginning.

I also found this to work:
Code:
if (str.search("This string needs to be tested...") == 0)
As it is true only when the substring is at the beginning of the test string; however, I am having issues with special characters.
I noticed that you escaped the periods in your example.

Is there a way to pass the test string as a raw literal?
or do I have to escape ALL special characters first?

There are a lot of backslashes in the input string and in the various test strings.

and again, thanks for everything!

Last edited by stormcel; 10-12-2017 at 11:55 AM. Reason: errors
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 02:17 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Mirth Corporation