如何在TypeScript中替换字符串的子字符串?
有时,在使用TypeScript时,我们需要用新字符串或任何特定字符替换子字符串。替换子字符串或字符串部分的简单方法是使用replace()方法。
在这里,我们将创建一个自定义算法来替换字符串,用于初学者的面试目的。但是,我们也将在本教程的最后看到replace()方法。
创建自定义算法以替换子字符串
在本部分中,我们将使用for循环迭代主字符串。我们将使用字符串库的substr()方法来查找索引'i'处的子字符串是否与可替换字符串匹配。如果索引位置'i'处的子字符串与可替换字符串匹配,我们将用新字符串替换它。
语法
在下面的语法中,我们实现了自定义算法,用新字符串替换字符串的一部分。我们使用substr()方法查找旧子字符串。
function replaceSubString(
mainString: string,
oldString: string,
newString: string
): string {
let tempString: string = "";
for (let i = 0; i < mainString.length; i++) {
if (mainString.substr(i, oldString.length) === oldString) {
tempString = tempString + newString;
i = i + oldString.length-1;
} else {
tempString = tempString + mainString[i];
}
}
return tempString;
}
参数
上述自定义函数采用三个字符串类型的参数。
mainString − 这是我们需要替换字符串的字符串。
oldString − 这是要替换的子字符串。
newString − 这是将替换oldString的新字符串。
算法
步骤1 − 创建一个函数,该函数采用mainString、oldString和newString作为参数并返回一个字符串。
步骤2 − 创建一个tempString变量来存储替换后的字符串。
步骤3 − 使用for循环迭代字符串。
步骤4 − 使用substr()库方法从第i个索引获取与oldString长度相同的子字符串。
步骤5 − 将子字符串与oldString匹配。如果第i个索引位置的子字符串与oldString匹配,则将新字符串添加到tempString。
步骤6 − 将'i'变量的值增加oldstring的长度 - 1。
步骤7 − 如果第i个索引处的子字符串与oldString不匹配,则将mainString中第i个索引的字符添加到tempString。
步骤8 − 迭代完成后,返回tempString。
示例
在下面的示例中,我们使用了mainString将单个单词替换为新单词。我们创建了一个名为replaceSubstring()的自定义函数并实现了上述算法。
在输出中,用户可以观察到在mainString中,“TypeScript”字被“coding”字替换。
// define the mainString, oldString, newString
let mainString: string =
"TutorialsPoint is the best website to learn TypeScript!";
let oldString: string = "TypeScript";
let newString: string = "Coding";
// function to replace a substring
function replaceSubString(
mainString: string,
oldString: string,
newString: string
):
string {
// create a temporary string
let tempString: string = "";
// iterate through the string
for (let i = 0; i < mainString.length; i++) {
// get the substring from ith index of length same as oldString's length and compare it with the oldString.
// If it matches with oldString, add new string to tempstring, and increase i by oldString's length-1.
if (mainString.substr(i, oldString.length) === oldString) {
tempString = tempString + newString;
i = i + oldString.length - 1;
}
else {
// if substring at index ith position doesn't match, add character from ith index to tempString
tempString = tempString + mainString[i];
}
}
// return tempString after iteration.
return tempString;
}
// call the replaceSubString function
console.log("The old string is " + mainString);
console.log("The new string is ");
console.log(replaceSubString(mainString, oldString, newString));
编译后,它将生成以下JavaScript代码:
// define the mainString, oldString, newString
var mainString = "TutorialsPoint is the best website to learn TypeScript!";
var oldString = "TypeScript";
var newString = "Coding";
// function to replace a substring
function replaceSubString(mainString, oldString, newString) {
// create a temporary string
var tempString = "";
// iterate through the string
for (var i = 0; i < mainString.length; i++) {
// get the substring from ith index of length same as oldString's length and compare it with the oldString.
// If it matches with oldString, add new string to tempstring, and increase i by oldString's length-1.
if (mainString.substr(i, oldString.length) === oldString) {
tempString = tempString + newString;
i = i + oldString.length - 1;
} else {
// if substring at index ith position doesn't match, add character from ith index to tempString
tempString = tempString + mainString[i];
}
}
// return tempString after iteration.
return tempString;
}
// call the replaceSubString function
console.log("The old string is " + mainString);
console.log("The new string is ");
console.log(replaceSubString(mainString, oldString, newString));
输出
上述代码将产生以下输出:
The old string is TutorialsPoint is the best website to learn TypeScript! The new string is TutorialsPoint is the best website to learn Coding!
使用replace()方法替换字符串的一部分
replace方法允许我们替换字符串的正则表达式或特定单词。此外,我们还可以使用replace()方法替换特定子字符串的所有出现。我们需要通过获取需要进行替换的字符串作为引用来调用它。
语法
用户可以按照以下语法用新字符串替换字符串的特定部分。
str.replace(reg_exp, new_str);
参数
reg_exp − 它将正则表达式作为第一个参数来匹配子字符串。
new_str − 这是用于替换与正则表达式匹配的子字符串的新字符串。
示例
在这个例子中,我们使用了replace()方法用“sample”字符串替换str的特定子字符串。在输出中,用户可以观察到“demo”被“sample”替换。
// defining the string
let str: string = "This is a demo string!";
// use the replace method to replace the "demo" substring with "sample"
let newString: string = str.replace("demo", "sample");
console.log("The old string is " + str);
console.log("The new String is " + newString);
编译后,它将生成以下JavaScript代码:
// defining the string
var str = "This is a demo string!";
// use the replace method to replace the "demo" substring with "sample"
var newString = str.replace("demo", "sample");
console.log("The old string is " + str);
console.log("The new String is " + newString);
输出
上述代码将产生以下输出:
The old string is This is a demo string! The new String is This is a sample string!
在本教程中,我们学习了用新字符串替换字符串特定部分的不同方法。显然,替换字符串的最佳方法是使用replace()方法,它也采用正则表达式作为参数。尽管如此,我们仍然应该了解替换子字符串的朴素方法的基本实现。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP