如何使用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);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章