Question: What's the **JavaScript** equivalent to this `C#` Method:

```
var x = "|f|oo||"; 
var y = x.Trim('|'); //  "f|oo"

```

C# trims the selected character only at the **beginning** and **end** of the string!

Answer: 
to keep this question up to date:

here is an approach i'd choose over the regex function using the ES6 spread operator.

```
function trimByChar(string, character) {
  const first = [...string].findIndex(char => char !== character);
  const last = [...string].reverse().findIndex(char => char !== character);
  return string.substring(first, string.length - last);
}

```

Improved version after @fabian 's comment (can handle strings containing the same character only)

```js
function trimByChar1(string, character) {
  const arr = Array.from(string);
  const first = arr.findIndex(char => char !== character);
  const last = arr.reverse().findIndex(char => char !== character);
  return (first === -1 && last === -1) ? '' : string.substring(first, string.length - last);
}
```
```
String.prototype.TrimStart = function (n) {
    if (this.charAt(0) == n)
        return this.substr(1);
};

String.prototype.TrimEnd = function (n) {
    if (this.slice(-1) == n)
        return this.slice(0, -1);
};

```