I have this string:
0000000020C90037:TEMP:data
I need this string:
TEMP:data.
With PHP I would do this:
$str = '0000000020C90037:TEMP:data'; $arr = explode(':', $str); $var = $arr[1].':'.$arr[2];
How do I effectively explode
a string in JavaScript the way it works in PHP?
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.
The explode() function breaks a string into an array. Note: The "separator" parameter cannot be an empty string. Note: This function is binary-safe.
PHP Explode function breaks a string into an array. PHP Implode function returns a string from an array.
PHP implode() and explode() The implode() function takes an array, joins it with the given string, and returns the joined string. The explode() function takes a string, splits it by specified string, and returns an array.
This is a direct conversion from your PHP code:
//Loading the variable var mystr = '0000000020C90037:TEMP:data'; //Splitting it with : as the separator var myarr = mystr.split(":"); //Then read the values from the array where 0 is the first //Since we skipped the first element in the array, we start at 1 var myvar = myarr[1] + ":" + myarr[2]; // Show the resulting value console.log(myvar); // 'TEMP:data'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With