简体   繁体   English

如何使用jQuery或JavaScript中的索引删除字符串之间的字符?

[英]how to Remove the characters in between a string using index in jquery or javascript?

I want to remove the few characters in a string using index. 我想使用索引删除字符串中的几个字符。 for example: My input is: "5,4,3,2,1" I want to remove the 1th to 2nd index position characters(here , to 4). 例如:我的输入是:“ 5,4,3,2,1”我要删除第1到第2索引位置字符(在这里,到4)。 The Output should be 5,3,2,1. 输出应为5,3,2,1。 Is there any predefined function in jquery or javascript to done this? 在jquery或javascript中是否有任何预定义的函数可以做到这一点?

I would juse use javascripts split function for that. 我会为此使用javascripts split函数。

So if you have 所以如果你有

 var string = "5,4,3,2,1";

than you just need to do 比你只需要做的

var splitted = string.split(",");

whereas the character in the brackets is the one you want to split on. 而方括号中的字符是您要分割的字符。 After you did that, you can just make a new string, and build it with the array elements. 完成之后,您可以制作一个新字符串,并使用数组元素构建它。

So you do something like 所以你做类似的事情

var string2 = splitted[0] + "," + splitted[2] + "," + splitted[3] + "," splitted[4];

You can try to use substring function like this: 您可以尝试使用像这样的子字符串函数:

var mystring = "5,4,3,2,1";
alert( mystring.substr(0, 1) + mystring.substr(3));

JSFIDDLE DEMO JSFIDDLE演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM