-2

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.

OffstageAlmond
  • 313
  • 4
  • 5
  • 11
  • 6
    Have you made a Google search before you asked it? – squancy Jul 20 '18 at 20:25
  • 1
    note 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 Answers5

4

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

squancy
  • 565
  • 1
  • 7
  • 25
1

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

1

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.

person
  • 458
  • 3
  • 16
0

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']
colxi
  • 7,640
  • 2
  • 45
  • 43
  • Again...js has a split function, you don't need that – Santiago Jul 20 '18 at 20:36
  • String.prototype.split lets you split a string into chunks with defined length ? – colxi Jul 20 '18 at 20:52
  • that's not the objetive of splitting a string – Santiago Jul 20 '18 at 20:53
  • 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
-2

Very simple!

var str = "How are you doing today?";
var res = str.split(" ");
Santiago
  • 76
  • 7