I know how to split strings in Python using square brackets ( [3:]
) / whatever number you want, but I cannot find how to do that in JS. Nothing seems to match what I am looking for. I am not very experienced in JS so I apologise if this has a very obvious answer.

- 313
- 4
- 5
- 11
-
6Have you made a Google search before you asked it? – squancy Jul 20 '18 at 20:25
-
1note that the term you're looking for is actually string _slicing_, not string _splitting_ – Hamms Jul 20 '18 at 20:26
-
Maybe [`.substring`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring) is what you're looking for? – CRice Jul 20 '18 at 20:26
-
@offstagealmond could you please clarify if you need to split on a character or if you are trying to get a specific segment of the string? – person Jul 20 '18 at 20:40
5 Answers
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

- 565
- 1
- 7
- 25
This is how you would do it: "STRING,STRING".split(',')
; if you wanted to split the string by commas. Hope this helps

- 446
- 3
- 19
-
or you could slice the string every so often. .slice(0,2) if you wanted the first 3 characters. – SharpInnovativeTechnologies Jul 20 '18 at 20:28
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.

- 458
- 3
- 16
-
That's not the answer. JS has a string function, you don't need to do that. Negative vote – Santiago Jul 20 '18 at 20:35
-
I didn't say that this is the only way to do it, but it's a pretty straightforward conversion of the way Python splits strings on indices, since that's that the user is accustomed to – person Jul 20 '18 at 20:38
-
-
the user never said they wanted an array, they said they wanted the equivalent of python indexing (which returns a string) – person Jul 20 '18 at 20:46
-
There are different approaches, depending on your needs ...
String.prototype.split() : Split a string with a separator
The split() method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.
let myString = "abc def";
let myResult = myString.split(" ");
console.log(myResult); // ['abc','def']]
RegExp : Split a string into chunks with defined length
If what you need is to split a string into smaller strings with a defined maximum length, you can use something like this :
function split (input, len) {
return input.match(new RegExp('.{1,'+len+'}(?=(.{'+len+'})+(?!.))|.{1,'+len+'}$', 'g'))
}
let myString= 'abcdefghi';
let myResult = split(myString,3)
console.log( myResult ) // ['abc','def','ghi']

- 7,640
- 2
- 45
- 43
-
-
String.prototype.split lets you split a string into chunks with defined length ? – colxi Jul 20 '18 at 20:52
-
-
Depends in wich language... in PHP , you can provide a second argument to **str_split( stringToSplit, splitLength )** to perform a split based on length. However , Javascript does not support it natively. – colxi Jul 20 '18 at 20:58