indexOf using a variable that is a string

Hannah C
Giga Expert

Hello,

I have a multi line text box variable called all_subprofiles. It is a string of a bunch of users names and their profile. Example:

John Smith - Traditional User

Jane Doe - Special User

Bob Williams - Heavy Traveler

I want to find u_requested_for in the list and then print just their profile. For example, if John Smith is the requestor I just want to print Traditional User.

I tried this but it is just taking off the first three letters instead of taking off the name + ' - '

var name = current.u_requested_for.name;
var currentsub_profile = all_subprofiles.substring(all_subprofiles.indexOf(name)+3);

1 ACCEPTED SOLUTION

Hi Hannah,

A simple way would be to split the string into an array at the comma, for example: currentsub_profile = 'Traditional User, Jane Doe - Special User, Bob Williams - Heavy Traveler', something like this would work:

var string = currentsub_profile.split(',')[0]; //this returns "Traditional User"

You can also achieve this by using regular expression:

var regex = /([^,]+)/;
var string = currentsub_profile.match(regex)[1]; //returns "Traditional User"

Phuong

If you find my suggestions helpful, please mark it as correct or helpful to help out others as well 🙂 

 

View solution in original post

4 REPLIES 4

Phuong Nguyen
Kilo Guru

Hi Hannah,

indexOf() returns the first occurrence of the specified value, so for example, if name = 'John Smith', all_subprofiles.indexOf(name) will return 0, then +3 = 3. So you are now effectively taking of the first 3 letter of the whole string: 0 (J), 1(o), 2(h). I hope that makes sense.

Phuong

If you find my suggestions helpful, please mark it as correct or helpful to help out others as well 🙂 

 

Thank you! I actually figured out how to find the name in the list and then remove the name and the three characters " - " from the front of the string and it is working. But now how to I return everything after the comma in the string?

Here is my code

var name = current.u_approve_it_profile_for.name.toString();
var name_length = name.length;
var currentsub_profile = subprofiles_array.substring(all_subprofiles.indexOf(name) + name_length + 3);

 

And if the user is John Smith it is returning:

Traditional User, Jane Doe - Special User, Bob Williams - Heavy Traveler

 

So I need to now remove everything after the first comma...would you be able to help me with that?

Hi Hannah,

A simple way would be to split the string into an array at the comma, for example: currentsub_profile = 'Traditional User, Jane Doe - Special User, Bob Williams - Heavy Traveler', something like this would work:

var string = currentsub_profile.split(',')[0]; //this returns "Traditional User"

You can also achieve this by using regular expression:

var regex = /([^,]+)/;
var string = currentsub_profile.match(regex)[1]; //returns "Traditional User"

Phuong

If you find my suggestions helpful, please mark it as correct or helpful to help out others as well 🙂 

 

A-N
Tera Expert

Here's one way to accomplish this assuming your name variable contains the string below

find_real_file.png

Output

find_real_file.png