Get Value From A String Before And After A Special Character - Jquery
Hi I want to read values before and after special character in text of a variable. The special character will always be the same e.g., '|' For example, my text will be stored as v
Solution 1:
You can do this in one line using destructuring
A working example
var [before, after] = 'Username|Jackie'.split("|")
console.log(before)
console.log(after)
The above is the same as doing this:
var pieces = 'Username|Jackie'.split("|")
var before = pieces[0]
var after = pieces[1]
console.log(before)
console.log(after)
Solution 2:
.split() will take a string and generate an array around the delimiter you define. (String.prototype.split()) This means that running .split() on your strings will return an array of length 2 with your value in index 1.
const username = "Username|Jackie";
const arr = username.split("|");
console.log(arr); // ["Username", "Jackie"];
const username = "Username|Jackie";
const arr = username.split("|");
console.log(arr); // ["Username", "Jackie"];
console.log(arr[0]); // "Username"
console.log(arr[1]); // "Jackie"
const [type, val] = username.split("|");
console.log(type); // "Username"
console.log(val); // "Jackie"
Post a Comment for "Get Value From A String Before And After A Special Character - Jquery"