-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[js] 第3天 去除字符串中最后一个指定的字符 #9
Comments
function delLast (str,del) { |
|
🐶 function delLast(str, target) {
return str.split('').reverse().join('').replace(target, '').split('').reverse().join('');
}
const str = delLast('asdfghhj', 'h')
console.log(str) // asdfghj |
String.prototype.reverse = function () {
return this.split('').reverse().join('')
}
String.prototype.removeFirstChar = function (m) {
return this.replace(m, '')
}
const string = 'emamam, your string'
const removedChar = 'm'
string.reverse().removeFirstChar(removedChar).reverse() |
function delLast(str,target) {
let reg =new RegExp(`${target}(?=([^${target}]*)$)`)
return str.replace(reg,'')
} |
怎么开始全是正则????@haizhilin2013 |
function delLastStr(str, delStr) {
} |
1:性能问题 个人见解 |
|
|
---楼主typeof打错啦,还有str.substring(0,index ) + str.substring(index+1,str.length);这行应该也要return一下吧,不然成功删除了以后没有返回值 |
const removeRight = (str, target) => {
if (!str || !target) {
return str;
}
const idx = str.lastIndexOf(target);
if (idx > -1) {
return str.slice(0, idx) + str.slice(idx + 1);
} else {
return str;
}
};
const str = "aaa1 bc de a 2";
console.log(removeRight(str, "e"));
console.log(removeRight(str, "a"));
console.log(removeRight(str, "c")); |
思路:循环,从字符串尾部开始移动,直到匹配到的第一个目标字符,将它删除,退出循环,循环结束或退出后,返回字符串。 |
var filterLast = function(str, del) {
const index = str.lastIndexOf(del);
if(index > -1) {
str = [...str];
str.splice(index, 1);
return str.join('');
}
return str;
} |
@undefinedYu 借用了一下你的方法稍作修改,让它能输出 |
function delString(str, delChar) {
if (!str || typeof str !== 'string' || !delChar || typeof delChar !== 'string') return false;
let arr = str.split('');
let idx = arr.lastIndexOf(delChar);
arr.splice(idx, 1);
return arr.join('');
} |
前面有朋友通过[...str]来实现,没考虑到如果不是单一字符的情况吧 function removeLastStr(str, dstr) {
var arr = str.split(dstr);
var len = arr.length;
var nArr = [].concat(arr.slice(0, len-2))
nArr.push(arr[len-2]+arr[len-1])
return nArr.join(dstr)
} |
lastIndexOf看看最后字的索引,截取即可 |
function subStrIn(str, value) { console.log(subStrIn("1234345", "3")); |
/**
*
* @param string '字符串'
* @param delItem '删除的字符'
*/
function delLastItem(string,delItem){
let arr = string.split('')
arr.map((item,index)=>{
if(delItem === item){
arr.splice(index,1)
}
})
console.log(136,arr.join(''))
}
delLastItem('12323sdaas2','a') //输出 '12323sdas2' |
function deleteSpecifiedLastCharacter(str, char) {
if (!char || typeof str !== 'string' || typeof char !== 'string') return str
return str.replace(new RegExp(`${char}(?=([^${char}]*)$)`), '')
} |
var a = (str,s)=>{
|
delLast('emamam, your string', 'ur') // =>emamam, your string |
function delLastStr(str, delStr) {
const reg = new RegExp(`^(.*)(${delStr})(.*)$`)
return str.replace(reg, '$1$3')
}
delLastStr('emamam, your string', 'ur') |
|
|
|
|
const deleteLastChar = (str, c) => { |
function removestr(str,target){ |
// 去除字符串中最后一个指定的字符 console.log(delLast('asavded242%1%f5f6h7h8s9s', '2')) |
function deleteStrLastAppoint (str, appoint) { |
function delLast(){ |
lastIndexOf + splice + join方法function delLastAnyChar(str, char) {
str = [...str];
let index = str.lastIndexOf(char);
str.splice(index, 1);
return str.join("");
}
数组遍历let newStr = [];
function delLastAnyChar(str, char) {
str = [...str];
let index = str.lastIndexOf(char);
str.forEach((v, i) => (i !== index ? newStr.push(v) : false));
return newStr.join("");
}
正则+substringlet strList = [];
function delLastAnyChar(str, char) {
let reg = eval(`/${char}/g`);
// 使用matchAll方法或者exec方法进行全局匹配
// while(res=reg.exec(str)) strList.push(res)
for (const i of str.matchAll(reg)) {
strList.push(i.index);
}
let index = strList[strList.length - 1];
console.log(index);
return str.substring(0, index) + str.substring(index + 1, str.length);
}
正则+replacefunction delLastAnyChar(str, char) {
// [^]就是相当于通配符`.`,([^]*)表示匹配任意长度的字符,并且为获取匹配
let reg = new RegExp(`^([^]*)(${char})([^]*)$`);
// console.log(str.match(eval(`/^([^]*)(${char})([^]*)$/`)));
return str.replace(reg, "$1$3");
}
模式可以是一个字符串或者一个正则表达式,替换值可以是一个字符串或者一个每次匹配都要调用的回调函数 如果 |
直接分割字符串然后拼接即可
|
let myStr = 'ddfekekdfkdjfioej' |
function removestr(str,target){ |
|
//第3天 去除字符串中最后一个指定的字符 |
|
您好,邮件已收到,我会尽快回复。
|
function DeleteCharAt(data, c){ |
const delLastChar = (str = '', char = '') => { |
|
function findLastCharacter(str, character) {
let index = str.lastIndexOf(character)
let res = str.slice(0, index) + str.slice(index+1)
console.log(res);
}
findLastCharacter('sfafasf_u_ser_li_stnihao serach', 's') |
|
// 考虑删除字符可能是多位字符的情况 |
先行断言正则 function delLastChar(str, target) {
let reg = new RegExp(`${target}(?=([^${target}]*)$)`)
return str.replace(reg, '')
} |
|
function delStr(str, delItem){ |
function strDel(str,target){ |
function trim(str, char) { |
let str = '12345678910';; |
|
function replaceFn(str, tar) { (学习大佬后记录一下) |
您好,邮件已收到,我会尽快回复。
|
const delLast = (str, chr) => { } console.log(delLast('oncekiing', 'i')); |
const delLast = (str, chr) => { } console.log(delLast('oncekiing', 'i')); |
[js] 第3天 去除字符串中最后一个指定的字符
The text was updated successfully, but these errors were encountered: