JSFiddle - React, Tailwind, and code Playground
by mehmetatas
HTML
<table>
<tr>
<td>Input String: </td>
<td><input type="text" id="txtString" value="AbCdEfGhI" /></td>
</tr>
<tr>
<td>Value 1: </td>
<td><input type="text" id="txtValue1" /></td>
</tr>
<tr>
<td>Value 2: </td>
<td><input type="text" id="txtValue2" /></td>
</tr>
<tr>
<td>Ignore Case: </td>
<td><input type="checkbox" id="chkIgnoreCase" /></td>
</tr>
<tr>
<td colspan="2">
<input type="button" value="Trim End" onclick="testTrimEnd();" />
<input type="button" value="Trim Start" onclick="testTrimStart();" />
<input type="button" value="Replace All" onclick="testReplaceAll();" />
<input type="button" value="Starts With" onclick="testStartsWith();" />
<input type="button" value="Ends With" onclick="testEndsWith();" />
</td>
</tr>
</table>
CSS
body {
font-family: Verdana;
font-size: small;
margin-top: 10px;
margin-left: 20px;
}
td {
padding: 2px;
}
JavaScript
String.prototype.trim = function(token, ignoreCase) {
return this.trimEnd(token, ignoreCase).trimStart(token, ignoreCase);
};
String.prototype.trimStart = function(token, ignoreCase) {
var str = this;
if (typeof(token) !== "string") {
if (!(token instanceof Array)) {
token = [" ", "\n", "\r", "\t"];
}
var tmp;
do {
tmp = str;
for (i = 0; i < token.length; i++) {
str = str.trimStart(token[i], ignoreCase);
}
} while (tmp != str);
return str;
}
if (token === null) {
return this;
}
if (token.length === 0 || this.length === 0 || token.length > this.length) {
return this;
}
if (typeof(ignoreCase) !== "undefined" && ignoreCase) {
str = str.toLowerCase();
token = token.toLowerCase();
}
var start = 0;
while (str.indexOf(token, start) === 0) {
start += token.length;
}
return this.substring(start);
};
String.prototype.trimEnd = function(token, ignoreCase) {
var str = this;
if (typeof(token) !== "string") {
if (!(token instanceof Array)) {
token = [" ", "\n", "\r", "\t"];
}
var tmp;
do {
tmp = str;
for (i = 0; i < token.length; i++) {
str = str.trimEnd(token[i], ignoreCase);
}
} while (tmp != str);
return str;
}
if (token === null) {
return this;
}
if (token.length === 0 || this.length === 0 || token.length > this.length) {
return this;
}
if (typeof(ignoreCase) !== "undefined" && ignoreCase) {
str = str.toLowerCase();
token = token.toLowerCase();
}
var end = this.length - token.length;
while (str.indexOf(token, end) === end) {
end -= token.length;
}
return this.substring(0, end + token.length);
};
String.prototype.replaceAll = function(token, newToken,...