js//1. (简答题) // 1、输入一个字符串,将该字符串转换为大写或小写 // 示例: 输入 hello 输出 HELLO // 输入 HELLO 输出 hello
时间: 2023-10-08 21:05:23 浏览: 46
答案:可以使用JavaScript中的toUpperCase()和toLowerCase()方法来实现将字符串转换为大写或小写的功能。代码如下:
```
// 将字符串转换为大写
let str1 = 'hello';
console.log(str1.toUpperCase()); // 输出 HELLO
// 将字符串转换为小写
let str2 = 'HELLO';
console.log(str2.toLowerCase()); // 输出 hello
```
相关问题
1. 简答题 定义一个字符串,计算字符串中大写字母、小写字母、数字的个数并输出。
首先,我们需要定义一个字符串并统计其中大写字母、小写字母和数字的个数。
可以使用 Python 的内置函数 isupper()、islower() 和 isdigit() 来判断一个字符是否为大写字母、小写字母和数字。
下面是一个示例代码:
```python
string = "Hello World 123"
upper_count = 0
lower_count = 0
digit_count = 0
for char in string:
if char.isupper():
upper_count += 1
elif char.islower():
lower_count += 1
elif char.isdigit():
digit_count += 1
print("大写字母个数:", upper_count)
print("小写字母个数:", lower_count)
print("数字个数:", digit_count)
```
输出结果:
```
大写字母个数: 2
小写字母个数: 8
数字个数: 3
```
这里我们定义了一个字符串变量 `string`,并使用了三个计数器变量 `upper_count`、`lower_count` 和 `digit_count` 来分别统计大写字母、小写字母和数字的个数。
然后我们使用 for 循环遍历字符串中的每个字符,如果字符是大写字母,则将 `upper_count` 计数器加 1,如果是小写字母,则将 `lower_count` 计数器加 1,如果是数字,则将 `digit_count` 计数器加 1。
最后输出三个计数器变量的值即可。
1. 简答题 从键盘输入一个字符串和一个字符,从字符串中删除制定的字符。
可以使用字符串的replace方法来删除指定的字符,代码如下:
```
string = input("请输入一个字符串:")
char = input("请输入要删除的字符:")
new_string = string.replace(char, "")
print("删除后的字符串为:", new_string)
```
这段代码首先从键盘输入一个字符串和一个字符,然后使用replace方法将字符串中所有的指定字符替换为空字符串,最后输出删除后的字符串。