java 大小写转换函数_不使用Java中的任何库函数将大写转换为小写
java 大小寫轉(zhuǎn)換函數(shù)
Given a string and we have to convert it from uppercase to lowercase.
給定一個(gè)字符串,我們必須將其從大寫轉(zhuǎn)換為小寫。
Examples:
例子:
Input:IncludeHelp.comOutput:includehelp.comInput:[email?protected]Output:[email?protected]大寫到小寫轉(zhuǎn)換 (Uppercase to lowercase conversion)
To convert an uppercase alphabet to lowercase alphabet – we can add 32 in uppercase alphabet's ASCII code to make it a lowercase alphabet (because the difference between a lowercase alphabet ASCII and an uppercase alphabet ASCII is 32).
要將大寫字母轉(zhuǎn)換為小寫字母 –我們可以在大寫字母的ASCII代碼中添加32 ,以使其成為小寫字母(因?yàn)樾懽帜窤SCII和大寫字母ASCII之間的差是32 )。
In the below code, we created a user-defined function UpperToLower() that will accept a string and returns string having lowercase characters. To convert uppercase alphabets of the string to lowercase alphabets, we are extracting characters one by one from the string using String.charAt() function and checking whether the character is an uppercase alphabet, if it is an uppercase alphabet, we are adding 32 to make it lowercase, else no change. Thus, only uppercase alphabets will be converted to lowercase alphabets, the rest of the characters like lowercase alphabets, digits and special characters will remain the same.
在下面的代碼中,我們創(chuàng)建了一個(gè)用戶定義的函數(shù)UpperToLower() ,該函數(shù)將接受一個(gè)字符串并返回包含小寫字母的字符串。 要將字符串的大寫字母轉(zhuǎn)換為小寫字母 ,我們使用String.charAt()函數(shù)從字符串中逐個(gè)提取字符,并檢查字符是否為大寫字母,如果是大寫字母,則將32加到將其設(shè)置為小寫,否則將保持不變。 因此,只有大寫字母將轉(zhuǎn)換為小寫字母,其余字符(如小寫字母,數(shù)字和特殊字符)將保持不變。
大寫到小寫轉(zhuǎn)換的Java代碼 (Java code for uppercase to lowercase conversion)
// Uppercase to lowercase conversion without using // any library function in Javapublic class Main {static String UpperToLower(String s) {String result = "";char ch = ' ';for (int i = 0; i < s.length(); i++) {//check valid alphabet and it is in Uppercaseif (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') {ch = (char)(s.charAt(i) + 32);}//else keep the same alphabet or any characterelse {ch = (char)(s.charAt(i));}result += ch; // concatenation, append c to result}return result;}public static void main(String[] args) {System.out.println(UpperToLower("IncludeHelp.com"));System.out.println(UpperToLower("www.example.com"));System.out.println(UpperToLower("[email?protected]"));System.out.println(UpperToLower("[email?protected]")); } }Output
輸出量
includehelp.com www.example.com [email?protected] [email?protected]翻譯自: https://www.includehelp.com/java-programs/uppercase-to-lowercase-conversion-without-using-any-library-function-in-java.aspx
java 大小寫轉(zhuǎn)換函數(shù)
總結(jié)
以上是生活随笔為你收集整理的java 大小写转换函数_不使用Java中的任何库函数将大写转换为小写的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: appweb ejs_具有快速路线的EJ
- 下一篇: hashmap clone_Java H