Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

similar function explode php in javascript?

I have problem when I want to separate my string in JavaScript, this is my code :

var str= 'hello.json';
str.slice(0,4); //output hello
str.slice(6,9); //output json

the problem is when i want to slice second string ('json') I should create another slice too.

I want to make this code more simple , is there any function in JavaScript like explode function in php ?

like image 448
user3620540 Avatar asked Oct 26 '15 04:10

user3620540


People also ask

What is explode in JavaScript?

If you want to explode or split a string from a certain character or separator you can use the JavaScript split() method. The following example will show you how to split a string at each blank space. The returned value will be an array, containing the splitted values.

What is difference between explode () or implode () in PHP?

PHP Explode function breaks a string into an array. PHP Implode function returns a string from an array.

What does explode () do in PHP?

The explode() function breaks a string into an array. Note: The "separator" parameter cannot be an empty string. Note: This function is binary-safe.

How convert string to array in PHP with explode?

1) Convert String to Array using explode()explode() method is one of the built-in function in PHP which can be used to convert string to array. The explode() function splits a string based on the given delimiter. A delimiter acts as a separater and the method splits the string where the delimiter exists.


1 Answers

You can use split()

var str = 'hello.json';
var res = str.split('.');

document.write(res[0] + ' ' + res[1])

or use substring() and indexOf()

var str = 'hello.json';

document.write(
  str.substring(0, str.indexOf('.')) + ' ' +
  str.substring(str.indexOf('.') + 1)
)
like image 62
Pranav C Balan Avatar answered Sep 21 '22 04:09

Pranav C Balan