Javascript 等价于 PHP Explode()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4514323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:44:39  来源:igfitidea点击:

Javascript Equivalent to PHP Explode()

javascriptphpstring

提问by Doug Molineux

I have this string:

我有这个字符串:

0000000020C90037:TEMP:data

0000000020C90037:TEMP:数据

I need this string:

我需要这个字符串:

TEMP:data.

温度:数据。

With PHP I would do this:

使用 PHP 我会这样做:

$str = '0000000020C90037:TEMP:data';
$arr = explode(':', $str);
$var = $arr[1].':'.$arr[2];

How do I effectively explodea string in JavaScript the way it works in PHP?

我如何有效地explode在 JavaScript 中使用字符串作为它在 PHP 中的工作方式?

回答by John Hartsock

This is a direct conversion from your PHP code:

这是从您的 PHP 代码的直接转换:

//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'

回答by Felix Kling

You don't need to split. You can use indexOfand substr:

你不需要分开。您可以使用indexOfsubstr

str = str.substr(str.indexOf(':')+1);

But the equivalent to explodewould be split.

但相当于explode将是split

回答by psycho brm

String.prototype.explode = function (separator, limit)
{
    const array = this.split(separator);
    if (limit !== undefined && array.length >= limit)
    {
        array.push(array.splice(limit - 1).join(separator));
    }
    return array;
};

Should mimic PHP's explode() function exactly.

应该完全模仿 PHP 的 expand() 函数。

'a'.explode('.', 2); // ['a']
'a.b'.explode('.', 2); // ['a', 'b']
'a.b.c'.explode('.', 2); // ['a', 'b.c']

回答by Quentin

Looks like you want split

看起来你想分开

回答by weltraumpirat

Try this:

尝试这个:

arr = str.split (":");

回答by JM Design

create's an object :

创建一个对象:

// create a data object to store the information below.
    var data   = new Object();
// this could be a suffix of a url string. 
    var string = "?id=5&first=John&last=Doe";
// this will now loop through the string and pull out key value pairs seperated 
// by the & character as a combined string, in addition it passes up the ? mark
    var pairs = string.substring(string.indexOf('?')+1).split('&');
    for(var key in pairs)
    {
        var value = pairs[key].split("=");
        data[value[0]] = value[1];
    }

// creates this object 
    var data = {"id":"5", "first":"John", "last":"Doe"};

// you can then access the data like this
    data.id    = "5";
    data.first = "John";
    data.last  = "Doe";

回答by exebook

console.log(('0000000020C90037:TEMP:data').split(":").slice(1).join(':'))

console.log(('0000000020C90037:TEMP:data').split(":").slice(1).join(':'))

outputs: TEMP:data

输出: TEMP:data

  • .split() will disassemble a string into parts
  • .join() reassembles the array back to a string
  • when you want the array without it's first item, use .slice(1)
  • .split() 将字符串分解成部分
  • .join() 将数组重新组合成字符串
  • 当您想要没有第一项的数组时,请使用 .slice(1)

回答by eriksv88

If you like php, take a look at php.JS - JavaScript explode

喜欢php的可以看看php.JS——JavaScript爆炸

Or in normal JavaScript functionality: `

或者在普通的 JavaScript 功能中:`

var vInputString = "0000000020C90037:TEMP:data";
var vArray = vInputString.split(":");
var vRes = vArray[1] + ":" + vArray[2]; `

回答by German Rumm

Use String.split

使用 String.split

"0000000020C90037:TEMP:data".split(':')

"0000000020C90037:TEMP:data".split(':')

回答by dylan maxey

With no intentions to critique John Hartsock, just in case the number of delimiters may vary for anyone using the given code, I would formally suggest to use this instead...

无意批评John Hartsock,以防万一使用给定代码的任何人的分隔符数量可能会有所不同,我会正式建议改用它......

var mystr = '0000000020C90037:TEMP:data';
var myarr = mystr.split(":");
var arrlen = myarr.length;
var myvar = myarr[arrlen-2] + ":" + myarr[arrlen-1];