There are several functions in JavaScript which can be used to split a string.
1. Split - (Split a string into an array of substrings)
var str = "This is a string";
var str = "This is a string";
var res = str.split(" ");
console.log(res);
It is important that split gives back an array with the string chunks.
2. Substr - (Extract parts of a string)
var str = "This is a string";
var res = str.substr(1, 4);
console.log(res);
3. Substring - (Extract characters from a string)
var str = "This is a string";
var res = str.substring(1, 4);
console.log(res);
Reference
W3Schools Split
W3Schools Substr
W3Schools Substring
Videos
There are several functions in JavaScript which can be used to split a string.
1. Split - (Split a string into an array of substrings)
var str = "This is a string";
var str = "This is a string";
var res = str.split(" ");
console.log(res);
It is important that split gives back an array with the string chunks.
2. Substr - (Extract parts of a string)
var str = "This is a string";
var res = str.substr(1, 4);
console.log(res);
3. Substring - (Extract characters from a string)
var str = "This is a string";
var res = str.substring(1, 4);
console.log(res);
Reference
W3Schools Split
W3Schools Substr
W3Schools Substring
I think you want to use substring() (read more here: https://www.w3schools.com/jsref/jsref_substring.asp)
For example,
var str = "Hello world!";
var res = str.substring(1, 4);
would be equivalent to this Python code:
str = "Hello world!"
res = str[1:4]
res would be 'ell'. The start and end indices work the same as in Python (includes the start index, goes through but not including the end index.
With JavaScript’s String.prototype.split function:
var input = 'john smith~123 Street~Apt 4~New York~NY~12345';
var fields = input.split('~');
var name = fields[0];
var street = fields[1];
// etc.
According to ECMAScript6 ES6, the clean way is destructuring arrays:
const input = 'john smith~123 Street~Apt 4~New York~NY~12345';
const [name, street, unit, city, state, zip] = input.split('~');
console.log(name); // john smith
console.log(street); // 123 Street
console.log(unit); // Apt 4
console.log(city); // New York
console.log(state); // NY
console.log(zip); // 12345
You may have extra items in the input string. In this case, you can use rest operator to get an array for the rest or just ignore them:
const input = 'john smith~123 Street~Apt 4~New York~NY~12345';
const [name, street, ...others] = input.split('~');
console.log(name); // john smith
console.log(street); // 123 Street
console.log(others); // ["Apt 4", "New York", "NY", "12345"]
I supposed a read-only reference for values and used the const declaration.
Enjoy ES6!
Documentation can be found e.g. at MDN. Note that .split() is not a jQuery method, but a native string method.
If you use .split() on a string, then you get an array back with the substrings:
var str = 'something -- something_else';
var substr = str.split(' -- ');
// substr[0] contains "something"
// substr[1] contains "something_else"
If this value is in some field you could also do:
tRow.append($('<td>').text($('[id$=txtEntry2]').val().split(' -- ')[0])));
If it is the basic JavaScript split function, look at documentation, JavaScript split() Method.
Basically, you just do this:
var array = myString.split(' -- ')
Then your two values are stored in the array - you can get the values like this:
var firstValue = array[0];
var secondValue = array[1];
Use the .split() method. When specifying an empty string as the separator, the split() method will return an array with one element per character.
entry = prompt("Enter your name")
entryArray = entry.split("");
ES6 :
const array = [...entry]; // entry="i am" => array=["i"," ","a","m"]