如何在 TypeScript 中将字符串转换为大写?
在本 TypeScript 教程中,我们将学习如何将字符串转换为大写。我们需要将每个字母字符都转换为大写,以将整个字符串转换为大写,同时保持数字和特殊字符不变。
作为一名初学者 TypeScript 程序员,您可能会产生一个疑问,为什么要将字符串转换为大写?答案非常简单。您是否曾经使用过任何网站的搜索栏,在搜索时会显示不同的结果?如果是,请搜索大写和小写字符串,它们都会返回搜索结果。这意味着无论您在搜索栏中搜索什么,服务器都会将其转换为大写或小写,然后与数据库进行匹配。
此外,在某些网站上填写表单时,您会发现即使您输入小写字符,它也会转换为大写。因此,将字符串转换为大写有很多用途。
在这里,我们将学习两种在 TypeScript 中将字符串转换为大写的方法。
使用 TypeScript 的 String.toUpperCase() 方法
toUpperCase() 方法是 TypeScript 中的内置库方法,用于将字符串转换为大写。我们可以通过获取字符串作为引用来调用 toUpperCase() 方法。此外,与 JavaScript 不同,TypeScript 不允许在任何其他数据类型变量上调用 toUpperCase() 方法,因为它具有类型安全特性。
语法
用户可以按照以下语法使用 TypeScript 中的 toUpperCase() 方法将字符串转换为大写。
let emp_name: string = "Shubham Vora"; let result = emp_name.toUpperCase()
步骤
步骤 1 − 定义一个包含一些小写字母的字符串。
步骤 2 − 使用 toUpperCase() 方法将上面定义的字符串转换为大写。
步骤 3 − 打印转换后的字符串。
示例 1
在下面的示例中,我们声明了一个字符串。之后,我们使用 toUpperCase() 方法将字符串转换为大写。
// Creating a string
let str: string = "Tutorials Point, Simply Easy Learning";
console.log("Before converting to Uppercase: " + str )
// Converting the string to uppercase
let str_upper = str.toUpperCase();
console.log( "After converting to uppercase: " + str_upper);
编译后,它将生成以下 JavaScript 代码:
// Creating a string
var str = "Tutorials Point, Simply Easy Learning";
console.log("Before converting to Uppercase: " + str);
// Converting the string to uppercase
var str_upper = str.toUpperCase();
console.log("After converting to uppercase: " + str_upper);
输出
以上代码将产生以下输出:
Before converting to Uppercase: Tutorials Point, Simply Easy Learning After converting to uppercase: TUTORIALS POINT, SIMPLY EASY LEARNING
示例 2
在下面的示例中,我们声明了一个字符串,其中也包含一些特殊字符和数字。之后,我们使用 toUpperCase() 方法将字符串转换为大写。
// Creating a string
let str: string = "@weds!23sd$ #$3c ^%^Gf";
console.log(" Before converting to Uppercase: " + str);
// Converting the string to uppercase
let upper_str = str.toUpperCase();
console.log("After converting to Uppercase: " + upper_str);
编译后,它将生成以下 JavaScript 代码:
// Creating a string
var str = "@weds!23sd$ #$3c ^%^Gf";
console.log(" Before converting to Uppercase: " + str);
// Converting the string to uppercase
var upper_str = str.toUpperCase();
console.log("After converting to Uppercase: " + upper_str);
输出
以上代码将产生以下输出:
Before converting to Uppercase: @weds!23sd$ #$3c ^%^Gf After converting to Uppercase: @WEDS!23SD$ #$3C ^%^GF
在示例输出中,用户可以观察到 toUpperCase() 方法会保留特殊字符和数字,并将字母字符转换为大写。
使用 TypeScript 的 toLocaleUpperCase() 方法
toLocaleUpperCase() 方法也是 TypeScript 中的内置库方法,在大多数情况下返回与 toUpperCase() 相同的结果。它在大小写映射后返回字符串的大写字符。某些语言环境(例如土耳其语)不遵循大小写映射;在这种情况下,toLocaleUpperCase() 方法返回不同的结果。
语法
在以下语法中,我们演示了如何使用 TypeScript 的 toLocaleUpperCase() 方法将字符串转换为大写。
let message: string = "Hello World!"; let result = emp_name.toLocaleUpperCase( loclaes );
参数
toLocaleUpperCase() 方法接受一个参数。
locales − 这是一个可选参数。它是一个语言标签,表示需要根据哪个特定的区域语言将字符串转换为大写。
示例 1
在下面的示例中,我们定义了一个字符串。我们根据“lt-LT”语言环境将字符串转换为大写。
let str: string = "Tutorials Point";
console.log("Before converting to Uppercase: ", str)
// Using the toLocaleUpperCase() method
let upp_str = str.toLocaleUpperCase("lt-LT");
console.log("After converting to Uppercase: " + upp_str);
编译后,它将生成以下 JavaScript 代码:
var str = "Tutorials Point";
console.log("Before converting to Uppercase: ", str);
// Using the toLocaleUpperCase() method
var upp_str = str.toLocaleUpperCase("lt-LT");
console.log("After converting to Uppercase: " + upp_str);
输出
以上代码将产生以下输出:
Before converting to Uppercase: Tutorials Point After converting to Uppercase: TUTORIALS POINT
示例 2
在下面的示例中,我们定义了一个字符串。我们使用 toLocaleUpperCase() 方法将字符串转换为大写。我们将“en-US”作为语言环境参数传递给此方法。
let str: string = "Tutorials Point";
console.log("String before converting to Uppercase: ", str)
// Using the toLocaleUpperCase() method
let upp_str = str.toLocaleUpperCase("en-US");
console.log("String after converting to Uppercase: " + upp_str);
编译后,它将生成以下 JavaScript 代码:
var str = "Tutorials Point";
console.log("String before converting to Uppercase: ", str);
// Using the toLocaleUpperCase() method
var upp_str = str.toLocaleUpperCase("en-US");
console.log("String after converting to Uppercase: " + upp_str);
输出
以上代码将产生以下输出:
String before converting to Uppercase: Tutorials Point String after converting to Uppercase: TUTORIALS POINT
用户学习了两种在 TypeScript 中将字符串转换为大写的方法。但是,两种方法都返回相同的结果,除了某些 Unicode 字符,其默认映射不同。建议使用 toUpperCase() 方法在 TypeScript 中将字符串转换为大写。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP