您好,欢迎访问一九零五行业门户网

如何创建一半大写、另一半小写的字符串?

要将字符串转换为小写和大写,我们可以使用 javascript 字符串类的内置方法,如 tolowercase() 和 touppercase()。此外,我们可以使用字符串长度属性或 substring() 方法将字符串分成两半。
我们将在本教程中学习两种方法,使用 javascript 将一半字符串转换为大写,另一半转换为小写。
使用 for 循环、touppercase() 和 tolowercase() 方法我们可以使用for循环来获取半字符串。之后,我们可以使用 touppercase() 方法将字符串的前半部分转换为大写。之后,我们需要使用 tolowercase() 方法将字符串的后半部分转换为小写。
语法用户可以按照下面的语法使用touppercase()和tolowercase()方法将字符串的前半部分转换为大写,另一部分转换为小写。
for (let k = 0; k < length / 2; k++) { newstr += string[k].touppercase();}for (k = length / 2; k < length; k++) { newstr += string[k].tolowercase();}
在上面的语法中,长度是字符串长度,我们将字符串的特定字符转换为大小写。
算法第 1 步 - 使用字符串的 length 属性并获取字符串长度。
第 2 步 - 创建一个 newstr 变量并使用空字符串对其进行初始化以存储新转换的字符串。
步骤 3 - 使用 for 循环迭代前半部分。另外,将前半部分的每个字符转换为大写并将其附加到 newstr 字符串变量中。
步骤 4 - 现在,使用 for 循环迭代字符串的另一半,并使用 tolowercase() 方法将每个字符转换为小写。另外,将小写字符附加到 newstr 变量。
第 5 步 - newstr 变量包含转换后的字符串。
示例 1在下面的示例中,我们创建了包含一些字符的字符串。之后,我们使用上面的算法将字符串的前半部分转换为大写,另一半转换为小写。
在输出中,用户可以观察转换后的字符串。
<html><body> <h2>using the <i> for-loop, touppercase(), and tolowercase() </i> methods </h2> <h3>converting first half of the string in uppercase and another half in lowercase</h3> <div id = output> </div> <script> let output = document.getelementbyid('output'); let string = fdsdksndsmsdsaxs; let length = string.length; let newstr = ; for (let k = 0; k < length / 2; k++) { newstr += string[k].touppercase(); } for (k = length / 2; k < length; k++) { newstr += string[k].tolowercase(); } output.innerhtml += the original string is + string + <br/>; output.innerhtml += the converted string is + newstr + <br/>; </script></body></html>
步骤第 1 步 - 获取长度变量中字符串的长度。
第2步 - 创建firsthalf和secondhalf变量并使用空字符串初始化它们。
步骤 3 - 现在,使用 for-of 循​​环迭代每个字符串字符。此外,在每次迭代中,将一个字符附加到firsthalf字符串变量并检查k变量的值。如果大于length/2,则中断循环。
第 4 步 - 现在,再次使用 for-of 循​​环迭代后半部分,并执行与前半部分相同的操作。
第5步 - 接下来,将firsthalf转换为大写,将secondhalf转换为小写。之后,使用“+”运算符合并两个字符串。
示例 2在下面的示例中,第一个 for-of 循​​环查找字符串的前半部分,另一个 for-of 循​​环查找字符串的后半部分。我们还在输出中使用转换后的字符串显示了firsthalf 和secondhalf 变量值。
<html><body> <h2>using the <i>for-of loop, touppercase(), and tolowercase()</i> methods to convert first half of the string in uppercase and another half in lowercase.</h2> <div id = output> </div> <script> let output = document.getelementbyid('output'); let originalstring = fsfjkkjkuyfersdfxwazasawecas; let length = originalstring.length; let firsthalf = ; let secondhalf = ; let k = 0; for (let char of originalstring) { if (k < length / 2) { firsthalf += char k++; } else { break; } } k=0; for (let char of originalstring) { if (k>= length/2 && k < length) { secondhalf += char } k++; } let convertedstr = firsthalf.touppercase() + secondhalf.tolowercase(); output.innerhtml += the original string is + originalstring + <br/>; output.innerhtml += the first half of the string is + firsthalf + <br/>; output.innerhtml += the second half of the string is + secondhalf + <br/>; output.innerhtml += the converted string is + convertedstr + <br/>; </script></body></html>
使用 substr()、touppercase() 和 tolowercase() 方法substr() 方法允许我们从原始字符串中获取子字符串。在这种方法中,我们将使用 substr() 方法获取前半子字符串和后半子字符串。
语法用户可以按照下面的语法使用 substr()、touppercase() 和 tolowercase() 方法将前半部分转换为大写,另一部分转换为小写。
let convertedstr = stringtoconvert.substr(0, length / 2).touppercase() + stringtoconvert.substr(length / 2).tolowercase();
在上面的语法中,我们使用了两次 substr() 方法来获取前半字符串和另一半字符串。
算法第 1 步 - 获取字符串的长度。
步骤 2 - 使用 substr() 方法获取前半部分。用户需要传入0作为第一个参数,代表起始点,length/2作为第二个参数,代表子串的长度。
步骤 3 - 再次使用 substr() 方法获得另一半。将 length/2 作为第一个参数传递,以获取从 length/2 到结尾的子字符串。
示例 3在下面的示例中,我们使用 substr() 方法获取子字符串的两半,并对前半部分使用 touppercase() 方法,对后半部分使用 tolowercase() 方法。
<html><body> <h2>using the <i> substr(), touppercase(), and tolowercase() </i> methods to convert first half of the string in uppercase and another half in lowercase.</h2> <div id = output> </div> <script> let output = document.getelementbyid('output'); let stringtoconvert = ertedcsrdewderere4ere; let length = stringtoconvert.length; let convertedstr = stringtoconvert.substr(0, length / 2).touppercase() + stringtoconvert.substr(length / 2).tolowercase(); output.innerhtml += the original string is + stringtoconvert + <br/>; output.innerhtml += the converted string is + convertedstr + <br/>; </script></body></html>
用户学会了使用 for 循环和 substr() 方法将字符串的前半部分转换为大写,另一半转换为小写。使用 substr() 方法,我们需要编写一行代码来实现我们的目标。因此,这是推荐的方法。
以上就是如何创建一半大写、另一半小写的字符串?的详细内容。
其它类似信息

推荐信息