java 大小写转换函数_不使用Java中的任何库函数将大写转换为小写

java 大小写转换函数

Given a string and we have to convert it from uppercase to lowercase.

给定一个字符串,我们必须将其从大写转换为小写。

Examples:

例子:

    Input:
    IncludeHelp.com
    Output:
    includehelp.com

    Input:
    [email protected]
    Output:
    [email protected]

大写到小写转换 (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).

要将大写字母转换为小写字母 –我们可以在大写字母的ASCII代码中添加32 ,以使其成为小写字母(因为小写字母ASCII和大写字母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.

在下面的代码中,我们创建了一个用户定义的函数UpperToLower() ,该函数将接受一个字符串并返回包含小写字母的字符串。 要将字符串的大写字母转换为小写字母 ,我们使用String.charAt()函数从字符串中逐个提取字符,并检查字符是否为大写字母,如果是大写字母,则将32加到将其设置为小写,否则将保持不变。 因此,只有大写字母将转换为小写字母,其余字符(如小写字母,数字和特殊字符)将保持不变。

大写到小写转换的Java代码 (Java code for uppercase to lowercase conversion)

// Uppercase to lowercase conversion without using 
// any library function in Java

public 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 Uppercase
            if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') {
                ch = (char)(s.charAt(i) + 32);
            }
            //else keep the same alphabet or any character
            else {
                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 大小写转换函数

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值