如何将“拆分”逗号切换为冒号

时间:2023-01-16 15:15:51

I think I am missing something easy here. I am trying to split the URL and then switch the commas to colons. Is there an easy way to do this.

我想我在这里很容易丢失一些东西。我试图拆分URL,然后将逗号切换为冒号。是否有捷径可寻。

<div id="splitit"></div>

<script>
    var mySplitResult = window.location.href.split('/');
    document.getElementById('splitit').textContent = mySplitResult;
</script>

So the results of "mySplitResult" is value,value,value,value,value and I want value:value:value:value

所以“mySplitResult”的结果是值,值,值,值,值和我想要的值:值:值:值

5 个解决方案

#1


mySplitResult actually contains an array of strings.

mySplitResult实际上包含一个字符串数组。

window.location.href.split('/');
["http:", "", "*.com", "questions", "30086262", "how-to-switch-a-split-comma-to-colons"]

Then Array.prototype.toString converts it to a comma delimited string.

然后Array.prototype.toString将其转换为逗号分隔的字符串。

Instead use Array.prototype.join

而是使用Array.prototype.join

document.getElementById('splitit').textContent = window.location.href.split('/').join(':');

#2


var mySplitResult=window.location.href.split('/').join(':')

That is how I would do it.

这就是我要做的。

#3


<script>
    var mySplitResult = window.location.href.split('/');
    document.getElementById('splitit').textContent = mySplitResult.replace(',' , ':');
</script>

Very simply, it will replace any , with a :

非常简单,它将取代任何,用:

#4


I think you're looking for replace

我想你正在寻找替代品

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

var myReplaceResult = window.location.href.replace(/\//g, ':');

#5


The reason that "mySplitResult" is being set to a comma delimited string is due to the nature of some prototype methods hanging off of the Array type. As of ECMA5, if an object contains a join() method it will be called, otherwise the toString() method will be invoked (see Array.prototype.toString)

将“mySplitResult”设置为以逗号分隔的字符串的原因是由于某些原型方法的性质偏离了Array类型。从ECMA5开始,如果一个对象包含一个join()方法,它将被调用,否则将调用toString()方法(参见Array.prototype.toString)

For the Array type there is a join() method that uses a comma as the default delimiter. This explains why you are seeing "value,value,value", because join() is being called during the assignment of textContent to mySplitResult.

对于Array类型,有一个join()方法,它使用逗号作为默认分隔符。这就解释了为什么你会看到“价值,价值,价值”,因为在将textContent赋值给mySplitResult的过程中调用了join()。

To achieve your desired results you would change your code to call the join() method explicitly and pass in the desired delimiter:

要获得所需的结果,您可以更改代码以显式调用join()方法并传入所需的分隔符:

var mySplitResult = window.location.href.split('/');
document.getElementById('splitit').textContent = mySplitResult.join(':');

#1


mySplitResult actually contains an array of strings.

mySplitResult实际上包含一个字符串数组。

window.location.href.split('/');
["http:", "", "*.com", "questions", "30086262", "how-to-switch-a-split-comma-to-colons"]

Then Array.prototype.toString converts it to a comma delimited string.

然后Array.prototype.toString将其转换为逗号分隔的字符串。

Instead use Array.prototype.join

而是使用Array.prototype.join

document.getElementById('splitit').textContent = window.location.href.split('/').join(':');

#2


var mySplitResult=window.location.href.split('/').join(':')

That is how I would do it.

这就是我要做的。

#3


<script>
    var mySplitResult = window.location.href.split('/');
    document.getElementById('splitit').textContent = mySplitResult.replace(',' , ':');
</script>

Very simply, it will replace any , with a :

非常简单,它将取代任何,用:

#4


I think you're looking for replace

我想你正在寻找替代品

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

var myReplaceResult = window.location.href.replace(/\//g, ':');

#5


The reason that "mySplitResult" is being set to a comma delimited string is due to the nature of some prototype methods hanging off of the Array type. As of ECMA5, if an object contains a join() method it will be called, otherwise the toString() method will be invoked (see Array.prototype.toString)

将“mySplitResult”设置为以逗号分隔的字符串的原因是由于某些原型方法的性质偏离了Array类型。从ECMA5开始,如果一个对象包含一个join()方法,它将被调用,否则将调用toString()方法(参见Array.prototype.toString)

For the Array type there is a join() method that uses a comma as the default delimiter. This explains why you are seeing "value,value,value", because join() is being called during the assignment of textContent to mySplitResult.

对于Array类型,有一个join()方法,它使用逗号作为默认分隔符。这就解释了为什么你会看到“价值,价值,价值”,因为在将textContent赋值给mySplitResult的过程中调用了join()。

To achieve your desired results you would change your code to call the join() method explicitly and pass in the desired delimiter:

要获得所需的结果,您可以更改代码以显式调用join()方法并传入所需的分隔符:

var mySplitResult = window.location.href.split('/');
document.getElementById('splitit').textContent = mySplitResult.join(':');