我正在尝试用'denied'一词替换一个子串。例如:如果原始字符串为“ abcdefg”,而要替换的子字符串为“ bcd”,则预期输出为“ a DENIED efg”。
我不能使用.replace
或其他任何东西,只能使用.substring
function replacer (variable, replace) {
for (let a = variable.length - 1; a >=0; a--) {
for (let b = replace.length - 1; b >= 0; b--) {
if (replace[b] === variable[a]) {
}
}
}
}
我不知道下一步该怎么做。
这是我的代码,用于从字符串中删除字符。
let stringToReturn = original;
for (let a = toDelete.length - 1; a >= 0; a--) {
for (let b = original.length - 1; b >= 0; b--) {
if (original[b] === toDelete[a]) {
stringToReturn = stringToReturn.substring(0, b) + stringToReturn.substring(b + 1, stringToReturn.length);
} else {
continue;
}
}
}
alert(stringToReturn);
}
但是这次,我不必删除一个字符,而是找到一个子字符串替换为 DENIED 。我对代码风格表示歉意。
答案 0 :(得分:2)
如果您知道要替换的子字符串的长度,则可以遍历“窗口”,只需迭代该字符串并检查该长度的所有可能的子字符串:
function replace(full, partial, placeholder) {
for (let i = 0; i <= full.length - partial.length; i++) {
const current = full.substring(i, i + partial.length);
if (current === partial) {
const prefix = full.substring(0, i);
const suffix = full.substring(i + partial.length);
return `${prefix}${placeholder}${suffix}`;
}
}
}
const ans = replace('abcdefghij', 'def', 'DENIED');
console.log(ans);
如果要替换所有出现的内容,请不要在第一个匹配项之后返回该值:
function replaceAll(full, partial, placeholder) {
let tmp = full;
for (let i = 0; i <= tmp.length - partial.length; i++) {
const current = tmp.substring(i, i + partial.length);
if (current === partial) {
const prefix = tmp.substring(0, i);
const suffix = tmp.substring(i + partial.length);
tmp = `${prefix}${placeholder}${suffix}`;
i += placeholder.length;
}
}
return tmp;
}
const ans = replaceAll('abcdefghijdef', 'def', 'DENIED');
console.log(ans);
答案 1 :(得分:1)
const source = "abcdefg";
const target = "bcd";
const replacer = "DENIED";
const replace = (source, target, replacer) => {
const position = source.indexOf(target);
if(position === -1) return source;
let output = source.substr(0, position)
output += replacer
output += source.substr(position + target.length);
return output
}
const replaceAll = (source, target, replacer) => {
let output = source;
do{
output = replace(output, target, replacer)
}
while(output !== replace(output, target, replacer))
return output;
}
console.log(replace(source, target, replacer))
当然,最好的解决方案是:
const replaceAll = (source, target, replacer) => {
return source.split(target).join(replacer)
}
答案 2 :(得分:1)
由于您未指定要执行完全替换还是单数操作,因此我已对其进行了修改,以允许使用boolean
参数,因此此boolean
表示是单数操作还是单数操作完全替换。
const replaceword = "DENIED";
const testword = "abcdef";
const testword2 = "abcdefdexyz";
const testword3 = "hello I don't have the sub";
//function takes a word parameter - the word to do a replace on
//and sub parameter of what to replace
//and replacement parameter of what to replace the substring with
//replaceall is a boolean as to whether to do a full replace or singular
function replace(word, sub, replacement, replaceall){
replaceall = replaceall || false; //default to singular replace
//Get the first index of the sub to replace
const startOfReplace = word.indexOf(sub);
//get the ending index of where the substring to be replaced ends
const endOfReplace = startOfReplace + sub.length - 1;
//variable to hold new word after replace
let replacedWord = "";
//If the substring is found do the replacement with the given replacement word
if(startOfReplace > -1){
for(let i = 0; i < word.length; i++){
if(i == startOfReplace){
replacedWord += replacement;
}else if(i >= startOfReplace && i <= endOfReplace){
continue;
}else{
replacedWord += word[i];
}
}
}else{ //set the return to the passed in word as no replacement can be done
replacedWord = word;
return replacedWord;
}
if(replaceall) //if boolean is true, recursively call function to replace all occurrences
//recursive call if the word has the sub more than once
return replace(replacedWord, sub, replacement);
else
return replacedWord; //else do the singular replacement
}
console.log(replace(testword, "de", replaceword));
console.log(replace(testword2, "de", replaceword, true));
console.log(replace(testword3, "de", replaceword));