user2243565
user2243565

Reputation: 13

JavaScript statement to determine the first occurrence of a value within a string

What is the JavaScript statement that would be used to determine the position of the first slash / within the string variable "address" and assign that value to the "result" variable.

Upvotes: 1

Views: 108

Answers (3)

Rico Sonntag
Rico Sonntag

Reputation: 1516

The method indexOf() returns the first occurrence of a given value within the string object. It returns -1 if the value isn't found.

var result = address.indexOf('/');

Upvotes: 1

Bhumi Shah
Bhumi Shah

Reputation: 9476

you can try below code: address.indexOf("/");

Upvotes: 0

karaxuna
karaxuna

Reputation: 26930

This will tell index of first '/':

var a = 'asdasd/';
a.indexOf('/');

And this will tell last:

var a = 'asdasd/asfwerf/';
a.lastIndexOf('/');

Upvotes: 1

Related Questions