Fr3nZy
Fr3nZy

Reputation: 65

Splitting 2 strings with different lengths returns the same value

var strings1 = ("https://www.letsgodeep.com/deep/deeper/deepest/").split('/')
var strings2 = ("https://www.letsgodeep.com/deep/deeper/deepest/bottom").split('/')
console.log(Object.keys(strings1).length)
console.log(Object.keys(strings2).length)

Both of these return 7. I'm assuming it's because string 2 doesn't end with '/' but that will usually be the case. I can't figure out an easy way to tell if I have string1 or string2. Everything up to and including 'deeper' is always the same value.

UPDATE: Thanks for the help. JD's answer was a good idea but hngr18's solution worked out for what I need. (double check that we're on the right path && if length is greater than 6 we're at the 'bottom')

if ((location.href).split('/')[3] == 'deep' && Array.from(new Set(location.href.split('/'))).length > 6)

Upvotes: 0

Views: 202

Answers (6)

Wyck
Wyck

Reputation: 11730

Checking to see if it ends with a slash is a straightforward approach. str.endsWith('/') should do it.

Did you know that 'a/b/'.split('/') returns [ 'a', 'b', '' ]? There's an empty string that occurs after the last /.

If the last element is the empty string, then you can conclude that your original string ended with a /. (or the original was just the empty string.)

By the way, there's a very popular way to remove the empties described here, if you need to remove them after testing to see if the last one is empty.

...which basically involves filtering out strings that are falsey. (The empty string '' is falsey.) For example, 'a/b/'.split('/').filter(x=>x) returns only [ 'a', 'b' ].

But be careful, because if you filter the empties, then you can't tell the difference between a/b and a//b.

Upvotes: 0

Syam
Syam

Reputation: 323

I think you can use regular expression

console.log("https://www.letsgodeep.com/deep/deeper/deepest/".match(/\/\S/ig).length); // 4
console.log("https://www.letsgodeep.com/deep/deeper/deepest/bottom".match(/\/\S/ig).length); // 5

Hope this helps

Upvotes: 0

Jaydeep
Jaydeep

Reputation: 1716

You can check if the last item of splitted array is empty or not.

if(string1[string1.length-1]!="")
   yourString = string2;
else
   yourString = string1;

Or you can remove the last (/) from string. then you can split and check.

var strings1 = "https://www.letsgodeep.com/deep/deeper/deepest/";
var strings2 = "https://www.letsgodeep.com/deep/deeper/deepest/bottom";
if (strings1.substring(strings1.length-1) == "/")
    strings1 = strings1.substring(0, strings1.length-1);
strings1 = strings1.split('/');
strings2 = strings2.split('/');
console.log(Object.keys(strings1).length)
console.log(Object.keys(strings2).length)

Upvotes: 0

zfrisch
zfrisch

Reputation: 8660

It's because when using split if the last character is the same that you are splitting on, it returns an empty string and adds it to the array.

So, yes, it's correct. One way to fix this would be to filter your results to remove empty strings by testing if the value is falsy.

var string1 = ("https://www.letsgodeep.com/deep/deeper/deepest/").split('/')
var string2 = ("https://www.letsgodeep.com/deep/deeper/deepest/bottom").split('/')

alert(string1.filter(_=>_).length)
alert(string2.filter(_=>_).length)

Upvotes: 1

hngr18
hngr18

Reputation: 857

As the last item in the array strings2 is "" (empty string) and there is already a "" from the http://, use Array.from with a new set, these de-duplicate the data items and return a more faithful value

console.log(Array.from(new Set(strings1)).length);

returns 6

console.log(Array.from(new Set(strings2)).length);

returns 7

Upvotes: 0

ktilcu
ktilcu

Reputation: 3128

You could normalize the strings. If there is a trailing / then remove it. Then your method would work well!

Upvotes: 0

Related Questions