Home > Mobile >  Get String Specific Characters
Get String Specific Characters

Time:08-11

I am trying to write some code that takes a uuid string and returns only the characters between the 2nd and 3rd _ characters in an array. What I currently have below is returning every character in the string in to the array. I have been looking at this for some time and am obviously missing something glaringly obvious I suppose. Can someone maybe point out what is wrong here?

var uuid = "159as_ss_5be0lk875iou_.1345.332.11.2"
var count = 0
var  values = []
            
for(y=0; y<uuid.length; y  ){
    if(uuid.charAt(y) == '_'){
        count  
    }
    if(count = 2){
        values.push(uuid.charAt(y))
    }
}
return values

EDIT:

So for my case I would want the values array to contain all of the characters in 5be0lk875iou

CodePudding user response:

You can get the same behavior in less lines of code, like this:

let uuid = "159as_ss_5be0lk875iou_.1345.332.11.2"
let values = uuid.split("_")[2];

CodePudding user response:

You can use the split function to do that:

let values = uuid.split("_");

CodePudding user response:

By using the split function, you can get separate the whole string into smaller parts:

const parts = uuid.split("_");

This will return the following array:

["159as", "ss", "5be0lk875iou", ".1345.332.11.2"]

From here, you can take the string at index 2, and split it again to receive an array of characters:

const values = parts[2].split("");
  • Related