天天看點

如何使用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);
}
           

繼續閱讀