2

The initial string:
initString = '/digital/collection/music/bunch/of/other/stuff'

What I want:
music

  • Specifically, I want any term (will never include slashes) that would come between collection/ and /bunch

How I'm going about it:

if(initString.includes('/digital/collection/')){
    let slicedString = initString.slice(19); //results in 'music/bunch/of/other/stuff'
    let indexOfSlash = slicedString.indexOf('/'); //results, in this case, to 5
    let desiredString = slicedString.slice(0, indexOfSlash); //results in 'music'
}

Question:
How the heck do I accomplish this in javascript in a more elegant way?

  • I looked for something like an endIndexOf() that would replace my hardcoded .slice(19)
    • lastIndexOf() isn't what I'm looking for, because I want the index at the end of the first instance of my substring /digital/collection/
  • I'm looking to keep the number of lines down, and I couldn't find anything like a .getStringBetween('beginCutoff, endCutoff')

Thank you in advance!

James B
  • 432
  • 5
  • 22

7 Answers7

5

your title says "index" but your example shows you wanting to return a string. If, in fact, you are wanting to return the string, try this:

if(initString.includes('/digital/collection/')) {
    var components = initString.split('/');
    return components[3]; 
}
Rick
  • 1,710
  • 8
  • 17
  • Thank you for correcting me! You are right as I was definitely looking for the string. I've edited the title to fit the question. – James B Oct 25 '19 at 19:31
3

If the path is always the same, and the field you want is the after the third /, then you can use split.

var initString = '/digital/collection/music/bunch/of/other/stuff';
var collection = initString.split("/")[2]; // third index

In the real world, you will want to check if the index exists first before using it.

var collections = initString.split("/");
var collection = "";
if (collections.length > 2) {
    collection = collections[2];
}
Ibu
  • 42,752
  • 13
  • 76
  • 103
  • This is exactly what I was looking for, though I believe you would need `.split('/')[3]`. (fourth index). I tested using the third index on `/digital/collection/music/bunch` and it resulted in `collections`. Thank you very much!!! – James B Oct 25 '19 at 19:19
2

You can use const desiredString = initString.slice(19, 24); if its always music you are looking for.

j8user28
  • 46
  • 2
  • The term where music is will change and may also include numbers, so unfortunately, this wouldn't work.. thank you though! – James B Oct 25 '19 at 19:13
2

If you need to find the next path param that comes after '/digital/collection/' regardless where '/digital/collection/' lies in the path

  1. first use split to get an path array
  2. then use find to return the element whose 2 prior elements are digital and collection respectively

const initString = '/digital/collection/music/bunch/of/other/stuff'

const pathArray = initString.split('/')

const path = pathArray.length >= 3 
    ? pathArray.find((elm, index)=> pathArray[index-2] === 'digital' && pathArray[index-1] === 'collection')
    : 'path is too short'

console.log(path)
Willman.Codes
  • 1,352
  • 1
  • 5
  • 13
1

Think about this logically: the "end index" is just the "start index" plus the length of the substring, right? So... do that :)

const sub = '/digital/collection/';
const startIndex = initString.indexOf(sub);
if (startIndex >= 0) {
    let desiredString = initString.substring(startIndex + sub.length);
}

That'll give you from the end of the substring to the end of the full string; you can always split at / and take index 0 to get just the first directory name form what remains.

IceMetalPunk
  • 5,476
  • 3
  • 19
  • 26
1

You can also use regular expression for the purpose.

const initString = '/digital/collection/music/bunch/of/other/stuff';
const result = initString.match(/\/digital\/collection\/([a-zA-Z]+)\//)[1];
console.log(result);

The console output is:

music

dmigo
  • 2,849
  • 4
  • 41
  • 62
1

If you know the initial string, and you have the part before the string you seek, then the following snippet returns you the string you seek. You need not calculate indices, or anything like that.

// getting the last index of searchString
// we should get: music
const initString = '/digital/collection/music/bunch/of/other/stuff'
const firstPart = '/digital/collection/'


const lastIndexOf = (s1, s2) => {
  return s1.replace(s2, '').split('/')[0]
}

console.log(lastIndexOf(initString, firstPart))
muka.gergely
  • 8,063
  • 2
  • 17
  • 34