Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

I want to get userlocationcity by using indexof() function

sainath reddy
Tera Contributor

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)){

}

2 ACCEPTED SOLUTIONS

Prince Arora
Tera Sage

@sainath reddy 

 

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.

View solution in original post

Chetan Mahajan
Mega Sage

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

View solution in original post

6 REPLIES 6

Prince Arora
Tera Sage

@sainath reddy 

 

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.

Chetan Mahajan
Mega Sage

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

var userlocationcity = "Santa Rosa, CA USA";

cant we get the value dynamically and search in the indexOf() dynamically into the function

 

 

@sainath reddy 

 

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.