Dou*_*eux 339 javascript php string
我有一个看起来像这样的字符串:
0000000020C90037:TEMP:数据
我需要在第一次冒号之后抓住所有内容,以便我有TEMP:数据.
我不经常使用Javascript,如果它是PHP我会这样做:
$str = '0000000020C90037:TEMP:data';
$arr = explode(":", $str);
$var = $arr[1].":".$arr[2];
Run Code Online (Sandbox Code Playgroud)
Joh*_*ock 596
这是从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'
Run Code Online (Sandbox Code Playgroud)
Fel*_*ing 44
str = str.substr(str.indexOf(':')+1);
Run Code Online (Sandbox Code Playgroud)
但是,相当于explode将split.
psy*_*brm 33
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;
};
Run Code Online (Sandbox Code Playgroud)
应该完全模仿PHP的explode()函数.
'a'.explode('.', 2); // ['a']
'a.b'.explode('.', 2); // ['a', 'b']
'a.b.c'.explode('.', 2); // ['a', 'b.c']
Run Code Online (Sandbox Code Playgroud)
创建一个对象:
// 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";
Run Code Online (Sandbox Code Playgroud)
如果你喜欢 php,请看一下php.JS - JavaScriptexplode
或者在正常的 JavaScript 功能中:`
var vInputString = "0000000020C90037:TEMP:data";
var vArray = vInputString.split(":");
var vRes = vArray[1] + ":" + vArray[2]; `
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
335775 次 |
| 最近记录: |