如何将逗号分隔的值字符串包装在单引号中? [英] How to wrap comma separated values string in single quotes?

查看:84
本文介绍了如何将逗号分隔的值字符串包装在单引号中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下字符串示例:

"label1, label2, label3, label4, label5"

现在,因为它将用作初始化jquery插件的对象,所以它需要如下所示:

Now, because this will be used as an object initialising a jquery plugin, it needs to look like this:

'label1','label2','label3','label4','label5'

我已经设法用split(,")分割字符串,将其转换为数组,但是我不确定如何用单引号将每个数组项包装起来,在那个阶段,我将能够将其连接回字符串以供使用?

I already managed to split the string with split(","), turning it into an array, however i am not sure how i can wrap each of the array items with single quotes, at which stage, i will be able to join it back to a string for usage?

有什么想法吗?

解决方案只能是js或jquery.

solution can be js only or jquery.

推荐答案

您可以按照以下步骤进行操作.希望对您有所帮助.

You can do it like below. Hope it helps.

var input = "label1, label2, label3, label4, label5";
var result = '\'' + input.split(',').join('\',\'') + '\'';

这篇关于如何将逗号分隔的值字符串包装在单引号中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆