Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
the data is like this /Canada/Quebec/Montreal or /Japan/Osaka/ or /USA/California/Sanfransico
and I want to extract only country name which is right after first /.
The result should be Canada, Japan and USA
Thank you.
You can split the string on slash and take the second item:
var country = data.split('/')[1];
You can also locate the second slash and use substr:
substr
var country = data.substr(1, data.indexOf('/', 1) - 1);
Add a comment
Your substring operation would look something like this:
substring
var data = '/Canada/Quebec/Montreal'; var country = data.substring(1,data.indexOf('/',1));
So basically take the string, exclude the first character up to the next occurance of / after the first index.
/
var thestring = '/Canada/Quebec/Montreal'; console.log(thestring.split('/')[1]);
var country = str.slice(1, str.indexOf('/', 2))
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.