如何在 TypeScript 中获取子字符串?
字符串包含各种字符,子字符串是字符串的一部分。我们可以通过两种方式从字符串中获取子字符串。第一种是使用子字符串的起始和结束位置,第二种是使用子字符串的起始位置和长度。
在本教程中,我们将学习在 TypeScript 中从字符串中获取子字符串的两种方法。
使用起始位置和结束位置获取子字符串
在 TypeScript 中,字符串的索引从零开始。因此,如果我们有一个从零开始的子字符串的起始和结束位置,我们可以从特定字符串中获取子字符串。
语法
用户可以按照以下语法使用起始和结束位置从特定字符串中获取子字符串。
get_sub_string(start, end): string { let resultantString = ""; for (let i = 0; i < this.string.length; i++) { if (i >= start && i <= end) { resultantString += this.string[i]; } } return resultantString; }
算法
步骤 1 - 定义 get_sub_string() 方法,该方法获取子字符串的起始和结束索引并返回字符串。
步骤 2 - 创建字符串类型的 resultantString 变量来存储子字符串。
步骤 3 - 使用 for 循环遍历字符串,我们需要获取子字符串。
步骤 4 - 在 for 循环中,检查索引是否位于包含的开始和结束之间,然后将该位置的字符附加到 resultantString 变量。
步骤 5 - for 循环迭代完成后,返回 resultantString,这是所需的子字符串。
示例
在下面的示例中,我们创建了包含字符串成员的类 stringClass。此外,我们根据上述语法和算法实现了 get_sub_string() 方法。
之后,我们创建了 stringClass 的对象,并通过传递各种起始和终止位置作为参数来调用 get_sub_sting() 方法。
// 创建一个名为 stringClass 的类 class stringClass { // string memeber string: string; // 构造函数初始化字符串成员 constructor(str: string) { this.string = str; } // 获取子字符串的方法,采用开始和结束属性 get_sub_string(start: number, end: number): string { // 定义 resultString 变量来存储子字符串 let resultantString = ""; for (let i = 0; i < this.string.length; i++) { // 如果索引位于开始和结束之间,则将该位置的字符包含在结果字符串中 if (i >= start && i <= end) { resultantString += this.string[i]; } } // 返回子字符串 return resultantString; } } let str1: stringClass = new stringClass("TutorialsPoint"); console.log( "The substring of TutorialsPoint starting from 2 to 5 is " + str1.get_sub_string(2, 5) ); console.log( "The substring of TutorialsPoint starting from 0 to 7 is " + str1.get_sub_string(0, 7) );
编译后,它将生成以下 JavaScript 代码 -
// 创建一个名为 stringClass 的类 var stringClass = /** @class */ (function () { // 构造函数初始化字符串成员 function stringClass(str) { this.string = str; } // 获取子字符串的方法,采用开始和结束属性 stringClass.prototype.get_sub_string = function (start, end) { // 定义 resultString 变量来存储子字符串 var resultantString = ""; for (var i = 0; i < this.string.length; i++) { // 如果索引位于开始和结束之间,则将该位置的字符包含在结果字符串中 if (i >= start && i <= end) { resultantString += this.string[i]; } } // 返回子字符串 return resultantString; }; return stringClass; }()); var str1 = new stringClass("TutorialsPoint"); console.log("The substring of TutorialsPoint starting from 2 to 5 is " + str1.get_sub_string(2, 5)); console.log("The substring of TutorialsPoint starting from 0 to 7 is " + str1.get_sub_string(0, 7));
输出
上述代码将产生以下输出 -
The substring of TutorialsPoint starting from 2 to 5 is tori The substring of TutorialsPoint starting from 0 to 7 is Tutorial
使用 substring() 方法获取 子字符串
我们可以使用 TypeScript 中字符串类的 substring() 方法,而不是从头编写一个函数来使用起始和终止位置获取子字符串。它也像上面的示例一样工作并返回子字符串。
语法
用户可以按照以下语法在 TypeScript 中使用 substring() 方法。
let str: string = "Hello"; let subString: string = str.substring(start,[end]);
参数
start − 它是一个必需参数,表示我们需要从哪里获取子字符串的起始位置。
end − 它是一个可选参数,直到我们获得子字符串。它从结束位置排除字符。
返回值
在此示例中,我们定义了名为字符串变量的 sampleString。此外,我们以 samleString 为引用使用了 substring() 方法。此外,我们为 substring() 方法传递了不同的 start 和 end 参数值,用户可以观察输出。
示例
let sampleString: string = "Welcome"; let subString: string = sampleString.substring(0, 4); console.log("The substring from 0 to 4 is " + subString); // 使用 substring() 方法而不传递结束参数 subString = sampleString.substring(2); console.log("The substring from 2 to end is " + subString); subString = sampleString.substring(-2, 5); console.log("The substring from -2 to 5 is " + subString); subString = sampleString.substring(-2, -5); console.log("The substring from -2 to -5 is " + subString); subString = sampleString.substring(-5, -2); console.log("The substring from -5 to -2 is " + subString);
编译后,它将生成以下 JavaScript 代码 -
var sampleString = "Welcome"; var subString = sampleString.substring(0, 4); console.log("The substring from 0 to 4 is " + subString); // 使用 substring() 方法而不传递结束参数 subString = sampleString.substring(2); console.log("The substring from 2 to end is " + subString); subString = sampleString.substring(-2, 5); console.log("The substring from -2 to 5 is " + subString); subString = sampleString.substring(-2, -5); console.log("The substring from -2 to -5 is " + subString); subString = sampleString.substring(-5, -2); console.log("The substring from -5 to -2 is " + subString);
输出
上述代码将产生以下输出 -
The substring from 0 to 4 is Welc The substring from 2 to end is lcome The substring from -2 to 5 is Welco The substring from -2 to -5 is The substring from -5 to -2 is
在上面的输出中,用户可以观察到,如果我们将两个负值作为参数传递,则 substring() 方法将返回一个空字符串。如果我们仅传递起始负值,它将从第 0 个索引开始子字符串。此外,如果我们不传递结束参数,它将返回从开头到字符串长度的子字符串。
使用 substr() 方法获取子字符串
在 TypeScript 中,substr() 方法也从特定字符串返回子字符串,并且几乎与字符串类的 substring() 方法相同。substr() 和 substring() 方法之间的基本区别在于它们作为参数所采用的值。 substring() 将结束位置作为第二个参数,而 substr() 将子字符串的长度作为第二个参数。
语法
在下面的语法中,我们使用了 substr() 方法。
let str2: string = "Hi there!"; let substring: stirng = str2.substr(start, [len])
参数
start − 它是子字符串从零开始的起始位置。
len − 它是可选参数,表示子字符串的长度。
返回值
substr() 方法返回从起始位置开始、长度为 len 的子字符串。如果我们不传递 len 参数,它将返回从开始到结束的子字符串。
示例
我们在此示例中使用了具有不同参数值的 substr() 方法。用户可以观察输出以及它如何返回具有不同 start 和 len 参数值的子字符串。
let demoStr: string = "Shubham"; let substring: string = demoStr.substr(1,3); console.log("The substring of length 3 starting from 1st index is " + substring); // substr() 方法没有可选参数 substring = demoStr.substr(3); console.log("The substring starting from 3rd index is " + substring);
编译后,它将生成以下 JavaScript 代码 -
var demoStr = "Shubham"; var substring = demoStr.substr(1, 3); console.log("The substring of length 3 starting from 1st index is " + substring); // substr() 方法没有可选参数 substring = demoStr.substr(3); console.log("The substring starting from 3rd index is " + substring);
输出
上述代码将产生以下输出 -
The substring of length 3 starting from 1st index is hub The substring starting from 3rd index is bham