Commodore Commodore
Commodore
625 views

How to retrieve only part of a string

Jump to solution

Hello all,

 

I have a question that I am sure will be easy for many people, just not me...

 

I am reading and storing a long string into a trigger I setup on a table, and I need the first part of it only.  How can I get that either directly in the trigger, or by calling a Script?  I've tried the .before and .after functions that I saw in other places, but couldn't get them to work here.

 

Example string is

IM123456 The server is down US_Server Support 09:12:25

 

I can't figure out how to  get just the IM123456 out of the first part of the string.  There are no spaces in front of the IM#, so I just need to know how to read up until the first space.

 

Thanks ahead of time!

0 Likes
1 Solution

Accepted Solutions
Micro Focus Expert
Micro Focus Expert

I'm not too familiar with the before function. You could try this. Copy from 1st character to the position of first space.

 

im = thename.substr(0,str.indexOf(' '));

View solution in original post

5 Replies
Commodore Commodore
Commodore

I'll add what I have as of now.

 

thename is equal to "IM123456 The server is down US_Server Support 09:12:25" from my example

 

function getIMNumber( thename )
{
     if ( thename )
     {
       var im=thename;
       print("IM before function: ", im);
       im = im.before(" ");
       print("IM after function: ", im);
       return im;
     }
     else
       return false;
  }

 

The error I am seeing in the sm.log is

 1720(  2828) 07/01/2014 12:36:13  RAD E Script 'GetIMNumber'  line 7: ERROR TypeError: im.before is not a function at char 1

0 Likes
Micro Focus Expert
Micro Focus Expert

I'm not too familiar with the before function. You could try this. Copy from 1st character to the position of first space.

 

im = thename.substr(0,str.indexOf(' '));

View solution in original post

Absent Member.. Absent Member..
Absent Member..

The substr() method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.

 

Example

Extract parts of a string:

 

var str = "Hello world!";
var res = str.substr(1, 4)
 

The result of res will be:

ello
 
You can find the JavaScript String substr() Method in this link: http://www.w3schools.com/jsref/jsref_substr.asp
 
0 Likes
Commodore Commodore
Commodore

Thanks for the help, I got it to work.  A slight tweak was needed, but the end working function for others to see is this.

 

function getIMNumber( thename )
{
     if ( thename )
     {
       var im;
       print("IM before function: ", im);
       im = thename.substr(0,thename.indexOf(" "));
       print("IM after function: ", im);
       return im;
     }
     else
       return false;
}

 

0 Likes
Micro Focus Expert
Micro Focus Expert
Opps, forgot to replace the str sample with your var name. 😉 Glad it worked.
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.