- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2023 12:20 AM - edited 08-08-2023 12:28 AM
i'm using userlocationcity variable from location to get the city.after getting the location i have to use 'if' condition with the indexOf() function with the userlocationcity.indexOf("string").how to search the string by using indexOf() > 1 to match the given string in indexOf() string to the userlocation string
var userlocationcity = current.variables.requested_for.location.toString().toLowerCase();
if((userlocationcity == 'hyderabad')||(userlocation.indexOf('hyderabad')==-1)){
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2023 12:28 AM
Basically "indexOf" is the Javascript String function it returns
"-1" - when string is not found
Index - returns the index of string where it is available in the complete sentence
you can check as below method:
if(userlocationcity.indexOf(String) !=-1){ // it means its found
}
OR
if(userlocationcity.indexOf(String) >= 0 ){
}
If my answer solved your issue, please mark my answer as ✅ Correct & 👍Helpful based on the Impact.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2023 12:30 AM - edited 08-08-2023 12:32 AM
Hello @sainath reddy ,
If you want to check if a specific string is present anywhere in the userlocationcity string, you should use indexOf() != -1. This condition will return true if the substring is found and false if it's not found. use script as below
// Assume userlocationcity contains the user's location city as a string
var userlocationcity = "Santa Rosa, CA USA";
// String to search for
var searchString = "Santa Rosa";
// Check if the searchString is present in userlocationcity using indexOf()
if (userlocationcity.indexOf(searchString) != -1) {
// The searchString is found in userlocationcity
gs.info("Search string found in user's location city.");
} else {
// The searchString is not found in userlocationcity
gs.info("Search string not found in user's location city.");
}
Kindly mark Correct and helpful if applicable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2023 12:28 AM
Basically "indexOf" is the Javascript String function it returns
"-1" - when string is not found
Index - returns the index of string where it is available in the complete sentence
you can check as below method:
if(userlocationcity.indexOf(String) !=-1){ // it means its found
}
OR
if(userlocationcity.indexOf(String) >= 0 ){
}
If my answer solved your issue, please mark my answer as ✅ Correct & 👍Helpful based on the Impact.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2023 12:30 AM - edited 08-08-2023 12:32 AM
Hello @sainath reddy ,
If you want to check if a specific string is present anywhere in the userlocationcity string, you should use indexOf() != -1. This condition will return true if the substring is found and false if it's not found. use script as below
// Assume userlocationcity contains the user's location city as a string
var userlocationcity = "Santa Rosa, CA USA";
// String to search for
var searchString = "Santa Rosa";
// Check if the searchString is present in userlocationcity using indexOf()
if (userlocationcity.indexOf(searchString) != -1) {
// The searchString is found in userlocationcity
gs.info("Search string found in user's location city.");
} else {
// The searchString is not found in userlocationcity
gs.info("Search string not found in user's location city.");
}
Kindly mark Correct and helpful if applicable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2023 12:59 AM
var userlocationcity = "Santa Rosa, CA USA";
cant we get the value dynamically and search in the indexOf() dynamically into the function
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2023 01:02 AM
You can get Values dynamically like
var userlocationcity = (current.location.city).getDisplayValue() // this is just an example
if(userlocationcity.indexOf(String)!= -1){
}
If my answer solved your issue, please mark my answer as ✅ Correct & 👍Helpful based on the Impact.