如何使用Javascript从字符串中删除字符?

本文翻译自:How can I remove a character from a string using Javascript?

I am so close to getting this, but it just isn't right. 我非常接近这个意思,但这是不对的。 All I would like to do is remove the character r from a string. 我要做的就是从字符串中删除字符r The problem is, there is more than one instance of r in the string. 问题是,字符串中有多个r实例。 However, it is always the character at index 4 (so the 5th character). 但是,它始终是索引4处的字符(因此是第5个字符)。

example string: crt/r2002_2 示例字符串: crt/r2002_2

What I want: crt/2002_2 我想要的: crt/2002_2

This replace function removes both r 这种替换功能将同时删除r

mystring.replace(/r/g, '')

Produces: ct/2002_2 产生: ct/2002_2

I tried this function: 我试过这个功能:

String.prototype.replaceAt = function (index, char) {
    return this.substr(0, index) + char + this.substr(index + char.length);
}
mystring.replaceAt(4, '')

It only works if I replace it with another character. 仅当我用另一个字符替换它时,它才起作用。 It will not simply remove it. 它不会简单地将其删除。

Any thoughts? 有什么想法吗?


#1楼

参考:https://stackoom.com/question/fg0z/如何使用Javascript从字符串中删除字符


#2楼

对于全局替换“ / r”,此代码对我有用。

mystring = mystring.replace(/\/r/g,'');

#3楼

I dislike using replace function to remove characters from string. 我不喜欢使用替换功能从字符串中删除字符。 This is not logical to do it like that. 这样是不合逻辑的 Usually I program in C# (Sharp), and whenever I want to remove characters from string, I use the Remove method of the String class, but no Replace method, even though it exists, because when I am about to remove, I remove, no replace. 通常,我使用C#(Sharp)进行编程,每当我想从字符串中删除字符时,我都会使用String类的Remove方法,但是即使存在,也不会使用Replace方法,因为当我要删除时,我会删除,无可替代。 This is logical! 这是合乎逻辑的!

In Javascript, there is no remove function for string, but there is substr function. 在Javascript中,没有用于字符串的remove函数,但是有substr函数。 You can use the substr function once or twice to remove characters from string. 您可以使用一次或两次substr函数从字符串中删除字符。 You can make the following function to remove characters at start index to the end of string, just like the c# method first overload String.Remove(int startIndex): 您可以执行以下函数来删除字符串的开始索引处到字符串末尾的字符,就像c#方法的第一个重载String.Remove(int startIndex)一样:

function Remove(str, startIndex) {
    return str.substr(0, startIndex);
}

and/or you also can make the following function to remove characters at start index and count, just like the c# method second overload String.Remove(int startIndex, int count): 和/或您还可以使以下函数删除起始索引和计数处的字符,就像c#方法第二次重载String.Remove(int startIndex,int count)一样:

function Remove(str, startIndex, count) {
    return str.substr(0, startIndex) + str.substr(startIndex + count);
}

and then you can use these two functions or one of them for your needs! 然后您可以使用这两个功能或其中之一来满足您的需要!

Example: 例:

alert(Remove("crt/r2002_2", 4, 1));

Output: crt/2002_2 输出:crt / 2002_2

Achieving goals by doing techniques with no logic might cause confusions in understanding of the code, and future mistakes, if you do this a lot in a large project! 如果您在大型项目中经常这样做,那么通过不使用逻辑的技术来实现目标可能会导致对代码理解的困惑以及将来的错误!


#4楼

In C# (Sharp), you can make an empty character as '\\0'. 在C#(Sharp)中,您可以将一个空字符设为'\\ 0'。 Maybe you can do this: 也许您可以这样做:

String.prototype.replaceAt = function (index, char) {
return this.substr(0, index) + char + this.substr(index + char.length);
}
mystring.replaceAt(4, '\0')

Search on google or surf on the interent and check if javascript allows you to make empty characters, like C# does. 在Google或Interent上搜索,然后检查javascript是否允许您像C#一样使空字符。 If yes, then learn how to do it, and maybe the replaceAt function will work at last, and you'll achieve what you want! 如果是,那么学习如何做,也许replaceAt函数最后将起作用,您将实现所需的功能!

Finally that 'r' character will be removed! 最终,“ r”字符将被删除!


#5楼

A simple functional javascript way would be 一种简单的功能性javascript方式是

mystring = mystring.split('/r').join('/')

simple, fast, it replace globally and no need for functions or prototypes 简单,快速,可在全球范围内替换,无需功能或原型


#6楼

The following function worked best for my case: 以下功能最适合我的情况:

public static cut(value: string, cutStart: number, cutEnd: number): string {
    return value.substring(0, cutStart) + value.substring(cutEnd + 1, value.length);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章